【转载】 SQL Server 创建表 添加主键 添加列常用SQL语句
--删除主键
alter table 表名 drop constraint 主键名--添加主键
alter table 表名 add constraint 主键名 primary key(字段名1,字段名2……)
--添加非聚集索引的主键
alter table 表名 add constraint 主键名 primary key NONCLUSTERED(字段名1,字段名2……)
*************************************************************
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)
********************************************************
新建表: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 ,
)
实例:
CREATE TABLE [dbo].[TB_MSTORAGE](
[ID] [numeric](10, 0) IDENTITY(1,1) NOT NULL,
[MCODE] [varchar](50) NULL,
[MNAME] [varchar](100) NULL,
[MATERIAL] [varchar](100) NOT NULL,
[MMODLE] [varchar](100) NOT NULL,
[MSTANDARD] [varchar](20) NOT NULL,
[MCOUNT] [numeric](10, 2) NOT NULL,
[MUNIT] [varchar](20) NOT NULL,
[PRICE] [numeric](10, 2) NOT NULL,
CONSTRAINT [PK_TB_MSTORAGE] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
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
插入数据:
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 ...