新建数据库
create database stu on
(name='s2',
filename='C:\temp\stu.mdf',
size=3MB,
filegrowth=5%
)
log on(
name='s2_log',
filename='C:\temp\stu.ldf',
size=3MB,
filegrowth=5%
)
删除数据库
创建学生表
create table stu_info(
sid int primary key not null identity(1,1),
name nvarchar(10) not null,
sex char(2) check(sex in('男','女')) not null,
age as datediff(year,birth,getdate()),
birth date not null,
addr nvarchar(50) default ('北京'),
email nvarchar(50) check(email like '%@%'),
tel nvarchar(30),
)
删除学生表
创建课程表和成绩表
create table course(
cid int primary key not null identity(1,1),
cname nvarchar(20) not null,
)
-- 创建成绩表
create table score(
sid int foreign key(sid) references stu_info(sid),
cid int foreign key(cid) references course(cid),
cj int not null check(cj between 0 and 100),
primary key(sid,cid),
)
处理数据
-- 修改表结构
-- 增加字段
alter table stu_info add mr bit
select * from stu_info
alter table stu_info add beizhu nvarchar(200);
-- 删除字段
alter table stu_info drop column beizhu;
-- 修改字段
alter table stu_info alter column beizhu nvarchar(50);
-- 增加数据
-- 全部字段插入
insert into stu_info values('w1','男','1988-8-8','上海','w1@onetest.com','13825689516',0);