数据库表操作总结

来源:互联网 发布:本机mac地址查询 编辑:程序博客网 时间:2024/06/04 18:13

 修改已建好的数据表
(1) 修改列的数据类型
   alter    table   student_inf   alter   column    student_name   nvarchar(10)  null
注:alter:修改 student_inf:表名 column:列 student_name:列名 nvarchar(10):数据类型 null:允许为空
(2) 删除一列内容:
   alter   table   student_inf    drop   column   student_sex
注:drop:删除 student_sex:所要删除的列名
(3) 增加一列内容:
   alter   table   student_inf   add   student_sex  nvarchar(1)      null
注:student_sex:列名 null:增加列名允许为空
七、 为了保证数据的完整性,需要添加的约束
(1) 实体完整性:
1) 主键:primary  key
在创建时加主键约束:
create  table  student_mark
(
student_id     int    not   null   primary  key,
computer_mark     nvarchar(15)    not   null,
math_mark     nvarchar(1)     not    null,
Chinese_mark    int    not   null  
)
注:student_mark:表名        computer_mark:计算机成绩
math_mark:数学成绩      Chinese_mark:语文成绩
在修改表时添加主键约束:
alter   table    student_inf   add    constraint   pk   primary   key  (student_id) 
注:add:加 constraint:约束
     pk:自己起的约束名称,方便于对约束进行删除
在修改表时删除主键约束:
alter   table    student_inf   drop   constraint   pk
2) 唯一约束:unique
在创建时加唯一约束:
create  table  student_mark
(
student_id     int    not   null   unique,
computer_mark     nvarchar(15)    not   null,
math_mark     nvarchar(1)     not    null,
Chinese_mark    int    not   null  
)

在修改表时添加唯一约束:
alter   table    student_inf   add    constraint   un   unique  (student_id)

在修改表时删除唯一约束:
alter   table    student_inf   drop   constraint   un

3) 标识列:identity(标识种子,标识增量)—从标识种子开始,每加一条记录就自增1,需在创建时加入,并可直接将此列定义为主键列。
create  table  student_mark
(
student_id     int    identity(1,1)  primary  key,
computer_mark     nvarchar(15)    not   null,
math_mark     nvarchar(1)     not    null,
Chinese_mark    int    not   null  
)
(2) 引用完整性
外键:foreign   key
在创建时加入外键:
create  table  student_mark
(
student_id     int    not   null   foreign  key    references    student_inf(student_id),
computer_mark     nvarchar(15)    not   null,
math_mark     nvarchar(1)     not    null,
Chinese_mark    int    not   null  
)
注:references:关系 student_inf:主键表 student_id:主键  列
在修改表时加入外键:
alter   table    student_mark   add    constraint   fk   foreign  key  (student_id)  references    student_inf(student_id)

在修改表时删除外键约束:
alter   table    student_mark   drop   constraint   fk
(3) 域完整性
1) default约束:当列值为空时,用default约束后面的值来代替空值

在建表时同时创建:
create  table  student_mark
(
student_id     int    not   null,  
computer_mark     nvarchar(15)    null      default     ‘unknow’,
math_mark     nvarchar(1)     not    null,
Chinese_mark    int    not   null  
)
注:unknow:不知道

在修改表时加入default约束:
alter   table    student_mark   add    constraint   de   default    ‘unknow’    for    computer_mark    


在修改表时删除default约束:
alter   table    student_mark   drop   constraint   de
2) check约束:用条件来约束本列数据
在建表时同时创建:
create  table  student_inf
(
student_id     int    not   null,
student_age    int    not   null       check(student_age>15  and  student_age <100  ) , 
student_name     nvarchar(15)    not   null,
student_sex     nvarchar(1)     not    null

在修改表时加入check约束:
alter   table    student_inf   add    constraint   ch   check(student_age>15  and  student_age <100  )
注:连接两个条用:and:并且     or:或

在修改表时删除default约束:
alter   table    student_mark   drop   constraint   ch
八、 删除数据库表
drop    table    student_inf

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/sunxing007/archive/2009/07/29/4390929.aspx

原创粉丝点击