mysql表屬性、索引、約束


1、表屬性

  創建表的基本語法: 

    create table 【if not exists】 表名 (字段列表 【,索引或約束列表】)【表選項列表】

  其中,字段列表格式如下:

    字段名 類型 【屬性列表】,

    字段名 類型 【屬性列表】...

  屬性列表中各個屬性之間用空格隔開。

  常用的字段屬性:

auto_increment 設置字段值自動增長,用於整數類型
primary key 設置字段為主鍵,此時該字段的值可以“唯一確定”一行數據
unique key 設置字段為唯一的,在整個數據表中不會重復
not null 設置字段不能為null,如果不指定該屬性,那么字段的值默認是可以為null的
default      設置字段的默認值,插入數據的時候,若不指定該字段你的值,會使用默認值填充。
comment 設置字段的說明

  說明:primary key跟unique key都確定了字段的唯一性,由這兩個屬性修飾的字段都能唯一的確定一行數據,區別在於primary key不能為null(指定了primary key的時候,默認的指定了not null屬性),而unique key則可以為null。可以說,primary key是特殊的unique key。

代碼:

/*創建表,注意屬性*/
mysql> create table item_properties_table( -> id int auto_increment primary key, -> name varchar(20) not null unique key, -> pwd varchar(48) not null, -> age tinyint default 18, -> email varchar(50) comment '電子郵件' -> ); Query OK, 0 rows affected (0.02 sec)
/*查看表結構,主要是為了查看comment*/ mysql
> show full columns from item_properties_table; +-------+------------+-----------------+------+-----+---------+--------------+---------------------------------+---------+ | Field | Type | Collation | Null | Key | Default |Extra | Privileges | Comment | +-------+------------+-----------------+------+-----+---------+--------------+---------------------------------+---------+ | id | int(11) | NULL | NO | PRI | NULL |auto_increment| select,insert,update,references | | | name | varchar(20)| utf8_general_ci | NO | UNI | NULL | | select,insert,update,references | | | pwd | varchar(48)| utf8_general_ci | NO | | NULL | | select,insert,update,references | | | age | tinyint(4) | NULL | YES | | 18 | | select,insert,update,references | | | email | varchar(50)| utf8_general_ci | YES | | NULL | | select,insert,update,references | 電子郵件 | +-------+------------+-----------------+------+-----+---------+--------------+---------------------------------+---------+ 5 rows in set (0.00 sec) mysql>

 

2、索引

  索引是幫助mysql高效獲取數據的數據結構,由系統維護。

  要使用索引,就要建立索引,就是指定一個表的某個或某些字段作為“索引字段”。格式如下:

    索引類型(字段名)

   索引類型:

    (1)、普通索引

        格式:key(字段名)

    (2)、唯一索引

        格式:unique key(字段名)

    (3)、主鍵索引

        格式:primary key(字段名)

    (4)、外鍵索引

        格式:foreign key(字段名)references 其他表(字段名)

    (5)、全文索引

        格式:fulltext(字段名)

普通索引、主鍵索引、唯一索引代碼:

mysql> create table indexes_table(#創建表,演示索引的創建
    -> id int auto_increment,
    -> name varchar(20),
    -> email varchar(50),
    -> age int,                   #沒有為age建立索引
    -> key(email),                #為email建立普通索引
    -> primary key(id),           #為id建立主鍵索引
    -> unique key(name)           #為name建立唯一索引
    -> );
Query OK, 0 rows affected (0.05 sec)

mysql> show full columns from indexes_table;
+-------+-------------+-----------------+------+-----+---------+----------------+---------------------------------+-------+
| Field | Type        | Collation       | Null | Key | Default | Extra          | Privileges                      |Comment|
+-------+-------------+-----------------+------+-----+---------+----------------+---------------------------------+-------+
| id    | int(11)     | NULL            | NO   | PRI | NULL    | auto_increment | select,insert,update,references |       |
| name  | varchar(20) | utf8_general_ci | YES  | UNI | NULL    |                | select,insert,update,references |       |
| email | varchar(50) | utf8_general_ci | YES  | MUL | NULL    |                | select,insert,update,references |       |
| age   | int(11)     | NULL            | YES  |     | NULL    |                | select,insert,update,references |       |
+-------+-------------+-----------------+------+-----+---------+----------------+---------------------------------+-------+
4 rows in set (0.00 sec)

