SQL技术内幕二DDL

  • 创建数据库

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

  • 创建表

use eb_fy_data_test---use 切换所在数据库上下文

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

drop TABLE UserTest

CREATE TABLE UserTest {}

  • 数据完整性

1.主键约束

alter table UserTest

add constraint PK_UserTest

Primary Key(ID)

创建主键时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

 

posted @ 2015-07-19 17:41  CodingWang  阅读(175)  评论(0编辑  收藏  举报