创建数据库笔记(一)

use master
go
--创建数据库
if exists(select * from sysdatabases where name='DemoDB')
drop database DemoDB
go
create database DemoDB
on primary
(
name='DemoDB_data',
filename='C:\\DB\DemoDB_data.mdf',
size=10MB,
filegrowth=2MB
)
log on
(
    name='DemoDB_log',
filename='C:\\DB\DemoDB_log.ldf',
size=5MB,
filegrowth=2MB
)
go
--创建需要的各种数据表
use DemoDB
go
if exists(select * from sysobjects where name='Sutudents')
drop table Students
go
create table Students
(
StudentId int identity(100000,1),--学好,自动增长列
StudentName varchar(20) not null,
Gender char(2) not null,
Birthday datetime not null,
StudentIdNo numeric(18,0) not null,--身份证号
Age int not null,
PhoneNumber varchar(50),
StudentAddress varchar(500),
ClassId int not null  --班级编号(外键)
)
go
--创建班级表
if exists(select * from sysobjects where name='StudentClass')
drop table StudentClass
go
create table StudentClass
(
ClassId int primary  key,
ClassName varchar(20) not null
)
go
--创建成绩表
if exists(select * from sysobjects where name='ScoreList')
drop table ScoreList
go
create table ScoreList
(
Id int identity(1,1) primary key,
StudentId int not null,
CSharp int null,
SQLServerDB int null,
UpdateTime datetime not null
)
go
--创建管理员表
if exists(select * from sysobjects where name='Admins')
drop table Admins
go
create table Admins
(
LoginId int identity(1000,1) primary key,
LoginPwd varchar(20) not null,
AdminName varchar(20) not null
)
go
--创建主键约束
use DemoDB
go
if exists(select * from sysobjects where name='pk_StudentId')
alter table Students drop constraint pk_StudentId
alter table Students add constraint pk_StudentId primary key(StudentId)


--创建唯一约束
alter table Students add constraint uq_StudentIdNo unique (StudentIdNo)


--创建检查约束
alter table Students add constraint ck_Age check (Age between 18 and 25)


--创建默认约束
alter table Students add constraint df_StudentAddress default('地址不详') for  StudentAddress


--创建外键约束
alter table Students add constraint fk_ClassId foreign key(classId) references StudentClass(ClassId)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值