国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數(shù)據(jù)庫 > MySQL > 正文

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

2020-01-18 22:42:04
字體:
供稿:網(wǎng)友

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

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

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


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

/*創(chuàng)建防火墻表,將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、設置主鍵自增
下面我們通過一個實例來講解設置主鍵自增的方法:
首先創(chuàng)建數(shù)據(jù)庫,創(chuàng)建表

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獲取系統(tǒng)時間:

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開始增長。),還要得到系統(tǒng)默認日期:

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)  
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 木兰县| 阿图什市| 海兴县| 加查县| 青海省| 潮州市| 台东市| 桐乡市| 丹巴县| 韩城市| 独山县| 永城市| 平阳县| 邢台县| 长顺县| 唐山市| 丹凤县| 余姚市| 汉阴县| 闽侯县| 道孚县| 南华县| 普兰店市| 武隆县| 喀喇| 察哈| 宝鸡市| 都江堰市| 乐东| 汕尾市| 长泰县| 唐海县| 衡阳市| 宿州市| 阿巴嘎旗| 延川县| 晴隆县| 洛南县| 阜康市| 昆山市| 宝坻区|