-------创建数据库----------
create database ceshi
on primary(
name = 'ceshi',
filename = 'F:数据库ceshi.mdf',
size = 5mb,
filegrowth = 1mb
)
log on(
name = 'ceshi_log',
filename = 'F:数据库ceshi_log.ldf',
size = 1mb,
filegrowth = 10%
)
-------创建表--------------
use ceshi;
create table ClassInfo(
cId int not null primary key identity(1,1),
cTitle nvarchar(10)
)
create table StudentInfo(
[sId] int not null primary key identity(1,1),
sName nvarchar(10) not null,
sGender char(6),
sBirthday datetime,
sPhone char(11),
sEmail varchar(20),
cid int not null,
foreign key(cid) references ClassInfo(cid)
)
-------查看表--------------
select * from ClassInfo
select * from StudentInfo
--------添加约束-------------
------手动删除一列------
alter table StudentInfo
drop column QQ
------手动添加一列-----
alter table StudentInfo
add sPhone char(11)
------手动修改列的数据类型----
alter table StudentInfo
alter column sPhone char(12)
------为ClassInfo添加一个主键约束---------
alter table ClassInfo
add constraint PK_cid primary key(cId)
------为StudentInfo添加外键约束---------
alter table StudentInfo
add constraint FK_sCid foreign key(cid) references ClassInfo(cid)
------非空约束,为sGender增加一个非空约束---
alter table StudentInfo
alter column sGender char(6) not null
------为sName增加一个唯一约束--------
alter table StudentInfo
add constraint UQ_sName unique(sName)
------为性别增加一个默认约束,默认为'男'---
alter table StudentInfo
add constraint DF_sGender default('男') for sGender
------为年龄增加一个检查约束:年龄必须在0-120岁之间
alter table StudentInfo
add constraint CK_sAge check(sAge >= 0 and sAge <= 120)
------删除约束--------
alter table StudentInfo
drop constraint [FK__StudentInfo__cid__1273C1CD]
-------对数据增删改查------
-----增-------
insert into StudentInfo
values('张三','男',1996-2-1,'2312@qq.com',1,'13223455432')
-----查-------
select * from StudentInfo
-----删-------
delete from StudentInfo where sName = '张三'
truncate table StudentInfo ---删除表内容,并释放空间
drop table StudentInfo ---删除表内容和结构
-----改-------
update StudentInfo set sName = '李四' where sId = 4