這樣,就建立了索引。此時,以id,name或者email做條件來進行查找,速度會很快,如果以age做條件進行查找,相對就會慢一些。

 

外鍵索引代碼:

 

mysql> create table classes(#班級表
    -> id int auto_increment primary key, 
    -> classid varchar(10) unique key comment '班級號碼',
    -> teacher varchar(10) comment '班主任',
    -> opendate date comment '開班日期'
    -> );
Query OK, 0 rows affected (0.02 sec)

mysql> create table students(#學生表
    -> id int auto_increment primary key,
    -> name varchar(20),
    -> age tinyint,
    -> classid int comment '所在班級的id',
    -> foreign key(classid) references classes(id)
    -> );
Query OK, 0 rows affected (0.03 sec)

mysql> show full columns from classes;
+--------+-----------+---------------+------+-----+---------+---------------+---------------------------------+----------+
|Field   |Type       |Collation      | Null | Key | Default | Extra         | Privileges                      | Comment  |
+--------+-----------+---------------+------+-----+---------+---------------+---------------------------------+----------+
|id      |int(11)    |NULL           | NO   | PRI | NULL    | auto_increment| select,insert,update,references |          |
|classid |varchar(10)|utf8_general_ci| YES  | UNI | NULL    |               | select,insert,update,references | 班級號碼  |
|teacher |varchar(10)|utf8_general_ci| YES  |     | NULL    |               | select,insert,update,references | 班主任    |
|opendate|date       |NULL           | YES  |     | NULL    |               | select,insert,update,references | 開班日期  |
+--------+-----------+---------------+------+-----+---------+---------------+---------------------------------+----------+
4 rows in set (0.00 sec)

mysql> show full columns from students;
+-------+-----------+-----------------+------+-----+--------+--------------+---------------------------------+-----------+
|Field  |Type       | Collation       | Null | Key | Default|Extra         | Privileges                      | Comment   |
+-------+-----------+-----------------+------+-----+--------+--------------+---------------------------------+-----------+
|id     |int(11)    | NULL            | NO   | PRI | NULL   |auto_increment| select,insert,update,references |           |
|name   |varchar(20)| utf8_general_ci | YES  |     | NULL   |              | select,insert,update,references |           |
|age    |tinyint(4) | NULL            | YES  |     | NULL   |              | select,insert,update,references |           |
|classid|int(11)    | NULL            | YES  | MUL | NULL   |              | select,insert,update,references | 所在班級的id|
+-------+-----------+-----------------+------+-----+--------+--------------+---------------------------------+-----------+
4 rows in set (0.00 sec)

 

tips:外鍵——某個表中的某個字段,必須在另一個表中存在(字段名字可以不同,但是意義要一樣,比如上面學生表中的classid與班級表中的id)。

 

3、約束

  約束,就是對數據的一種要求。

  約束類型:

  (1)、主鍵約束

      格式:primary key(字段名)

      作用:該字段可以唯一的確定一行數據,其實就是主鍵。

  (2)、唯一約束

      格式:unique key(字段名)

      作用:該字段的值是唯一的,也可以區分一行數據。

  (3)、外鍵約束

      格式:foreign key(字段名)references 其他表(字段名)

      作用:字段的值必須是其他表中的某個對應的字段,不能隨便輸入。

  (4)、非空約束

      格式:not null,就是字段的屬性

  (5)、默認約束

      格式:default value,就是字段的默認值屬性

  (6)、檢查約束

      格式:check(判斷語句)

      代碼:

        create table check_constraint(

          age tinyint,

          check(age>=0 and age<100) #檢查約束

        );

 

 

總結:“主鍵約束、外鍵約束、唯一約束”跟“主鍵索引、外鍵索引、唯一索引”,其實是同一個概念的不同理解角度

    


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM