程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> Mysql主鍵相關的sql語句集錦,mysql主鍵sql集錦

Mysql主鍵相關的sql語句集錦,mysql主鍵sql集錦

編輯:MySQL綜合教程

Mysql主鍵相關的sql語句集錦,mysql主鍵sql集錦


添加表字段

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


怎獲取mysql數據庫中某個表的主鍵或唯一字段

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 | |
+--------------+-----------------------+------+-----+---------+-------+...余下全文>>
 

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';

測試執行結果如下:

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)
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved