SQL技術內幕二DDL

  • 建立數據庫

if db_id('DBTest') is null
create database DBTestsql

  • 建立表

use eb_fy_data_test---use 切換所在數據庫上下文數據庫

if object_id('UserTest','u') is not null server

drop TABLE UserTest索引

CREATE TABLE UserTest {}table

  • 數據完整性

1.主鍵約束test

alter table UserTestobject

add constraint PK_UserTest語法

Primary Key(ID)im

建立主鍵時sql server在幕後會自動建立一個惟一索引,保證惟一性。數據

添加惟一索引的語法(一種物理機制):

alter table UserTest

add constraint PK_UserTest

Unique(UserCNo)

2.外鍵約束

alter table UserTest

add constraint PK_UserTest

Foreign Key(OrderID)

references OrderInfo(OrderID)

也能夠添加外鍵約束與本表的其餘字段,但此時可容許null值

3.檢查約束

alter table UserTest

add constraint PK_UserTest

Check(age>18)

4.默認約束即默認值Default

相關文章
相關標籤/搜索