--主键约束(Primary Key constraint):要求主键列的数据唯一,并且不允许为空。
--唯一约束(Unique Constraint):要求该列唯一,允许为空,但只能出现一个空值。
--检查约束(Check Constraint):某列取值范围限制、格式限制等,如有关年龄的约束。
--默认约束(Default Constraint):某列的默认值,如我们的男性同学较多,性别默认为男。
--外键约束(Foreign Key):用于在两表之间建立关系需要制定引用主表的哪一列。
语法如下
alter table 表名
add constraint 约束名 约束类型具体的约束说明
示例:
--添加主键约束
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 stuInfo
add constraint FK_stuNo foreign key(stuNo) references stuInfo(stuNo)
删除约束
alter table 表名 drop constraint 约束名
【转自】http://blog.sina.com.cn/s/blog_63a48d250100smu3.html
另外附:
主键:
添加列语法:ALTER TABLE 表名
Add 列名 类型 ...
添加主键语法:ALTER TABLE 表名
ADD CONSTRAINT 主键名 PRIMARY KEY(列名)
1.新建一数据表,里面有字段id,将id设为为主键
create table tb(id int,constraint pkid primary key (id)) create table tb(id int primary key )
2.新建一数据表,里面有字段id,将id设为主键且自动编号
create table tb(id int identity(1,1),constraint pkid primary key (id)) create table tb(id int identity(1,1) primary key )
3.已经建好一数据表,里面有字段id,将id设为主键
alter table tb alter column id int not null alter table tb add constraint pkid primary key (id)
4.删除主键
Declare @Pk varChar(100); Select @Pk=Name from sysobjects where Parent_Obj=OBJECT_ID(\'tb\') and xtype=\'PK\'; if @Pk is not null exec(\'Alter table tb Drop \'+ @Pk)
5.也可以添加列时直接指定是主键
alter table tb add id int not null identity(1,1) primary key (id)
再附: 常用sql语句
新建表:
create table [表名]
(
[自动编号字段] int IDENTITY (1,1) PRIMARY KEY ,
[字段1] nVarChar(50) default \\'默认值\\' null ,
[字段2] ntext null ,
[字段3] datetime,
[字段4] money null ,
[字段5] int default 0,
[字段6] Decimal (12,4) default 0,
[字段7] image null ,
)
删除表:
Drop table [表名]
插入数据:
INSERT INTO [表名] (字段1,字段2) VALUES (100,\\'51WINDOWS.NET\\')
删除数据:
DELETE FROM [表名] WHERE [字段名]>100
更新数据:
UPDATE [表名] SET [字段1] = 200,[字段2] = \\'51WINDOWS.NET\\' WHERE [字段三] = \\'HAIWA\\'
新增字段:
ALTER TABLE [表名] ADD [字段名] NVARCHAR (50) NULL
删除字段:
ALTER TABLE [表名] DROP COLUMN [字段名]
修改字段:
ALTER TABLE [表名] ALTER COLUMN [字段名] NVARCHAR (50) NULL
重命名表:(Access 重命名表,请参考文章:在Access数据库中重命名表)
sp_rename \\'表名\\', \\'新表名\\', \\'OBJECT\\'
新建约束:
ALTER TABLE [表名] ADD CONSTRAINT 约束名 CHECK ([约束字段] <= \\'2000-1-1\\')
删除约束:
ALTER TABLE [表名] DROP CONSTRAINT 约束名
新建默认值
ALTER TABLE [表名] ADD CONSTRAINT 默认值名 DEFAULT \\'51WINDOWS.NET\\' FOR [字段名]
删除默认值
ALTER TABLE [表名] DROP CONSTRAINT 默认值名
删除Sql Server 中的日志,减小数据库文件大小
dump transaction 数据库名 with no_log
backup log 数据库名 with no_log
dbcc shrinkdatabase(数据库名)
exec sp_dboption \\'数据库名\\', \\'autoshrink\\', \\'true\\'
\\\\'添加字段通用函数
Sub AddColumn(TableName,ColumnName,ColumnType)
Conn.Execute(\"Alter Table \"&TableName&\" Add \"&ColumnName&\" \"&ColumnType&\"\")
End Sub
\\\\'更改字段通用函数
Sub ModColumn(TableName,ColumnName,ColumnType)
Conn.Execute(\"Alter Table \"&TableName&\" Alter Column \"&ColumnName&\" \"&ColumnType&\"\")
End Sub
\\\\'检查表是否存在
sql=\"select count(*) as dida from sysobjects where id = object_id(N\\'[所有者].[表名]\\') and OBJECTPROPERTY(id, N\\'IsUserTable\\') = 1\"
set rs=conn.execute(sql)
response.write rs(\"dida\")\\'返回一个数值,0代表没有,1代表存在
判断表的存在:
select * from sysobjects where id = object_id(N\\'[dbo].[tablename]\\') and OBJECTPROPERTY(id, N\\'IsUserTable\\') = 1
某个表的结构
select * from syscolumns where id = object_id(N\\'[dbo].[你的表名]\\') and OBJECTPROPERTY(id, N\\'IsUserTable\\') = 1