下面给大家分享下sql server建库、建表、建约束技巧,下文介绍有文字有代码。
--创建School数据库之前:首先判断数据库是否存在,若存在则删除后再创建,若不存在则创建--
--exists关键字:括号里边能查询到数据则返回‘true' 否则返回‘false'
if exists(select * from sysdatabases where name = 'School')
--exists返回‘true'则执行删除数据库操作--
drop database School
--exists返回‘false'则表明数据库不存在,直接创建
create database School
on primary
(
--主数据库文件--
name = 'School', --主数据文件逻辑名
fileName = 'D:\project\School.mdf', --主数据文件物理逻辑名
size = 5MB, --初始值大小
maxsize = 100MB, --最大大小
filegrowth = 15% --数据文件增长量
)
log on
(
--日志文件--
name = 'School_log',
filename = 'D:\project\School_log.ldf',
size = 2MB,
filegrowth = 1MB
)
go
----------------------------------------使用T-SQL创建employee数据库------------------------------------
create database employee
on primary
(
--主要数据文件--
name = 'employee1',
filename = 'D:\project\employee1.mdf',
size = 10MB,
filegrowth = 10%
),
(
--次要数据文件--
name = 'employee2',
filename = 'D:\project\employee2.ndf',
size = 20MB,
maxsize = 100MB,
filegrowth = 1MB
)
log on
(
--第一个日志文件--
name = 'employee_log1',
filename = 'D:\project\employee_log1.ldf',
size = 10MB,
filegrowth = 1MB
),
(
--第二个日志文件--
name = 'employee_log2',
filename = 'D:\project\employee_log2.ldf',
size = 10MB,
maxsize = 50MB,
filegrowth = 1MB
)
---------------------------------查询已存在的数据库信息---------------------------
select * from sysdatabases
---------------------------------删除数据库------------------------------------
drop database School
---------------------------------创建Student数据库表----------------------------
--1、选择操作的数据库--
use School
go
--判断表是否存在--
if exists(select * from sysobjects where name = 'Student')
drop table Student
--2、创建表---
create table Student
(
--具体的列名 数据类型 列的特征(是否为空)--
StudentNo int identity(2,1) not null,
LoginPwd nvarchar(20) not null,
StudentName nvarchar(20) not null,
Sex int not null,
GradeId int not null,
phone nvarchar(50) not null,
BornDate datetime not null,
Address nvarchar(255),
Email nvarchar(50),
IDENTITYcard varchar(18)
)
go
---查看所有数据库对象(数据库表)---
select * from sysobjects
drop table Student
----------------------创建subject课程表-------------------
-----1、判断表是否存在;若存在则删除再创建,若不存在则直接创建--------
if exists(select * from sysobjects where name = 'subject')
drop table subject
use School
go
---创建subject课程表--
create table subject
(
SubjectNo int not null identity(1,1),
SubjectName nvarchar(50),
ClassHour int,
GradeID int
)
----------------------------------------创建Result成绩表-------------------
-----1、判断表是否存在;若存在则删除再创建,若不存在则直接创建--------
if exists(select * from sysobjects where name = 'Result')
drop table Result
use School
go
---创建Result成绩表--
create table Result
(
StudentNo int not null,
SubjectNo int not null,
ExamDate Datetime not null,
StudentResult int not null
)
-----------------------------------------创建Grande年级表-------------------
-----1、判断表是否存在;若存在则删除再创建,若不存在则直接创建--------
if exists(select * from sysobjects where name = 'Grade')
drop table Grade
use School
go
---创建Grande年级表--
create table Grade
(
GradeId int not null,
GrandeName nvarchar(50)
)
-----------------------------------------T-SQL添加约束-------------------------
--给StudentNo添加主键约束---
alter table Student
add constraint pk_StuNo primary key(StudentNo)
--给身份证添加唯一约束--
alter table Student
add constraint uq_StuIdcard unique(IDENTITYcard)
---给地址address添加默认约束--
alter table Student
add constraint df_stuaddress default('地址不详') for Address
---删除地址address默认约束---
alter table Student
drop constraint df_stuaddress
----------出生日期添加检查约束--------
alter table Student
add constraint ck_stuBorndate check(Borndate > '1980-01-01')
---------与Grand(年级表)建立主外键关系--------
--1、添加Grade主键(操作Grade)---
alter table Grade
add constraint pk_graid primary key(GradeId)
--2、添加Grade外键(操作Student)--
alter table Student
add constraint fk_stuGradeID foreign key(GradeId) references Grade(GradeId)
-------------------给subject课程表添加约束-----------------------
----给subjectNo列添加主键约束------
alter table subject
add constraint pk_SubID primary key(SubjectNo)
------给课程名称subjectName添加非空约束;-----
-----with nocheck:已经存在数据不通过check约束-------
alter table subject with nocheck
add constraint ck_subName check(SubjectName is not null)
-----学时必须大于0-----
alter table subject with nocheck
add constraint ck_ClassHour check(ClassHour > 0)
-----与Grade年级表添加主外键约束----
alter table subject with nocheck
add constraint fk_GradeID foreign key(GradeID)
references Grade(GradeID)
----------给result成绩表添加约束------------
-------添加多个约束---------
alter table Result
add
constraint pk_No_subID_date primary key(StudentNo,SubjectNo,ExamDate),
constraint df_examdate default(getdate()) for ExamDate,
constraint ck_StudentResult check(StudentResult between 0 and 100),
constraint fk_StuNo foreign key(StudentNo) references Student(StudentNo),
constraint fk_subNo foreign key(SubjectNo) references Subject(SubjectNo)
--删除多个约束--
alter table Result
drop constraint pk_No_subID_date,fk_subNo,fk_StuNo,ck_StudentResult,df_examdate
--------更改列的数据类型----------
alter table Result
alter column StudentResult int
以上就是本文全部内容,希望大家喜欢。