SQLServer创建用户、数据库、表、约束、存储过程、视图
--创建登录账户和数据库用户 exec sp_addlogin 'sysAdmin','123456' exec sp_grantdbaccess 'sysAdmin','aa' --给数据库用户赋权限 grant select,update,insert,delete on userInfo to aa --建立数据库前的判断 Use master GO if exists(select * from sysdatabases where name='bankDB') drop database bankDB GO exec xp_cmdshell 'md d:\bank',no_output go --建立数据库 create database bankDB on ( name='bankDB_data', filename='D:\bank\bankDB.mdf', size=3, filegrowth=15% ) log on ( name='bankDB_log', filename='D:\bank\bankDB_log.ldf', size=3, filegrowth=15% ) GO use bankDB GO --建立银行卡信息 表 if exists(select * from sysobjects where name='cardInfo') drop table cardInfo GO create table cardInfo ( cardID varchar(20) not null, curType varchar(5) not null, savingType nvarchar(4) not null, openDate datetime not null, openMoney money not null, balance money not null, pass varchar(6) not null, IsReportLoss bit not null, --是否挂失,1表示挂失 customerID int not null ) go --检查约束 alter table cardInfo add constraint CK_cardID check(cardID like '1010 3576 [0-9][0-9][0-9][0-9] [0-9][0-9][0-9][0-9]') --主键约束 alter table cardInfo add constraint PK_cardID primary key(cardID) --默认约束 alter table cardInfo add constraint DF_curType default('RMB') for curType --创建外键约束 alter table cardInfo add constraint FK_customerID foreign key(customerID) references userInfo(customerID) --创建唯一约束 alter table userInfo add constraint UK_PID unique(cardID) --创建索引 create unique nonclustered index IX_cardID on cardInfo(cardID) with fillfactor=70 --创建视图 create view v_userInfo as select customerID 客户编号,customerName 开户用户,PID 身份证号,telephone 电话,address 地址 from userInfo go select * from v_userInfo --创建存储过程 --产生随机号的存储过程 if exists(select * from sysobjects where name='proc_randCardID') drop proc proc_randCardID go create proc proc_randCardID @i varchar(10), @rand varchar(20) output as declare @b varchar(10) declare @r numeric(15,8) select @r=rand(datepart(mm,getdate())*100000+datepart(ss,getdate())*1000+datepart(ms,getdate())) select @b=substring(convert(varchar(30),@r),3,4)+' '+substring(convert(varchar(30),@r),7,4) set @rand=@i+' '+@b go --测试获得随机数 declare @result varchar(20) exec proc_randcardID '1524 2222',@result output select @result go
记录常用的一些使用方式,以备需要的时候查看!
分类:
数据库 / SqlServer
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