添加表字段
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)
declare @cmd varchar(500)
declare @tablename varchar(100)
declare @keyname varchar(100)
Set @tablename='Temp1'
Set @keyname='id' --需要設置的key,分隔
select @defname= name
FROM sysobjects so
JOIN sysconstraints sc
ON so.id = sc.constid
WHERE object_name(so.parent_obj) = @tablename
and xtype='PK'
if @defname is not null
begin
select @cmd='alter table '+ @tablename+ ' drop constraint '+ @defname
--print @cmd
exec (@cmd)
end
else
set @defname='PK_'+@keyname
select @cmd='alter table '+ @tablename+ ' ADD constraint '+ @defname +' PRIMARY KEY CLUSTERED('+@keyname+')'
exec (@cmd)
如何取主鍵字段名稱及字段類型--得到主鍵字段名
1:
SELECT TABLE_NAME,COLUMN_NAME FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE TABLE_NAME<>'dtproperties'
2:
EXEC sp_pkeys @table_name='表名'
3:
select o.name as 表名,c.name as 字段名,k.colid as 字段序號,k.keyno as 索引順序,t.name as 類型
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
describe 表名 ;
然後循環取字段名和屬性,判斷哪個是主鍵 Key=='PRI'為主鍵
mysql> describe cdb_videos;
+--------------+-----------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-----------------------+------+-----+---------+-------+
| vid | varchar(16) | NO | PRI | | |
| uid | mediumint(8) unsigned | NO | | 0 | |
| dateline | int(10) unsigned | NO | MUL | 0 | |
| displayorder | tinyint(3) | NO | MUL | 0 | |
| tid | mediumint(8) unsigned | NO | | 0 | |
| pid | int(10) unsigned | NO | | 0 | |
| vtype | tinyint(1) unsigned | NO | | 0 | |
| vview | mediumint(8) unsigned | NO | | 0 | |
| vtime | smallint(6) unsigned | NO | | 0 | |
| visup | tinyint(1) unsigned | NO | | 0 | |
| vthumb | varchar(128) | NO | | | |
| vtitle | varchar(64) | NO | | | |
| vclass | varchar(32) | NO | | | |
| vautoplay | tinyint(1) | NO | | 0 | |
+--------------+-----------------------+------+-----+---------+-------+...余下全文>>
SELECT
t.TABLE_NAME,
t.CONSTRAINT_TYPE,
c.COLUMN_NAME,
c.ORDINAL_POSITION
FROM
INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS t,
INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS c
WHERE
t.TABLE_NAME = c.TABLE_NAME
AND t.TABLE_SCHEMA = 'test'
AND t.CONSTRAINT_TYPE = 'PRIMARY KEY';
測試執行結果如下:
mysql> SELECT
-> t.TABLE_NAME,
-> t.CONSTRAINT_TYPE,
-> c.COLUMN_NAME,
-> c.ORDINAL_POSITION
-> FROM
-> INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS t,
-> INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS c
-> WHERE
-> t.TABLE_NAME = c.TABLE_NAME
-> AND t.TABLE_SCHEMA = 'test'
-> AND t.CONSTRAINT_TYPE = 'PRIMARY KEY'
-> LIMIT 3;
+------------+-----------------+-------------+------------------+
| TABLE_NAME | CONSTRAINT_TYPE | COLUMN_NAME | ORDINAL_POSITION |
+------------+-----------------+-------------+------------------+
| mr_dept | PRIMARY KEY | dept_id | 1 |
| order | PRIMARY KEY | id | 1 |
| tab | PRIMARY KEY | id | 1 |
+------------+-----------------+-------------+------------------+
3 rows in set (0.06 sec)