当前位置:Gxlcms > 数据库问题 > Linux学习笔记:MySQL创建表

Linux学习笔记:MySQL创建表

时间:2021-07-01 10:21:17 帮助过:25人阅读

语句

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也是后补上去的。

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


添加索引

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

那个name(name)其实是索引名(字段名),索引名字可以自行定义。成功后,在desc里可以看到key显示MUL。


删除索引

alter table mydb.student drop index name;

这里的name是索引的名字


查看索引

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


创建部分内容的索引

有些内容比较长,但其中前几个字符已经是唯一的情况下,即可使用这几个字符形成索引。

例如使用name的前4个字符形成索引

  1. mysql> create index index_name on mydb.student (name(4));
  2. Query OK, 0 rows affected (0.20 sec)
  3. Records: 0  Duplicates: 0  Warnings: 0
  4. mysql> desc mydb.student;                                            
  5. +-------+-------------+------+-----+---------+----------------+
  6. | Field | Type        | Null | Key | Default | Extra          |
  7. +-------+-------------+------+-----+---------+----------------+
  8. | id    | int(11)     | NO   | PRI | NULL    | auto_increment |
  9. | name  | varchar(16) | NO   | MUL | NULL    |                |
  10. | score | int(1)      | YES  |     | 0       |                |
  11. +-------+-------------+------+-----+---------+----------------+
  12. 3 rows in set (0.00 sec)
  13. mysql>  show index from mydb.student\G;
  14. *************************** 1. row ***************************
  15.         Table: student
  16.    Non_unique: 0
  17.      Key_name: PRIMARY
  18.  Seq_in_index: 1
  19.   Column_name: id
  20.     Collation: A
  21.   Cardinality: 0
  22.      Sub_part: NULL
  23.        Packed: NULL
  24.          Null: 
  25.    Index_type: BTREE
  26.       Comment: 
  27. Index_comment: 
  28. *************************** 2. row ***************************
  29.         Table: student
  30.    Non_unique: 1
  31.      Key_name: index_name
  32.  Seq_in_index: 1
  33.   Column_name: name
  34.     Collation: A
  35.   Cardinality: 0
  36.      Sub_part: 4
  37.        Packed: NULL
  38.          Null: 
  39.    Index_type: BTREE
  40.       Comment: 
  41. Index_comment: 
  42. 2 rows in set (0.00 sec)
  43. ERROR: 
  44. No query specified

看2. Row的Sub_part


关于索引类型,参考http://blog.sina.com.cn/s/blog_6fd335bb0100v1lm.html



创建联合索引

在上面索引的基础上,输入多个字段名用以创建一个联合索引

例如

  1. mysql> create index index_id_name on mydb.student (id,name(4));    
  2. Query OK, 0 rows affected (0.13 sec)
  3. Records: 0  Duplicates: 0  Warnings: 0
  4. mysql> show index from mydb.student \G
  5. *************************** 1. row ***************************
  6.         Table: student
  7.    Non_unique: 0
  8.      Key_name: PRIMARY
  9.  Seq_in_index: 1
  10.   Column_name: id
  11.     Collation: A
  12.   Cardinality: 0
  13.      Sub_part: NULL
  14.        Packed: NULL
  15.          Null: 
  16.    Index_type: BTREE
  17.       Comment: 
  18. Index_comment: 
  19. *************************** 2. row ***************************
  20.         Table: student
  21.    Non_unique: 1
  22.      Key_name: index_name
  23.  Seq_in_index: 1
  24.   Column_name: name
  25.     Collation: A
  26.   Cardinality: 0
  27.      Sub_part: 4
  28.        Packed: NULL
  29.          Null: 
  30.    Index_type: BTREE
  31.       Comment: 
  32. Index_comment: 
  33. *************************** 3. row ***************************
  34.         Table: student
  35.    Non_unique: 1
  36.      Key_name: index_id_name
  37.  Seq_in_index: 1
  38.   Column_name: id
  39.     Collation: A
  40.   Cardinality: 0
  41.      Sub_part: NULL
  42.        Packed: NULL
  43.          Null: 
  44.    Index_type: BTREE
  45.       Comment: 
  46. Index_comment: 
  47. *************************** 4. row ***************************
  48.         Table: student
  49.    Non_unique: 1
  50.      Key_name: index_id_name
  51.  Seq_in_index: 2
  52.   Column_name: name
  53.     Collation: A
  54.   Cardinality: 0
  55.      Sub_part: 4
  56.        Packed: NULL
  57.          Null: 
  58.    Index_type: BTREE
  59.       Comment: 
  60. Index_comment: 
  61. 4 rows in set (0.00 sec)
  62. 可以看到第3、4行的key_name是一样的

删除联合索引也是一样用索引名字即可

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


创建单一索引,在关键字index前加unique关键字

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



Linux学习笔记:MySQL创建表

标签:mysql

人气教程排行