SQL 数据库表,字段的操作(增,删,改,查)
添加
1、向Loginfo中添加新列“newcomlum1” ,int型,
ALTER TABLE Loginfo ADD newcomlum1 int NULL
2、表:LogInfo
代码
1 --判断要添加列的表中是否有主键
2 if exists(select 1 from sysobjects where parent_obj=object_id('LogInfo') and xtype='PK')
3 begin
4 print '表中已经有主键,列只能做为普通列添加'
5 --添加int类型的列,默认值为0
6 alter table LogInfo add 列名 int default 0
7 end
8 else
9 begin
10 print '表中无主键,添加主键列'
11 --添加int类型的列,默认值为0
12 alter table LogInfo add 列名 int primary key default 0
13 end
2 if exists(select 1 from sysobjects where parent_obj=object_id('LogInfo') and xtype='PK')
3 begin
4 print '表中已经有主键,列只能做为普通列添加'
5 --添加int类型的列,默认值为0
6 alter table LogInfo add 列名 int default 0
7 end
8 else
9 begin
10 print '表中无主键,添加主键列'
11 --添加int类型的列,默认值为0
12 alter table LogInfo add 列名 int primary key default 0
13 end
删
ALTER TABLE LogInfo DROP COLUMN 列名
修改
1、修改 类型
修改字段类型为int
ALTER TABLE LogInfo ALTER COLUMN 列名 int
2、修改字段名
LogInfo(表).LogInfocomlum1(字段名)--改为---〉LogInfocomlum2
EXEC sp_rename 'LogInfo.LogInfocomlum1', 'LogInfocomlum2'
查
(数据库中所有的表、用户表名)
select name from sysobjects
select name from sysobjects where xtype='u'
--下面LogInfo,用户自己建的表
select c.* from syscolumns c, sysobjects o
where c.id = o.id and o.type = 'U' and o.name = 'LogInfo'
很不错的网盘(http://ww618.com/-旺旺618 祝博友们每天旺,每天发。-_-)

浙公网安备 33010602011771号