Linux學習筆記:MySQL建立表

來源:互聯網
上載者:User

標籤:mysql

語句

Create Table: CREATE TABLE `student` (

  `id` int(4) NOT NULL,

  `name` varchar(16) NOT NULL,

  `score` int(1) DEFAULT ‘0‘

) ENGINE=InnoDB DEFAULT CHARSET=utf8



增加主鍵

alter table student change id id int primary key auto_increment ;

一般不需要修改主鍵,但有時建表的時候忘記了加,要後補就可以這樣寫

格式是alter table <表名> change <舊欄位名> <新欄位名>  欄位屬性,這裡的auto_increment也是後補上去的。

mysql> desc mydb.student;+-------+-------------+------+-----+---------+----------------+| Field | Type        | Null | Key | Default | Extra          |+-------+-------------+------+-----+---------+----------------+| id    | int(11)     | NO   | PRI | NULL    | auto_increment || name  | varchar(16) | NO   |     | NULL    |                || score | int(1)      | YES  |     | 0       |                |+-------+-------------+------+-----+---------+----------------+3 rows in set (0.00 sec)


添加索引

mysql>  alter table mydb.student add index name(name);Query OK, 0 rows affected (0.63 sec)Records: 0  Duplicates: 0  Warnings: 0mysql> desc mydb.student;+-------+-------------+------+-----+---------+----------------+| Field | Type        | Null | Key | Default | Extra          |+-------+-------------+------+-----+---------+----------------+| id    | int(11)     | NO   | PRI | NULL    | auto_increment || name  | varchar(16) | NO   | MUL | NULL    |                || score | int(1)      | YES  |     | 0       |                |+-------+-------------+------+-----+---------+----------------+3 rows in set (0.00 sec)

那個name(name)其實是索引名(欄位名),索引名字可以自行定義。成功後,在desc裡可以看到key顯示MUL。


刪除索引

alter table mydb.student drop index name;

這裡的name是索引的名字


查看索引

show index from mydb.student;    +---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+| Table   | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardinality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+| student |          0 | PRIMARY  |            1 | id          | A         |           0 |     NULL | NULL   |      | BTREE      |         |               |+---------+------------+----------+--------------+-------------+-----------+-------------+----------+--------+------+------------+---------+---------------+1 row in set (0.00 sec)


建立部分內容的索引

有些內容比較長,但其中前幾個字元已經是唯一的情況下,即可使用這幾個字元形成索引。

例如使用name的前4個字元形成索引

mysql> create index index_name on mydb.student (name(4));Query OK, 0 rows affected (0.20 sec)Records: 0  Duplicates: 0  Warnings: 0mysql> desc mydb.student;                                            +-------+-------------+------+-----+---------+----------------+| Field | Type        | Null | Key | Default | Extra          |+-------+-------------+------+-----+---------+----------------+| id    | int(11)     | NO   | PRI | NULL    | auto_increment || name  | varchar(16) | NO   | MUL | NULL    |                || score | int(1)      | YES  |     | 0       |                |+-------+-------------+------+-----+---------+----------------+3 rows in set (0.00 sec)mysql>  show index from mydb.student\G;*************************** 1. row ***************************        Table: student   Non_unique: 0     Key_name: PRIMARY Seq_in_index: 1  Column_name: id    Collation: A  Cardinality: 0     Sub_part: NULL       Packed: NULL         Null:    Index_type: BTREE      Comment: Index_comment: *************************** 2. row ***************************        Table: student   Non_unique: 1     Key_name: index_name Seq_in_index: 1  Column_name: name    Collation: A  Cardinality: 0     Sub_part: 4       Packed: NULL         Null:    Index_type: BTREE      Comment: Index_comment: 2 rows in set (0.00 sec)ERROR: No query specified

看2. Row的Sub_part


關於索引類型,參考http://blog.sina.com.cn/s/blog_6fd335bb0100v1lm.html



建立聯合索引

在上面索引的基礎上,輸入多個欄位名用以建立一個聯合索引

例如

mysql> create index index_id_name on mydb.student (id,name(4));    Query OK, 0 rows affected (0.13 sec)Records: 0  Duplicates: 0  Warnings: 0mysql> show index from mydb.student \G*************************** 1. row ***************************        Table: student   Non_unique: 0     Key_name: PRIMARY Seq_in_index: 1  Column_name: id    Collation: A  Cardinality: 0     Sub_part: NULL       Packed: NULL         Null:    Index_type: BTREE      Comment: Index_comment: *************************** 2. row ***************************        Table: student   Non_unique: 1     Key_name: index_name Seq_in_index: 1  Column_name: name    Collation: A  Cardinality: 0     Sub_part: 4       Packed: NULL         Null:    Index_type: BTREE      Comment: Index_comment: *************************** 3. row ***************************        Table: student   Non_unique: 1     Key_name: index_id_name Seq_in_index: 1  Column_name: id    Collation: A  Cardinality: 0     Sub_part: NULL       Packed: NULL         Null:    Index_type: BTREE      Comment: Index_comment: *************************** 4. row ***************************        Table: student   Non_unique: 1     Key_name: index_id_name Seq_in_index: 2  Column_name: name    Collation: A  Cardinality: 0     Sub_part: 4       Packed: NULL         Null:    Index_type: BTREE      Comment: Index_comment: 4 rows in set (0.00 sec)可以看到第3、4行的key_name是一樣的

刪除聯合索引也是一樣用索引名字即可

mysql> drop index index_id_name on mydb.student;  Query OK, 0 rows affected (0.00 sec)Records: 0  Duplicates: 0  Warnings: 0


建立單一索引,在關鍵字index前加unique關鍵字

mysql> create unique index index_id_name on mydb.student (id,name(4));Query OK, 0 rows affected (0.23 sec)Records: 0  Duplicates: 0  Warnings: 0



Linux學習筆記:MySQL建立表

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.