1.新建表:
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 ,
)
2.删除表:
Drop table [表名]
Truncate table [表名] --不写log 而且自增字段复位从1开始
3.新增字段:
ALTER TABLE [表名] ADD [字段名] NVARCHAR (50) NULL
添加多个字段时:ALTER TABLE [表名] ADD [字段名] NVARCHAR (50) NULL,[字段名] NVARCHAR (50) NULL
如:alter table DayLog add aaa char(10), abb char(10), acc char(10), ade char(10)
4.删除字段:
ALTER TABLE [表名] DROP COLUMN [字段名]
5.修改字段:
ALTER TABLE [表名] ALTER COLUMN [字段名] NVARCHAR (50) NULL
6.重命名表:
sp_rename ‘表名‘, ‘新表名‘, ‘OBJECT‘
7.新建约束:
ALTER TABLE [表名] ADD CONSTRAINT 约束名 CHECK ([约束字段] <= ‘2010-12-1‘)
8.删除约束:
ALTER TABLE [表名] DROP CONSTRAINT 约束名
如:IF EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N‘[dbo].[FK_BuiAddProType_BuildFunction]‘) AND type = ‘F‘)
ALTER TABLE [dbo].[BuiAddProType] DROP CONSTRAINT [FK_BuiAddProType_BuildFunction]
9.新建默认值
ALTER TABLE [表名] ADD CONSTRAINT 默认值名 DEFAULT ‘ABC‘ FOR [字段名]
10.删除默认值
ALTER TABLE [表名] DROP CONSTRAINT 默认值名
11.查看表结构
DESC order_00_00_db.t_order_info_0
12查看表字段以及字段注释
select column_name, column_comment from information_schema.columns where table_schema =‘order_00_00_db‘ and table_name = ‘t_order_info_0‘