SQLServer创建用户、数据库、表、约束、存储过程、视图
--创建登录账户和数据库用户
exec sp_addlogin 'sysAdmin',''
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创建用户、数据库、表、约束、存储过程、视图的更多相关文章
- ABP框架使用Mysql数据库,以及基于SQLServer创建Mysql数据库的架构和数据
ABP默认的数据库是SQLServer,不过ABP框架底层是EF框架,因此也是很容易支持其他类型的数据库的,本篇随笔介绍在ABP框架使用Mysql数据库,以及基于SQLServer创建MySql数据库 ...
- oracle12c创建用户和表空间出现的问题
Oracle12c 中,增加了可插接数据库的概念,即PDB,允许一个数据库容器(CDB)承载多个可插拔数据库(PDB).CDB全称为 ContainerDatabase,中文翻译为数据库容器,PDB全 ...
- iOS:CoreData数据库的使用一(创建单个数据库表)
CoreData数据库框架:mac系统自带的数据库,它是苹果公司对sqlite进行封装而来的,既提供了对数据库的主要操作,也提供了具体的视图关系模型. 需要用到三个对象: 1•Managed Obje ...
- 创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表
创建ASP.NET Core MVC应用程序(3)-基于Entity Framework Core(Code First)创建MySQL数据库表 创建数据模型类(POCO类) 在Models文件夹下添 ...
- Java创建Oracle数据库表
我们通常只用java执行DML(即:insert, update, delete, select)操作,很少用来执行DDL(create, drop, alert)操作.今天试了下如何用java来创建 ...
- django在admin后台注册自己创建的数据库表
django在admin后台注册自己创建的数据库表,这样我们就可以在admin后台看到表结构信息,我们就可以在admin后台快速录入表记录信息 如果没有注册,那么你在登录django自带的admin的 ...
- Oracle创建用户、表(1)
Oracle创建用户.表(1) 1. 连接 C:\Users\LEI>sqlplus / as sysdba SQL*Plus: Release 12.1.0.2.0 Production on ...
- SQLServer创建用户自定义数据库用户
创建用户自定义数据库用户注意事项 如果已忽略 FOR LOGIN,则新的数据库用户将被映射到同名的SQL Server登录名. 默认架构将是服务器为此数据库用户解析对象名时将搜索的第一个架构. 除非另 ...
- 创建oracle数据库表空间并分配用户
我们在本地的oracle上或者virtualbox的oracle上 创建新的数据库表空间操作:通过system账号来创建并授权/*--创建表空间create tablespace YUJKDATAda ...
- 用sqlplus为oracle创建用户和表空间<转>
用Oracle10g自带的企业管理器或PL/SQL图形化的方法创建表空间和用户以及分配权限是相对比较简单的,本文要介绍的是另一种方法,使用Oracle 9i所带的命令行工具:SQLPLUS来创建表空间 ...
随机推荐
- kendo grid应用经验总结
学习网址 https://docs.telerik.com/kendo-ui/controls/editors/dropdownlist/overview https://demos.telerik. ...
- ASP.NET控件--DropDownList
设置默认值:DropDownList1.Items[i].Selected=true;绑定:DropDownList1.DataSource = dataSet.Tables["Tabl ...
- Linux系统下C语言程序的构建过程
本文转载自:http://www.ruanyifeng.com/blog/2014/11/compiler.html 源码要运行,必须先转成二进制的机器码.这是编译器的任务. 比如,下面这段源码(假定 ...
- JVM内存分布
参考引用文章地址: http://hllvm.group.iteye.com/group/wiki/3053-JVMhttp://blog.csdn.net/william001zs/article/ ...
- leetcode_目录
3Sum Closest 3Sum 4Sum Add Binary Add Two Numbers Anagrams Balanced Binary Tree Best Time to Buy and ...
- 使用echo命令清空tomcat日志文件
使用echo命令清空日志文件echo -n "" > /server/tomcat/logs/catalina.out ==>要加上"-n"参数,默 ...
- margin显示怪异,外边距合并问题
很多时候我们使用两个div,内层的div设置文字,需要垂直居中与上层div,但是怎么设置样式都不行,vertical-align:middle也不行. 代码: <div style=" ...
- JWT(Json web token)认证详解
JWT(Json web token)认证详解 什么是JWT Json web token (JWT), 是为了在网络应用环境间传递声明而执行的一种基于JSON的开放标准((RFC 7519).该to ...
- 20145104张家明 《Java程序设计》第8周学习总结
20145104张家明 <Java程序设计>第8周学习总结 教材学习内容总结 第15章 -java.util.logging包提供了日志功能相关类与接口,不必额外配置日志组件,就可以在标准 ...
- 20145106 java实验二
1)复数类ComplexNumber的属性 m_dRealPart: 实部,代表复数的实数部分 m_dImaginPart: 虚部,代表复数的虚数部分 public class ComplexNumb ...