SQL Server的基础


```sql
use master
go
drop database stuDB2
go
go 
create database stuDB2
go
use stuDB2
go
--约束写在表里,一般不这么写,不利于维护
create table stuInfo
(
	stuId int primary key identity(1,1),
	stuName nvarchar(32)  not null,
	stuAge  tinyint  check(stuAge between 5 and  30),
	stuSex  tinyint not  null,--0代表女,1代表男
	stuBrithday smalldatetime   null,
	stuAddress  nvarchar(256),
	stuMobile  nvarchar(16),
	stuNo  tinyint   --外键
)
--输入日期的时候可以用 -   . 和空格隔开,其它符号隔开无效
insert stuInfo values ('李文才',5,0,'1993 1 21','重庆市大足区','135276',3)
--smalldatetime 只能精确到分钟,如果遇到秒则采用四舍五入30S进一分钟,
insert stuInfo values ('李文才',5,0,'1993 1 21 12:20:28','重庆市大足区','135276',3)
select  *  from  stuInfo
go
--表外添加约束
create table studentInfo
(
	--自增从1开始,每次增加5
	stuId int  identity(1,5) not null,
	stuNo nvarchar(10) not null,
	stuNae nvarchar(32),
	stuSex char(1),
	stuBirthday datetime  not null,
	stuAddr nvarchar(128),
	stuMobile char(11),
	ClassNo int ,
	stuAge int,
	stuHigh float,
	stuFit float
)
--添加约束公式,
--alter table  表名 
--		add constraint  约束名  约束类型  约束内容
--添加主键约束		--约束名的命名格式约束的大写字母简写_表名_字段名
alter table studentInfo
		add constraint PK_studentInfo_stuId primary key(stuId)
--添加唯一约束,唯一约束括号内只能添加一个字段,添加多个字段则会出错
alter table studentInfo
		add  constraint UQ_stdentInfo_stuNo unique(stuNO)
		add  constraint UQ_stdentInfo_stuNae unique(stuNae)
--添加默认约束,有默认值的,不输入值必须用default 占位,只有为空的才能用'',或者用null占位
--数字不用‘’号
alter table studentInfo
		add constraint DF_studentInfo_stuBirthday default(getdate()) for stuBirthday
alter table studentInfo
		add constraint DF_studentInfo_stuAddr default '地址不详' for stuAddr
--添加check约束
alter table studentInfo
		add constraint CK_studentInfo_stuAge check(stuAge<80 and stuAge > 10)
--alter table studentInfo 
--		add constraint CK_studentInfo_stuHigh check(stuHigh<150 or stuHigh>120)--约束不生效
alter table studentInfo 
		add constraint CK_studentInfo_stuHigh check(stuHigh=150 or stuHigh=120)
--包含40和60 
alter table studentInfo 
		add constraint CK_studentInfo_stuFit check(stuFit between 40 and 60 )

go
insert into studentInfo values('N001','王美丽','1','1993-11-21','','13529756987',1,11,null,null)
insert into studentInfo values('N002','李文才','1','2001-1-21','重庆市江北区盘溪江山云著','13529756987',5,79,null,null)
insert into studentInfo values('N003','杨京俞','1','2001-1-21','重庆市江北区盘溪江山云著','13529756987',5,null,150,null)
insert into studentInfo values('N009','张秋丽','1', default,'重庆市江北区盘溪江山云著','13529756987',5,null,120,null)
insert into studentInfo values('N0011','张三' , '1',default,'重庆市江北区盘溪江山云著','13529756987',5,null,null,40)
insert into studentInfo values('N0012','李四 ', '1','1993-11-21',default,'13529756987',1,null,null,60)
--DROP TABLE studentInfo

select  *  from  studentInfo

create  table  ScoreInfo
(
	ScoureId int primary key identity(1,1),
	Chinese float ,
	Math    float,
	stuId int ,--外键
)
--添加外键约束,从表在前
alter table ScoreInfo
		add constraint FK_ScoreInfo_studentInfo_stuId foreign key(stuId) references studentInfo(stuId)
--删除约束constraint可省略
alter table studentInfo 
		drop constraint CK_studentInfo_stuFit 
--修改表数据
--添加列,若表中有数据则添加的列必须允许为空值或者该列有默认值,表中无数据则无限制
alter table ScoreInfo 
		add English float null,
	        Chemistry float null --同时添加两个字段用分号隔开
--删除列
alter table ScoreInfo
		drop column English,Chinese
--修改列,修改数据类型,注意有些数据类型不能相互转换,而且可能丢失精度
alter table ScoreInfo
		alter column Chemistry nvarchar(8)
--修改可空类型,不能省略数据类型
alter table ScoreInfo
		alter column Chemistry nvarchar(8) not null
--修改列名称,会用到系统函数,其后'column' 可以省略
exec sp_rename 'ScoreInfo.Chemistry','HuaXue','column'
go
--删除表,有外键先删从表再删主表,或者先删除外键
drop table ScoreInfo
--判断表是否存在,--判断数据库是否存在同理sys.databases
if exists (select * from sys.sysobjects where name='ScoreInfo')
		drop table ScoreInfo
go
--系统函数判断是否存在,语法
--if  object_id(对象名,对象类型),对象名可以是表名,约束名,存储过程名,视图名等,对象类型可以不写U表示用户自定义,V表示视图,C表示检查约束
if OBJECT_ID('ScoreInfo') is not null
		drop table ScoreInfo
go

		
select * from ScoreInfo

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值