use master;
go
go
if exists(select * from sys.databases where name='stuDB')
drop database stuDB;
go
drop database stuDB;
go
exec sp_configure 'show advanced options',1 --显示高级选项
go
reconfigure
go
exec sp_configure 'xp_cmdshell',1
go
reconfigure
go
exec xp_cmdshell 'mkdir D:\stuDB'
go
go
reconfigure
go
exec sp_configure 'xp_cmdshell',1
go
reconfigure
go
exec xp_cmdshell 'mkdir D:\stuDB'
go
create database stuDB
on(
name='stuDB_data',
filename='D:\stuDB\stuDB.mdf',
size=5MB,
filegrowth=15%
)log on(
name='stuDB_log',
filename='D:\stuDB\stuDB.log'
)
go
on(
name='stuDB_data',
filename='D:\stuDB\stuDB.mdf',
size=5MB,
filegrowth=15%
)log on(
name='stuDB_log',
filename='D:\stuDB\stuDB.log'
)
go
use stuDB;
go
go
if exists(select * from sys.objects where name='stuInfo')
drop table stuInfo;
go
drop table stuInfo;
go
create table stuinfo(
stuName varchar(40) not null,
stuNo varchar(10) primary key
constraint CK_stuNo check(stuNo like 'S253__'),
stuSex varchar(2) not null default '男'
constraint CK_stuSex check(stuSex in('男','女')),
stuAge int
constraint CK_stuAge check(stuAge between 15 and 30),
stuSeat int identity(1,1), --要求座位号只能在1-30之间,超过30从1开始重新自增
stuAddress text default '地址不详'
);
go
stuName varchar(40) not null,
stuNo varchar(10) primary key
constraint CK_stuNo check(stuNo like 'S253__'),
stuSex varchar(2) not null default '男'
constraint CK_stuSex check(stuSex in('男','女')),
stuAge int
constraint CK_stuAge check(stuAge between 15 and 30),
stuSeat int identity(1,1), --要求座位号只能在1-30之间,超过30从1开始重新自增
stuAddress text default '地址不详'
);
go
--获取表中刚刚生成的自增量
select distinct(@@IDENTITY) from 表名;
--创建一个触发器,当座位号大于30则重置标识列
if exists(select * from sys.triggers where name='tr_stuinfo')
drop trigger tr_stuinfo;
go
create trigger tr_stuinfo on stuinfo
after insert
as
if (select ident_current('stuinfo'))>=30
begin
dbcc checkident(stuinfo,reseed,0);
end
go
after insert
as
if (select ident_current('stuinfo'))>=30
begin
dbcc checkident(stuinfo,reseed,0);
end
go
if exists(select * from sys.objects where name='stuMarks')
drop table stuMarks;
go
drop table stuMarks;
go
create table stuMarks(
examNo varchar(20) not null
constraint CK_examNo check(examNo like 'E200507____'),
stuNo varchar(10) not null
constraint FK_sNo foreign key references stuInfo(stuNo),
writtenExam Numeric(5,2) default 0
constraint CK_we check(writtenExam between 0 and 100),
labExam Numeric(5,2) default 0
constraint CK_le check(labExam between 0 and 100)
);
examNo varchar(20) not null
constraint CK_examNo check(examNo like 'E200507____'),
stuNo varchar(10) not null
constraint FK_sNo foreign key references stuInfo(stuNo),
writtenExam Numeric(5,2) default 0
constraint CK_we check(writtenExam between 0 and 100),
labExam Numeric(5,2) default 0
constraint CK_le check(labExam between 0 and 100)
);