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

相关文章
相关标签/搜索