Mysql主键相关的sql语句集锦 |
本文标签:Mysql,主键 添加表字段 alter table table1 add transactor varchar(10) not Null; alter table table1 add id int unsigned not Null auto_increment primary key 修改某个表的字段类型及指定为空或非空 alter table 表名称 change 字段名称 字段名称 字段类型 [是否允许非空]; alter table 表名称 modify 字段名称 字段类型 [是否允许非空]; alter table 表名称 modify 字段名称 字段类型 [是否允许非空]; 修改某个表的字段名称及指定为空或非空 alter table 表名称 change 字段原名称 字段新名称 字段类型 [是否允许非空 删除某一字段 ALTER TABLE mytable DROP 字段 名; 添加唯一键 ALTER TABLE `test2` ADD UNIQUE ( `userid`) 修改主键 ALTER TABLE `test2` DROP PRIMARY KEY ,ADD PRIMARY KEY ( `id` ) 增加索引 ALTER TABLE `test2` ADD INDEX ( `id` ) ALTER TABLE `category ` MODIFY COLUMN `id` int(11) NOT NULL AUTO_INCREMENT FIRST ,ADD PRIMARY KEY (`id`); 修改主键的sql语句块如下: 22 declare @defname varchar(100) 如何取主键字段名称及字段类型--得到主键字段名 1: WHERE TABLE_NAME<>dtproperties 2: 3: from sysindexes i join sysindexkeys k on i.id = k.id and i.indid = k.indid join sysobjects o on i.id = o.id join syscolumns c on i.id=c.id and k.colid = c.colid join systypes t on c.xusertype=t.xusertype where o.xtype = U and o.name=要查询的表名 and exists(select 1 from sysobjects where xtype = PK and parent_obj=i.id and name = i.name) order by o.name,k.colid |