首页 > 数据库技术 > 详细

SQL技术内幕二DDL

时间:2015-07-19 19:24:53      阅读:222      评论:0      收藏:0      [点我收藏+]
  • 创建数据库

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

 

SQL技术内幕二DDL

原文:http://www.cnblogs.com/ykwang/p/4658953.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!