久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

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

157次閱讀
沒有評論

共計 4916 個字符,預計需要花費 13 分鐘才能閱讀完成。

這篇文章主要介紹“sql server 的建庫、建表、建約束技巧”,在日常操作中,相信很多人在 sql server 的建庫、建表、建約束技巧問題上存在疑惑,丸趣 TV 小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”sql server 的建庫、建表、建約束技巧”的疑惑有所幫助!接下來,請跟著丸趣 TV 小編一起來學習吧!

– 創建 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 sysobjectsdrop table Student

———————- 創建 subject 課程表 ——————- 復制代碼 代碼如下:
—–1、判斷表是否存在;若存在則刪除再創建,若不存在則直接創建 ——–if exists(select * from sysobjects where name = subject)drop table subjectuse 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 Resultuse 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 Gradeuse 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

到此,關于“sql server 的建庫、建表、建約束技巧”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注丸趣 TV 網站,丸趣 TV 小編會繼續努力為大家帶來更多實用的文章!

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-08-01發表,共計4916字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 武安市| 甘南县| 资中县| 顺义区| 平远县| 顺平县| 崇明县| 扎兰屯市| 隆安县| 茂名市| 庆安县| 诏安县| 定州市| 温泉县| 揭阳市| 平阳县| 古浪县| 万宁市| 黑山县| 托克逊县| 辉县市| 嘉峪关市| 雷波县| 高唐县| 礼泉县| 会理县| 屏东市| 东阳市| 仙游县| 南部县| 普洱| 顺义区| 鄂托克旗| 开化县| 大姚县| 南江县| 盐津县| 六枝特区| 丹东市| 志丹县| 海原县|