现在很少用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,
)
修改字段类型
Alter table [表名] Alter column [列名] [列类型]
创建表时同时创建主键、外键:
create table grade
(id int ,
grade int
constraint id_fk foreign key (id) references student (id)
)
create Table Examination (e_id smallint primary key not null,l_language float, english float,computer float) --可以创建表时同时创建主键、外键
alter table examination add constraint FK_ID foreign key(student_id) references student(student_id) --当然也可以建立表后再建外键
alter table examination add student_id int not null --修改student_id 为not null
alter table examination alter column e_id nvarchar not null --也要修改为not null
alter table examination add constraint PK_Id primary key(e_id) --添加主键
alter table examination drop constraint PK_id --删除主键
alter table examination alter column student_id nvarchar(50) --修改字段数据类型
alter table examination add constraint FK_ID foreign key(student_id) references student(student_id) --添加外键约束
删除表:
Drop table [表名]
删除所有表:
DECLARE curItems CURSOR
FOR select [name] from sysobjects where xtype='U'
FOR READ ONLY
OPEN curItems
DECLARE @n NVARCHAR(100),@m NVARCHAR(100)
FETCH FROM curItems INTO @n
WHILE @@FETCH_STATUS=0
BEGIN
set @m=@n
exec('Drop Table ' + @m)
FETCH NEXT FROM curItems INTO
@n
END
CLOSE curItems
DEALLOCATE curItems
--删除主键
alter table 表名 drop constraint 主键名
--添加主键
alter table 表名 add constraint 主键名 primary key(字段名1,字段名2……)
--添加非聚集索引的主键
alter table 表名 add constraint 主键名 primary key NONCLUSTERED(字段名1,字段名2……)
插入数据:
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
修改表前缀:
ALTER SCHEMA dbo TRANSFER prename.tablename;
如果表2已经存在,把表1中的记录加到表2中的语句:
insert into 表2 (字段1,字段2,...) select 字段1,字段2,.. from 表2 where ...
如果表2不存在,则用下面的语句会自动生成表2,字段的类型和表1一样:
select 字段1,字段2,.. INTO 表2 from 表1 where ...