检查数据库是否存在:
if db_id('数据库名')is not null--ASCII码,一个字符占一个字节,if db_id('N数据库名')is not null--Unicode编码,一个字符占两个字节,宽字符编码格式
drop database 数据库名
go
修改数据库:
alter database 数据库名
add/modify/remove(添加,修改,移除) file
(
)
go
检查表是否存在:
if exists(select * from sysobjects where name='表名')
drop table 表名
go
给表添加约束:
create table 表名
(
id int not null primary key,
sex nchar(2) not null check(sex='男' or sex='女'),
address varchar(100) default('福州市')
)
go
若在创建表时忘了添加约束,这时候就可以通过这种方式
alter table 表名
add constraint ck_sex check(sex='男' or sex='女')
go
alter table 表名
add constraint df_address default('福州市')for address
go
删除已有的约束:
alter table 表名
drop constraint df_address
go
alter table 表名
add constraint pk_test primary key(字段名1,字段名2...)