mysql 技巧之一

2021-09-22 01:31:16 字數 2035 閱讀 4940

primary key

表的主鍵,同時會建立索引。

mysql中每張table最多有乙個primary key,也可以沒有。primary key可以包含乙個column,也可以包含多個column。當包含多個column時,這些column的組合必須在table中唯一。

unique key

唯一鍵,可以定義多個,也可以不定義。乙個unique key可以包含乙個column,也可以包含多個column。unique key對應列或列組合的值必須在表中唯一。

key

可以定義多個,也可以不定義。可以包含乙個column或多個column,都可以有重複值。

information_schema.key_column_usage中可以查詢每個table中primary key和unique key的constraint_name、column_name。如果包含多個column,ordinal_position記錄每個column在key中的位置。

information_schema.columns可以查詢每個table中各個column的資訊,column_key表示該列的key屬性,取值有pri、uni、mul和空四種。pri表示該列是單列主鍵或聯合主鍵的一部分,uni表示該列是單列unique key,mul表示該列是聯合unique key的第一列或聯合key的第一列,或就是乙個單列key。

information_schema.statistics可以查詢所有key/index的資訊,seq_in_index表示column_name在索引中的序號。

比如有如下table test

create tabletest(

idvarchar(10) not null,

namevarchar(10) not null,

agetinyint not null,

staffnotinyint not null,

deptvarchar(5) not null,

teamvarchar(5) default null,

gendervarchar(5) not null,

homevarchar(30) default null,

zipcodevarchar(6) not null,

companyvarchar(10) not null,

key(company),

unique key(zipcode),

primary key(name,age,id),

unique key(dept,staffno,team),

key (gender,home)

);select column_name, column_key from information_schema.columns where table_schema=『test』 and table_name = 『test』;

select constraint_name, column_name, ordinal_position from information_schema.key_column_usage where table_schema = 『test』 and table_name=『test』;

select non_unique, index_name, seq_in_index, column_name from information_schema.statistics where table_schema = 『test』 and table_name = 『test』 order by index_name, seq_in_index;

原文:

linux技巧之一

實現redhat非正常關機的自動磁碟修復 先登入到伺服器,然後在 etc sysconfig裡增加乙個檔案autofsck,內容如下 autofsck def check yes prompt yes 改變檔案或目錄之最後修改時間 變為當前時間 執行格式 touch name name 可為檔案或目...

Visual C 程式設計技巧之一

visual c 程式設計技巧之一 1.如何獲取應用程式的例項控制代碼?2.如何通過 獲得應用程式主視窗的指標?3.如何在程式中獲得其他程式的 圖示?4.如何程式設計結束應用程式 如何程式設計控制 windows 的重新引導?5.怎樣加栽其他的應用程式?6.確定應用程式的 路徑 7.獲得各種目錄資訊...

JDBC優化技巧之一

以下是一些常用的jdbc小技巧,也許可以提高你的系統的執行速度。1.當使用preparedstatement callablestatement時,盡量使用它提供的setparams的方法。下面是錯誤的方法 callablestatement cstmt conn.preparecall resul...