sql server建庫(kù)、建表、建約束技巧
下面給大家分享下sql server建庫(kù)、建表、建約束技巧,下文介紹有文字有代碼。
--創(chuàng)建School數(shù)據(jù)庫(kù)之前:首先判斷數(shù)據(jù)庫(kù)是否存在,若存在則刪除后再創(chuàng)建,若不存在則創(chuàng)建--
--exists關(guān)鍵字:括號(hào)里邊能查詢(xún)到數(shù)據(jù)則返回‘true' 否則返回‘false'
if exists(select * from sysdatabases where name = 'School') --exists返回‘true'則執(zhí)行刪除數(shù)據(jù)庫(kù)操作-- drop database School --exists返回‘false'則表明數(shù)據(jù)庫(kù)不存在,直接創(chuàng)建 create database School on primary ( --主數(shù)據(jù)庫(kù)文件-- name = 'School', --主數(shù)據(jù)文件邏輯名 fileName = 'D:\project\School.mdf', --主數(shù)據(jù)文件物理邏輯名 size = 5MB, --初始值大小 maxsize = 100MB, --最大大小 filegrowth = 15% --數(shù)據(jù)文件增長(zhǎng)量 ) log on ( --日志文件-- name = 'School_log', filename = 'D:\project\School_log.ldf', size = 2MB, filegrowth = 1MB ) go
----------------------------------------使用T-SQL創(chuàng)建employee數(shù)據(jù)庫(kù)------------------------------------
create database employee on 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 )
---------------------------------查詢(xún)已存在的數(shù)據(jù)庫(kù)信息---------------------------
select * from sysdatabases
---------------------------------刪除數(shù)據(jù)庫(kù)------------------------------------
drop database School
---------------------------------創(chuàng)建Student數(shù)據(jù)庫(kù)表----------------------------
--1、選擇操作的數(shù)據(jù)庫(kù)--
use School
go
--判斷表是否存在--
if exists(select * from sysobjects where name = 'Student')
drop table Student
--2、創(chuàng)建表---
create table Student ( --具體的列名 數(shù)據(jù)類(lèi)型 列的特征(是否為空)-- 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ù)庫(kù)對(duì)象(數(shù)據(jù)庫(kù)表)---
select * from sysobjects
drop table Student
----------------------創(chuàng)建subject課程表-------------------
-----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成績(jī)表-------------------
-----1、判斷表是否存在;若存在則刪除再創(chuàng)建,若不存在則直接創(chuàng)建--------
if exists(select * from sysobjects where name = 'Result')
drop table Result
use School
go
---創(chuàng)建Result成績(jī)表--
create table Result
(
StudentNo int not null,
SubjectNo int not null,
ExamDate Datetime not null,
StudentResult int not null
)
-----------------------------------------創(chuàng)建Grande年級(jí)表-------------------
-----1、判斷表是否存在;若存在則刪除再創(chuàng)建,若不存在則直接創(chuàng)建--------
if exists(select * from sysobjects where name = 'Grade')
drop table Grade
use School
go
---創(chuàng)建Grande年級(jí)表--
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添加默認(rèn)約束--
alter table Student
add constraint df_stuaddress default('地址不詳') for Address
---刪除地址address默認(rèn)約束---
alter table Student
drop constraint df_stuaddress
----------出生日期添加檢查約束--------
alter table Student
add constraint ck_stuBorndate check(Borndate > '1980-01-01')
---------與Grand(年級(jí)表)建立主外鍵關(guān)系--------
--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)
------給課程名稱(chēng)subjectName添加非空約束;-----
-----with nocheck:已經(jīng)存在數(shù)據(jù)不通過(guò)check約束-------
alter table subject with nocheck
add constraint ck_subName check(SubjectName is not null)
-----學(xué)時(shí)必須大于0-----
alter table subject with nocheck
add constraint ck_ClassHour check(ClassHour > 0)
-----與Grade年級(jí)表添加主外鍵約束----
alter table subject with nocheck
add constraint fk_GradeID foreign key(GradeID)
references Grade(GradeID)
----------給result成績(jī)表添加約束------------
-------添加多個(gè)約束---------
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è)約束--
alter table Result
drop constraint pk_No_subID_date,fk_subNo,fk_StuNo,ck_StudentResult,df_examdate
--------更改列的數(shù)據(jù)類(lèi)型----------
alter table Result
alter column StudentResult int
以上就是本文全部?jī)?nèi)容,希望大家喜歡。
- mysql建庫(kù)時(shí)提示Specified key was too long max key length is 1000 bytes的問(wèn)題的解決方法
- Mysql 建庫(kù)建表技巧分享
- SQL Server--怎樣用ADO在SQL SERVER中建庫(kù),建表
- 詳解在MySQL中創(chuàng)建表的教程
- mysql建表常用sql語(yǔ)句個(gè)人經(jīng)驗(yàn)分享
- Oracle新建用戶(hù)、角色,授權(quán),建表空間的sql語(yǔ)句
- SQL Server 2008 阻止保存要求重新創(chuàng)建表的更改問(wèn)題的設(shè)置方法
- 必須會(huì)的SQL語(yǔ)句(二) 創(chuàng)建表、修改表結(jié)構(gòu)、刪除表
- 一條SQL語(yǔ)句修改多表多字段的信息的具體實(shí)現(xiàn)
- 用SQL語(yǔ)句添加刪除修改字段、一些表與字段的基本操作、數(shù)據(jù)庫(kù)備份等
- 用sql命令修改數(shù)據(jù)表中的一個(gè)字段為非空(not null)的語(yǔ)句
- SqlServer編寫(xiě)數(shù)據(jù)庫(kù)表的操作方式(建庫(kù)、建表、修改語(yǔ)句)
相關(guān)文章
監(jiān)控 log文件大小的存儲(chǔ)過(guò)程
用來(lái)監(jiān)控 log文件大小的存儲(chǔ)過(guò)程,需要的朋友可以參考下。2010-07-07關(guān)于數(shù)據(jù)庫(kù)優(yōu)化問(wèn)題收集匯總
筆者在工作實(shí)踐中發(fā)現(xiàn),不良的SQL往往來(lái)自于不恰當(dāng)?shù)乃饕O(shè)計(jì)、不充份的連接條件和不可優(yōu)化的where子句。以下就對(duì)數(shù)據(jù)庫(kù)優(yōu)化問(wèn)題進(jìn)行了介紹,需要的朋友可以參考下2013-07-07分享:在存儲(chǔ)過(guò)程中使用另一個(gè)存儲(chǔ)過(guò)程返回的查詢(xún)結(jié)果集的方法
本篇文章介紹了,在存儲(chǔ)過(guò)程中使用另一個(gè)存儲(chǔ)過(guò)程返回的查詢(xún)結(jié)果集的方法。需要的朋友參考下2013-04-04SQL Server一個(gè)字符串拆分多行顯示或者多行數(shù)據(jù)合并成一個(gè)字符串
這篇文章介紹了SQL Server一個(gè)字符串拆分多行顯示或者多行數(shù)據(jù)合并成一個(gè)字符串的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05SQL?Server查詢(xún)結(jié)果導(dǎo)出到EXCEL表格的圖文教程
相信大家常常會(huì)遇到將SqlServer查詢(xún)結(jié)果導(dǎo)出到Excel的問(wèn)題,下面這篇文章主要給大家給大家介紹了SQL?Server查詢(xún)結(jié)果導(dǎo)出到EXCEL表格的相關(guān)資料,文中通過(guò)圖文介紹的非常詳細(xì),需要的朋友可以參考下2022-08-08當(dāng)恢復(fù)sqlserver bak文件時(shí),原始的用戶(hù)無(wú)法刪除的解決方法
當(dāng)你從現(xiàn)有的bak文件,恢復(fù)數(shù)據(jù)庫(kù)時(shí),如果數(shù)據(jù)庫(kù)本身帶有一個(gè)用戶(hù):比如用戶(hù)叫:DemoUser.2010-06-06