国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > SQL Server > 正文

sql server建庫、建表、建約束技巧

2020-07-25 12:52:39
字體:
供稿:網(wǎng)友

下面給大家分享下sql server建庫、建表、建約束技巧,下文介紹有文字有代碼。

--創(chuàng)建School數(shù)據(jù)庫之前:首先判斷數(shù)據(jù)庫是否存在,若存在則刪除后再創(chuàng)建,若不存在則創(chuàng)建--
--exists關(guān)鍵字:括號里邊能查詢到數(shù)據(jù)則返回‘true' 否則返回‘false'

if exists(select * from sysdatabases where name = 'School')--exists返回‘true'則執(zhí)行刪除數(shù)據(jù)庫操作--drop database School--exists返回‘false'則表明數(shù)據(jù)庫不存在,直接創(chuàng)建 create database Schoolon primary(--主數(shù)據(jù)庫文件--name = 'School', --主數(shù)據(jù)文件邏輯名fileName = 'D:/project/School.mdf', --主數(shù)據(jù)文件物理邏輯名size = 5MB, --初始值大小maxsize = 100MB, --最大大小filegrowth = 15% --數(shù)據(jù)文件增長量)log on(--日志文件--name = 'School_log',filename = 'D:/project/School_log.ldf',size = 2MB,filegrowth = 1MB)go

----------------------------------------使用T-SQL創(chuàng)建employee數(shù)據(jù)庫------------------------------------

create database employeeon primary(--主要數(shù)據(jù)文件--name = 'employee1',filename = 'D:/project/employee1.mdf',size = 10MB,filegrowth = 10%),(--次要數(shù)據(jù)文件--name = 'employee2',filename = 'D:/project/employee2.ndf',size = 20MB,maxsize = 100MB,filegrowth = 1MB)log on(--第一個(gè)日志文件--name = 'employee_log1',filename = 'D:/project/employee_log1.ldf',size = 10MB,filegrowth = 1MB),(--第二個(gè)日志文件--name = 'employee_log2',filename = 'D:/project/employee_log2.ldf',size = 10MB,maxsize = 50MB,filegrowth = 1MB)

---------------------------------查詢已存在的數(shù)據(jù)庫信息---------------------------

select * from sysdatabases

---------------------------------刪除數(shù)據(jù)庫------------------------------------

復(fù)制代碼 代碼如下:

drop database School

---------------------------------創(chuàng)建Student數(shù)據(jù)庫表----------------------------

復(fù)制代碼 代碼如下:

--1、選擇操作的數(shù)據(jù)庫--
use School
go

--判斷表是否存在--

復(fù)制代碼 代碼如下:

if exists(select * from sysobjects where name = 'Student')
drop table Student

--2、創(chuàng)建表---

