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

首頁 > 數據庫 > SQL Server > 正文

SQL Server建庫-建表-建約束

2024-08-31 00:54:11
字體:
來源:轉載
供稿:網友
SQL Server建庫-建表-建約束

----------------------------------------SQL Server建庫-建表-建約束創建School數據庫--------------------------------------

--創建School數據庫之前:首先判斷數據庫是否存在,若存在則刪除后再創建,若不存在則創建----exists關鍵字:括號里邊能查詢到數據則返回‘true’ 否則返回‘false’if exists(select * from sysdatabases where name = 'School')--exists返回‘true’則執行刪除數據庫操作--drop database School--exists返回‘false’則表明數據庫不存在,直接創建create database Schoolon 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 employeeon 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 Schoolgo

--判斷表是否存在--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 Schoolgo

---創建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 Schoolgo

---創建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 Schoolgo

---創建Grande年級表--create table Grade(GradeId int not null,GrandeName nvarchar(50))

-----------------------------------------T-SQL添加約束---------------------------給StudentNo添加主鍵約束---alter table Studentadd constraint pk_StuNo primary key(StudentNo)

--給身份證添加唯一約束--alter table Studentadd constraint uq_StuIdcard unique(IDENTITYcard)

---給地址address添加默認約束--alter table Studentadd constraint df_stuaddress default('地址不詳') for Address

---刪除地址address默認約束---alter table Studentdrop constraint df_stuaddress

----------出生日期添加檢查約束--------alter table Studentadd constraint ck_stuBorndate check(Borndate > '1980-01-01')

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

--1、添加Grade主鍵(操作Grade)---alter table Gradeadd constraint pk_graid primary key(GradeId)

--2、添加Grade外鍵(操作Student)--alter table Studentadd constraint fk_stuGradeID foreign key(GradeId) references Grade(GradeId)

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

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

------給課程名稱subjectName添加非空約束;----------with nocheck:已經存在數據不通過check約束-------alter table subject with nocheckadd constraint ck_subName check(SubjectName is not null)

-----學時必須大于0-----alter table subject with nocheckadd constraint ck_ClassHour check(ClassHour > 0)

-----與Grade年級表添加主外鍵約束----alter table subject with nocheckadd constraint fk_GradeID foreign key(GradeID)references Grade(GradeID)

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

-------添加多個約束---------alter table Resultadd 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 Resultdrop constraint pk_No_subID_date,fk_subNo,fk_StuNo,ck_StudentResult,df_examdate

--------更改列的數據類型----------alter table Resultalter column StudentResult int


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 康保县| 丰镇市| 正镶白旗| 武威市| 榆中县| 海兴县| 尚义县| 南陵县| 陈巴尔虎旗| 绥宁县| 浪卡子县| 仙游县| 武汉市| 正镶白旗| 慈溪市| 鄄城县| 昌图县| 海伦市| 宁夏| 塔河县| 宜阳县| 张家港市| 探索| 灵川县| 双桥区| 喀什市| 伊宁市| 麻阳| 青田县| 文登市| 溧水县| 玛多县| 玉田县| 巴林右旗| 夏河县| 聂荣县| 本溪市| 旬阳县| 邹平县| 濮阳县| 泗水县|