黑马程序员---SQL基础之用T-SQL代码实现数据库、表等的创建


近几天学习了SQL的一些基础,现在总结一下通过T-SQL代码来实现数据库、表的创建。

创建&删除数据库

创建数据库

create database stuDB

on

(

  name='stuDB_data',

  filename='D:\database\stuDB_data.mdf',

  size=5mb,

  maxsize=100mb,

  filegrowth=15%

)

删除数据库

drop database stuDB

创建&删除数据表

创建数据表

create table stuInfo

(

  stuName varchar(20) not null,

  stuNO char(6) not null,

  stuAge int not null,

  stuID numeric(18,0),

  stuSeat smallint identity(1,1),

  stuAddress text

)

删除数据表

drop table stuInfo

创建&删除约束

常用的约束类型有五种:主键约束,唯一约束,检查约束,默认约束,外键约束。

为表中字段添加约束

添加主键约束

alter table stuInfo

add constraint PK_stuNO primary key (stuNO)

添加唯一约束

alter table stuInfo

add constraint UQ_stuID unique (stuID)

添加默认约束

alter table stuInfo

add constraint DF_stuAddress default ('地址不详') for stuAddress

添加检查约束

alter table stuInfo

add constraint CK_stuAge check (stuAge between 15 and 40)

添加外键约束

alter table stuMarks

add constraint FK_stuNO foreign key (stuNO) references stuInfo (stuNO)

删除约束

alter table stuInfo

drop constraint DF_stuAddress


posted @ 2012-02-25 22:37  wrzj5678  阅读(244)  评论(0编辑  收藏  举报