资源描述:
《t-sql语句(建库,建表,事务,存储过程,触发器等...)》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、T-SQL语句(建库,建表,事务,存储过程,触发器等...)T-SQL语句(建库,建表,事务,存储过程,触发器等...)dropdatabasebankDBusemasterexecsp_attach_db@dbname='bankDB',@filename1='f:bankbankDB_data.mdf'--附加数据库,参数1:物理名(数据库名),参数2:文件路径名execsp_detach_db'bankDB',null--分离数据库参数1:数据库名,参数2:类型值???gousemastergoife
2、xists(select*fromsysdatabaseswherename='bankDB')--判断系统是否存在bankDB数据库,存在就删除dropdatabasebankDBgoexecxp_cmdshell'mkdirF:bank'--新建文件夹gocreatedatabasebankDB--新建数据库名onprimary(name=bankDB_data,--逻辑名filename='F:bankbankDB_data.mdf',--主数据库名size=10mb,--文件大小filegrowt
3、h=15%--增长率)logon(name=bankDB_log,--逻辑名filename='F:bankbankDB_log.ldf',--日志文件size=1mb,--文件大小maxsize=20mb,--文件最大容量filegrowth=10%--增长率)goexecxp_cmdshell'rmdirF:bank'--删除数据库usebankDBgoifexists(select*fromsysobjectswherename='userInfo')--判断系统是否存在该表名,存在删除dropta
4、bleuserInfogocreatetableuserInfo--新建表名(UIDintnotnullidentity(1,1),--顾客编号,自动增长Unamevarchar(8)notnull,--开户名,必填PIDvarchar(18)notnull,--身份证号,必填telephonevarchar(13)notnull,--电话号码,必填addressvarchar(32)--地址,可选输入)altertableuserInfoaddconstraintPK_UIDprimarykey(UID),-
5、-主键约束constraintUQ_PIDunique(PID),--唯一约束constraintCK_PIDcheck(len(PID)=18orlen(PID)=15),--检查约束constraintCK_telephonecheck(telephonelike'0[0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'ortelephonelike'0[0-9][0-9][0-9]-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0
6、-9]'orlen(telephone)=11)createtablecardInfo(CUIDintnotnull,--顾客编号,外键CIDvarchar(19)notnull,--卡号,必填curTypevarchar(10)notnull,--货币种类,必填savingTypevarchar(10)notnull,--存款类型,必填openDatedatetimenotnull,--开户日期,必填openMoneyMoneynotnull,--开户金额,必填balanceMoneynotnull,--余额
7、,必填passvarchar(8)notnull,--密码,必填IsReportLossbitnotnull--是否挂失,必填)altertablecardInfoaddconstraintPK_CIDprimarykey(CID),--主键约束,CID为主键constraintCK_CIDcheck(CIDlike'10103576[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'),constraintDF_curTypedefault('RMB')forcurType,co
8、nstraintCK_savingTypecheck(savingTypein('活期','定活两便','定期')),constraintDF_openDatedefault(getDate())foropenDate,--默认约束,当天日期constraintCK_openMoneycheck(openMoney>=1),constraintCK_balancecheck(balan