create table Student(--具體的列名 數(shù)據(jù)類型 列的特征(是否為空)--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

---查看所有數(shù)據(jù)庫對象(數(shù)據(jù)庫表)---

復(fù)制代碼 代碼如下:

select * from sysobjects
drop table Student

----------------------創(chuàng)建subject課程表-------------------

復(fù)制代碼 代碼如下:

-----1、判斷表是否存在;若存在則刪除再創(chuàng)建,若不存在則直接創(chuàng)建--------
if exists(select * from sysobjects where name = 'subject')
drop table subject

use School
go

---創(chuàng)建subject課程表--
create table subject
(
SubjectNo int not null identity(1,1),
SubjectName nvarchar(50),
ClassHour int,
GradeID int
)

----------------------------------------創(chuàng)建Result成績表-------------------

復(fù)制代碼 代碼如下:

-----1、判斷表是否存在;若存在則刪除再創(chuàng)建,若不存在則直接創(chuàng)建--------
if exists(select * from sysobjects where name = 'Result')
drop table Result
use School
go

---創(chuàng)建Result成績表--

復(fù)制代碼 代碼如下:

create table Result
(
StudentNo int not null,
SubjectNo int not null,
ExamDate Datetime not null,
StudentResult int not null
)

-----------------------------------------創(chuàng)建Grande年級表-------------------

復(fù)制代碼 代碼如下:

-----1、判斷表是否存在;若存在則刪除再創(chuàng)建,若不存在則直接創(chuàng)建--------
if exists(select * from sysobjects where name = 'Grade')
drop table Grade
use School
go

---創(chuàng)建Grande年級表--

復(fù)制代碼 代碼如下:

create table Grade
(
GradeId int not null,
GrandeName nvarchar(50)
)

-----------------------------------------T-SQL添加約束-------------------------

復(fù)制代碼 代碼如下:

--給StudentNo添加主鍵約束---
alter table Student
add constraint pk_StuNo primary key(StudentNo)

--給身份證添加唯一約束--

復(fù)制代碼 代碼如下:

alter table Student
add constraint uq_StuIdcard unique(IDENTITYcard)

---給地址address添加默認(rèn)約束--

復(fù)制代碼 代碼如下:

alter table Student
add constraint df_stuaddress default('地址不詳') for Address

---刪除地址address默認(rèn)約束---

復(fù)制代碼 代碼如下:

alter table Student
drop constraint df_stuaddress


----------出生日期添加檢查約束--------

復(fù)制代碼 代碼如下:

alter table Student
add constraint ck_stuBorndate check(Borndate > '1980-01-01')

---------與Grand(年級表)建立主外鍵關(guān)系--------

--1、添加Grade主鍵(操作Grade)---

復(fù)制代碼 代碼如下:

alter table Grade
add constraint pk_graid primary key(GradeId)

--2、添加Grade外鍵(操作Student)--

復(fù)制代碼 代碼如下:

alter table Student
add constraint fk_stuGradeID foreign key(GradeId) references Grade(GradeId)

-------------------給subject課程表添加約束-----------------------

復(fù)制代碼 代碼如下:

----給subjectNo列添加主鍵約束------
alter table subject
add constraint pk_SubID primary key(SubjectNo)

------給課程名稱subjectName添加非空約束;-----

復(fù)制代碼 代碼如下:

-----with nocheck:已經(jīng)存在數(shù)據(jù)不通過check約束-------
alter table subject with nocheck
add constraint ck_subName check(SubjectName is not null)

-----學(xué)時(shí)必須大于0-----

復(fù)制代碼 代碼如下:

alter table subject with nocheck
add constraint ck_ClassHour check(ClassHour > 0)

-----與Grade年級表添加主外鍵約束----

復(fù)制代碼 代碼如下:

alter table subject with nocheck
add constraint fk_GradeID foreign key(GradeID)
references Grade(GradeID)


----------給result成績表添加約束------------

-------添加多個(gè)約束---------

復(fù)制代碼 代碼如下:

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)

--刪除多個(gè)約束--

復(fù)制代碼 代碼如下:

alter table Result
drop constraint pk_No_subID_date,fk_subNo,fk_StuNo,ck_StudentResult,df_examdate

--------更改列的數(shù)據(jù)類型----------

復(fù)制代碼 代碼如下:

alter table Result
alter column StudentResult int

以上就是本文全部內(nèi)容,希望大家喜歡。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 正阳县| 田林县| 锦州市| 灵台县| 长武县| 鄂州市| 库车县| 乌什县| 巴楚县| 河源市| 容城县| 武安市| 西充县| 苏尼特左旗| 华容县| 湖口县| 扎赉特旗| 加查县| 当涂县| 水富县| 溧水县| 仁化县| 西吉县| 泌阳县| 柳河县| 临安市| 定州市| 乐东| 西安市| 德昌县| 翁牛特旗| 杭锦后旗| 景洪市| 闻喜县| 社旗县| 南开区| 环江| 宁南县| 齐河县| 西华县| 周至县|