1. 程式人生 > >1.創建表

1.創建表

lld 默認 primer lec getdate str 客戶 隨機生成 不能

創建表:
1.主鍵(primary key)
2.唯一鍵(unique)
3.檢查約束(check)
4.默認值(default)
5.外鍵(foreign)
6.空值檢查(NULL)

在 SQL Server 2008 中,每個數據庫最多可包含 20 億個表。一個表中最多可以包含 30,000 個列,其中最多有 1024 個非稀疏列和計算列。表的行數及總大小僅受可用存儲空間的限制。每行最多包括 8,060 個字節。對於帶 varchar、nvarchar、varbinary 或 sql_variant 列(導致已定義表的總寬超過 8,060 字節)的表,此限制將放寬。其中每列的長度仍必須在 8,000 字節的限制內,但是它們的總寬可能超過表的 8,060 字節的限制。有關詳細信息,請參閱行溢出數據超過 8 KB。

每個表最多可以有 999 個非聚集索引和 1 個聚集索引。其中包括為支持表中所定義的 PRIMARY KEY 和 UNIQUE 約束而生成的索引。


重復怎麽辦?
年齡寫成負數怎麽辦?
手機號碼少寫一位怎麽辦?
郵箱地址不合法怎麽辦?


一.主鍵:--確保數據唯一
用來唯一確定表中記錄的標識符,其值不能為空,也不能重復。
一個表只能有一個主鍵,並且自動建立聚焦索引。
格式:constraint pk_sno primary key clustered(sno)

第一種:--不推薦使用第一種,因為主鍵名會隨機生成,將來影響操作。
create table Customer
(
cno int primary key
cname char(10)
)
第二種:
create table Customer
(
cno int constraint pk01_cno primary key clustered(cno)
cname char(10)
)
第三種方法:
create table Customer
(
cno int
cname char(10)
constraint pk02_cno primary key clustered(cno)
)
添加字段:
alter table Customer
add cmobil char(11)

校驗:
inset into

復合主鍵:

格式:constraint pk_sno primary key clustered(n1,n2)
比如借書表
管理主鍵:
1.查看主鍵信息:
select * from sys.objects where type=‘PK‘
2.為已存在的表添加主鍵:
Alter Table Customer constraint Pk_cno primery key clustered(cno)
3.刪除主鍵:
Alter Table Customer Drop constraint PK_cno

唯一鍵:unique--確保數據唯一
1.用於指定一個字段或者多個字段組合值具有唯一性,以防止字段中有重復的值;
2.和primary key區別:
主鍵每個表只能有一個,而unique最多249個;
主鍵的值不能為空,而unique可以有一行為空;
主鍵默認情況下建立聚集索引,而unique默認建立非聚集索引;

格式:
constraint unique_name unique NONclustered(name)

如果有多個非聚焦索引,可以多次使用以上格式.
查看唯一鍵信息,格式如下:
select * from sys.objects where type=‘UQ‘
為已存在的表添加唯一鍵:
alter table Customer Add Constraint UNIQUE_Mobile UNIQUE NONClustered(CMobileNo)

刪除唯一鍵:
alter table Customer Drop Constraint UNIQUE_Mobile

檢查約束(Check):
檢查約束是對相應字段輸入值設置檢查條件,以限制輸入值滿足某些條件.比如:性別只能填寫男女,年齡必須大於0,手機號必須要11位,郵箱必須滿足格式等.
格式:
Constraint Check_sex Check(sex in(‘男‘,‘女‘))
Constraint Check_age Check(cage>0)
Constraint Check_mobile Check(cmobile like ‘[1][358][0-9]‘)
Constraint Check_email Check(email like ‘%_[@]%_[.]%_‘)

查看Check約束信息:
select * from sys.objects where type=‘C‘
為已存在的表添加Check:
Alter Table Customer Add constraint Check_sex Check(sex in(‘男‘,‘女‘))
刪除Check約束:
Alter Table Customer Drop constraint Check_sex


默認值:(default)
默認值是在插入操作中如果沒有指定輸入值的時候,系統自動指定的值.
1.一個字段只能指定一個默認值;
2.如果定義的默認值的長度超過了其對應字段允許的長度,系統將自動截取;
3.不能對數據類型為timestamp的列或具有IDENTITY屬性的列創建default定義.
格式:
constraint DF_name default ‘marry‘ for Cname
如果寫到字段後面,則不需要寫for.


外鍵(foreign key):
1.如果在foreign key約束的列中輸入非NULL值,則此值必須在引用列中存在;否則將返回違反外鍵約束的錯誤信息;
2.foreign key約束僅能引用位於同一服務器上的同一數據庫中的表;跨數據庫的引用完整性必須通過觸發器實現;
3.foreign key約束可引用同一表中的其他列,此行稱為自引用;
4.列級foreign key約束的References子句只能列出一個引用例;此列的數據類型必須與定義約束的列的數據類型相同;
5.foreign key約束只能引用所引用的表的Primary Key或Unique約束中的列或所引用的表上Unique Index中的列;
6.一個表中最多有32個外鍵,臨時表不能有外鍵.
格式:
constraint FK_Cno Foreign Key(Cno) References Customer(Cno)
案例:
A.創建客戶表(customer):
create table customer
(
cno int constraint pk_cno primary key clustered(cno),
cname char(10),
cage tinyint constraint check_cage check(cage>0),
csex char(2) ,
cmobile char(11),
cemail varchar(50)
)
B.創建商品表(product):
create table product
(
productID int constraint pk_pdID primary key clustered(productID),
productName varchar(50),
Manufacturer char(40),
price float
)
C.創建購物明細表(detail):
create table detail
(
cno int constraint fk_cno foreign key(Cno) references customer(cno),
productID int constraint fk_prodid foreign key(productID) references customer(productID),
purDate smallDateTime constraint df_date default getdate(),
constraint pk_detail primary key clustered(cno,productID)
)
插入數據校驗:
insert into customer() values()
管理外鍵:
1.查詢外鍵:
select * from sys.objects where type=‘FK‘
2.添加外鍵:
Alter Table Customer Add constraint fk_cno foreign key(cno) references customer(cno)
3.刪除外鍵:
Alter Table Customer Drop constraint fk_cno

空值(NULL):檢查某個字段是否為空。
註意:
A.在一個表調用聚合函數的時候,比如:avg,count,只要某個字段為空,調用函數的時間,此條記錄會被忽略;
B.變量沒有賦值,默認就是為空,空值和任何數據運算都是空值。例如:
declare @i int
select @i+100
結果顯示為NULL。

格式:直接加 not null

1.創建表