0%

数据库字段相关

记录一些平时用到的SQL语句

SQLServer

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
-- 新增字段
alter table databaseName.dbo.TableName add userId int default 0 not null;
-- 新增字段注释
exec sp_addextendedproperty N'MS_Description', N'注释', N'SCHEMA', N'dbo',N'TABLE', N'TableName', N'COLUMN', N'新增字段名称';

-- 修改字段
alter table databaseName.dbo.TableName alter userId int default 0 not null;

-- 删除字段
alter table databaseName.dbo.TableName drop cloumn 字段名称;

-- 定义新主键
-- 查询主键字段
exec sp_pkeys @table_name='TableName'
-- 删除原有主键
alter table databaseName.dbo.TableName drop constraint PK__qs_Table__D1BE6C7B566A428A;
-- 插入新主键
alter table [databaseName].[dbo].[TableName] add constraint [PK__qs_Table__D1BE6C7B566A428A] primary key clustered ([PK1], [PK2], [PK3], [PK4]...)

Oracle

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
-- 新增字段
alter table databaseName.TableName add userId number(11) default 0 not null;
-- 新增字段注释
comment on column databaseName.TableName.字段名称 is '注释';

-- 修改字段
alter table databaseName.dbo.TableName modify userId number(11) default 0 not null;

-- 删除字段
alter table databaseName.TableName drop (字段名称);

-- 定义新主键
-- 查询主键字段
select t.* from user_cons_columns t where t.table_name = 'TableName' and t.position is not null;
-- 删除原有主键
alter table databaseName.TableName drop constraint SYS_C00117888;
-- 插入新主键
alter table databaseName.TableName add constraint SYS_C00117888 primary key(PK1,PK2,PK2...);
-------------本文结束 感谢您的阅读-------------
只想买包辣条、喝杯奶茶加珍珠