MySQL中的主鍵以及設定其自增的用法教程_Mysql

來源:互聯網
上載者:User

1、聲明主鍵的方法:
您可以在建立表的時候就為表加上主鍵,如:
CREATE TABLE tbl_name ([欄位描述省略...], PRIMARY KEY(index_col_name));
也可以更新表結構時為表加上主鍵,如:

ALTER TABLE tbl_name ADD PRIMARY KEY (index_col_name,…);/*建立一個qq表,將qq_id設為主鍵,且沒有對其進行NOT NULl約束*/create table qq(qq_id int(10),nick_name varchar(255) not null,primary key (qq_id))/*插入一條資料,將qq號設為10000(咱也幻想一下),暱稱設為"simaopig"*/INSERT INTO qq( qq_id, nick_name )VALUES ('10000', 'simaopig');

主鍵被認為是NOT NULL和UNIQUE約束最好的結合。如果這些列沒有被明確地定義為NOT NULL,MySQL會隱含地定義這些列。


2、主鍵也是索引:
剛才已經說了,主鍵其實也是索引,甚至在MySQL的術語裡面“鍵”就等於“索引”,所以“外鍵”一定要先設為“索引”。所以主鍵也應該和索引一樣,既可以作用於單獨的欄位,又可以作用於多個欄位。
舉個簡的例子吧,我住3單元,501室,我叫小小子,那麼只有3單元501室才能在本小區表裡面唯一確定我家。因為2單元,501室住著的可能也是個小小子,所以只有兩個欄位才能唯一確定我,也就是說可以二者組合作為主鍵。組合的主鍵,每個列都會隱含定義NOT NULL約束,且其二者加在一起被定義了UNIQUE 惟一約束。

/*建立防火牆表,將host 和port組合設為主鍵,注意我沒有將port設NOT NULL約束*/create table firewall(host varchar(11) not null,port smallint(4),access enum('deny', 'allow') not null,primary key (host,port))/*插入一條新的記錄,沒有啥問題1 row(s) inserted.*/INSERT INTO firewall (host ,port ,access)VALUES ('202.65.3.87', '21', 'deny');

3、設定主鍵自增
下面我們通過一個執行個體來講解設定主鍵自增的方法:
首先建立資料庫,建立表

mysql> create database ssh2; 
Query OK, 1 row affected (0.04 sec) 

 

mysql> use ssh2; 
Database changed 
mysql> create table user(   -> id integer primary key,   -> firstname varchar(200) not null,   -> lastname varchar(200) not null,   -> age integer   -> ); 
Query OK, 0 rows affected (0.46 sec) 

給主鍵增加一個自增的功能:

mysql> alter table user modify id integer auto_increment ; 
Query OK, 1 row affected (0.28 sec) Records: 1 Duplicates: 0 Warnings: 0 

這樣,上面的user表裡面的主鍵,id可以自增了。

給上面的主鍵id增加預設值和自增功能。

mysql> alter table user modify id integer auto_increment ; 
Query OK, 0 rows affected (0.39 sec) Records: 0 Duplicates: 0 Warnings: 0 

 

mysql> alter table user modify id integer default '1'; 
Query OK, 0 rows affected (0.16 sec) Records: 0 Duplicates: 0 Warnings: 0 

 

mysql> alter table user modify id integer auto_increment ; 
Query OK, 1 row affected (0.28 sec) Records: 1 Duplicates: 0 Warnings: 0 

MySql擷取系統時間:

mysql> alter table user add createtime timestamp default current_timestamp; 
Query OK, 2 rows affected (0.17 sec) Records: 2 Duplicates: 0 Warnings: 0 

MySql設定主鍵不可為空,還要自動成長(這裡沒有設定預設值,但是預設是1,從1開始增長。),還要得到系統預設日期:

mysql> create table dd(   -> id int primary key not null auto_increment,   -> name varchar(20),   -> time timestamp default current_timestamp   -> ); 
Query OK, 0 rows affected (0.10 sec) 
 mysql> insert into dd(name) values ('fhihgifds');

  

Query OK, 1 row affected (0.14 sec) 

 

mysql> insert into dd(name) values ('steven'); 
Query OK, 1 row affected (0.08 sec) 

 

mysql> select * from dd; 
+----+-----------+---------------------+ | id | name   | time        | +----+-----------+---------------------+ | 1 | fhihgifds | 2011-03-27 01:58:46 | | 2 | steven  | 2011-03-27 01:59:35 | +----+-----------+---------------------+ 2 rows in set (0.08 sec) 

 

mysql> insert into dd(name) values ('anthony'); 
Query OK, 1 row affected (0.09 sec) 

 

mysql> select * from dd; 
+----+-----------+---------------------+ | id | name   | time        | +----+-----------+---------------------+ | 1 | fhihgifds | 2011-03-27 01:58:46 | | 2 | steven  | 2011-03-27 01:59:35 | | 3 | anthony  | 2011-03-27 02:00:07 | +----+-----------+---------------------+ 3 rows in set (0.00 sec)  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.