SQL Server sql 操作

时间:2021-03-02 20:26:02

1、重命名表
将表OLD重命名为NEW:

EXEC sp_rename 'OLD','NEW'

2、重命名列
将表table1中的列old重命名为new:

EXEC sp_rename 'table1.old','new','COLUMN'

3、删除主键:

Declare @Pk varChar(100);
Select @Pk=Name from sysobjects where Parent_Obj=OBJECT_ID('tableName') and xtype='PK';
if @Pk is not null
exec('Alter table tableName Drop '+ @Pk)

4、创建主键:

ALTER Table tableName ADD CONSTRAINT pkName PRIMARY KEY (pkfield)

5、联表更新

update t1 set t1.column2 = t2.column2  from table1  t,table2  t2  where t.column1 = t2.column1;