mysql三:表操做

1、存儲引擎介紹

  存儲引擎即表類型,mysql根據不一樣的表類型會有不一樣的處理機制。html

  詳見:點擊查看mysql

2、表的介紹

  表至關於文件,表中的一條記錄就至關於文件的一行內容,不一樣的是,表中的一條記錄有對應的標題,稱爲表的字段sql

    

  id,name,qq,age稱爲字段,其他的,一行內容稱爲一條記錄session

3、建立表

#語法:
create table 表名(
字段名1 類型[(寬度) 約束條件],
字段名2 類型[(寬度) 約束條件],
字段名3 類型[(寬度) 約束條件]
);

#注意:
1. 在同一張表中,字段名是不能相同
2. 寬度和約束條件可選
3. 字段名和類型是必須的
mysql> create  database db2 charset utf8; 
Query OK, 1 row affected (0.01 sec)

mysql> use db2
Database changed
mysql> create table t1(
    -> id int,
    -> name varchar(50),
    -> sex enum('male', 'female'),
    -> age int(3)
    -> );
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
+---------------+
| Tables_in_db2 |
+---------------+
| t1            |
+---------------+
1 row in set (0.00 sec)

mysql> desc t1;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type                  | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id    | int(11)               | YES  |     | NULL    |       |
| name  | varchar(50)           | YES  |     | NULL    |       |
| sex   | enum('male','female') | YES  |     | NULL    |       |
| age   | int(3)                | YES  |     | NULL    |       |
+-------+-----------------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

mysql> select id,name,sex,age from t1;
Empty set (0.00 sec)

mysql> select * from t1;
Empty set (0.00 sec)
創表示例
mysql> insert into t1 values (1,'egon','male',18), (2,'alex','female',81);
Query OK, 2 rows affected (0.00 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from t1;
+------+------+--------+------+
| id   | name | sex    | age  |
+------+------+--------+------+
|    1 | egon | male   |   18 |
|    2 | alex | female |   81 |
+------+------+--------+------+
2 rows in set (0.00 sec)

mysql> insert into t1(id) values
    -> (3),
    -> (4);
Query OK, 2 rows affected (0.01 sec)
Records: 2  Duplicates: 0  Warnings: 0

mysql> select * from t1;
+------+------+--------+------+
| id   | name | sex    | age  |
+------+------+--------+------+
|    1 | egon | male   |   18 |
|    2 | alex | female |   81 |
|    3 | NULL | NULL   | NULL |
|    4 | NULL | NULL   | NULL |
+------+------+--------+------+
4 rows in set (0.00 sec)
往表中插入數據

  特別須要注意:字段匹配順序和表中最後一個字段不能加逗號ide

4、查看錶結構

mysql> describe t1;
+-------+-----------------------+------+-----+---------+-------+
| Field | Type                  | Null | Key | Default | Extra |
+-------+-----------------------+------+-----+---------+-------+
| id    | int(11)               | YES  |     | NULL    |       |
| name  | varchar(50)           | YES  |     | NULL    |       |
| sex   | enum('male','female') | YES  |     | NULL    |       |
| age   | int(3)                | YES  |     | NULL    |       |
+-------+-----------------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> show create table t1\G;  # 查看錶結構詳細結構,能夠添加\G

  相似的show create table mysql.user\G; 執行查看,能夠解決在屏幕比較小時,「----」顯示混亂的問題。測試

5、數據類型

  詳見:點擊插看ui

6、表完整性約束

  詳見:點擊查看this

7、修改表結構

語法:
1. 修改表名
      ALTER TABLE 表名 
                          RENAME 新表名;

2. 增長字段
      ALTER TABLE 表名
                          ADD 字段名  數據類型 [完整性約束條件…],    # 添加多個字段
                          ADD 字段名  數據類型 [完整性約束條件…];
      ALTER TABLE 表名
                          ADD 字段名  數據類型 [完整性約束條件…]  FIRST;   # 添加新字段到第一個
      ALTER TABLE 表名
                          ADD 字段名  數據類型 [完整性約束條件…]  AFTER 字段名;   # 添加新字段到指定字段以後

3. 刪除字段
      ALTER TABLE 表名 
                          DROP 字段名;

4. 修改字段————主要針對字段類型和字段名稱
      ALTER TABLE 表名 
                          MODIFY  字段名 數據類型 [完整性約束條件…];    # 修改字段類型
      ALTER TABLE 表名 
                          CHANGE 舊字段名 新字段名 舊數據類型 [完整性約束條件…];    # 修改字段名稱
      ALTER TABLE 表名 
                          CHANGE 舊字段名 新字段名 新數據類型 [完整性約束條件…];
示例:
mysql> use db2
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> create table service;
ERROR 1113 (42000): A table must have at least 1 column
mysql> create table service(id int);
Query OK, 0 rows affected (0.02 sec)

# 1、修改存儲引擎
mysql> alter table service 
    -> engine=innodb;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> create table student10(id int);               
Query OK, 0 rows affected (0.01 sec)

# 2、添加字段
mysql> alter table student10
    -> add name varchar(20) not null,
    -> add age int(3) not null default 22;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table student10
    -> add stu_num varchar(10) not null after name;    # after關鍵詞,添加到name字段後
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table student10
    -> add sex enum('male', 'female') default 'male' first;    # first關鍵詞,添加到最前面
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> select * from student10;
Empty set (0.00 sec)

mysql> describe student10;    # 查看錶結構
+---------+-----------------------+------+-----+---------+-------+
| Field   | Type                  | Null | Key | Default | Extra |
+---------+-----------------------+------+-----+---------+-------+
| sex     | enum('male','female') | YES  |     | male    |       |
| id      | int(11)               | YES  |     | NULL    |       |
| name    | varchar(20)           | NO   |     | NULL    |       |
| stu_num | varchar(10)           | NO   |     | NULL    |       |
| age     | int(3)                | NO   |     | 22      |       |
+---------+-----------------------+------+-----+---------+-------+
5 rows in set (0.00 sec)

# 3、刪除字段
mysql> alter table student10
    -> drop sex;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table service add mac varchar(20);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table service drop mac;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> describe student10;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int(11)     | YES  |     | NULL    |       |
| name    | varchar(20) | NO   |     | NULL    |       |
| stu_num | varchar(10) | NO   |     | NULL    |       |
| age     | int(3)      | NO   |     | 22      |       |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

# 4、修改字段類型modify
mysql> alter table student10
    -> modify age int(3);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table student10
    -> modify id int(11) not null primary key auto_increment;    # 修改成主鍵
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> select * from student10;
Empty set (0.01 sec)

mysql> describe student10;
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int(11)     | NO   | PRI | NULL    | auto_increment |
| name    | varchar(20) | NO   |     | NULL    |                |
| stu_num | varchar(10) | NO   |     | NULL    |                |
| age     | int(3)      | YES  |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

# 5、增長約束(針對已有的主鍵添加auto_increment)
mysql> alter table student10 modify id int(11) not null primary key auto_increment;
ERROR 1068 (42000): Multiple primary key defined

mysql> alter table student10 modify id int(11) not null auto_increment;
Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> describe student10;
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int(11)     | NO   | PRI | NULL    | auto_increment |
| name    | varchar(20) | NO   |     | NULL    |                |
| stu_num | varchar(10) | NO   |     | NULL    |                |
| age     | int(3)      | YES  |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

# 6、對已經存在的表添加複合主鍵
mysql> alter table service 
    -> add host_ip varchar(24),
    -> add port int(4);
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> describe service;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int(11)     | YES  |     | NULL    |       |
| host_ip | varchar(24) | YES  |     | NULL    |       |
| port    | int(4)      | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

mysql> alter table service
    -> add primary key(host_ip, port);    # 增長複合主鍵,經測試必須是這個字段存在
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> describe service;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int(11)     | YES  |     | NULL    |       |
| host_ip | varchar(24) | NO   | PRI | NULL    |       |
| port    | int(4)      | NO   | PRI | NULL    |       |
+---------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

# 7、刪除主鍵
# a.刪除自增約束
mysql> describe student10;
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int(11)     | NO   | PRI | NULL    | auto_increment |
| name    | varchar(20) | NO   |     | NULL    |                |
| stu_num | varchar(10) | NO   |     | NULL    |                |
| age     | int(3)      | YES  |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)

mysql> alter table student10
    -> modify name varchar(10) not null primary key;
ERROR 1068 (42000): Multiple primary key defined
mysql> alter table student10
    -> drop primary key;
ERROR 1075 (42000): Incorrect table definition; there can be only one auto column and it must be defined as a key
mysql> alter table student10
    -> modify id int(11) not null;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> describe student10;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int(11)     | NO   | PRI | NULL    |       |
| name    | varchar(20) | NO   |     | NULL    |       |
| stu_num | varchar(10) | NO   |     | NULL    |       |
| age     | int(3)      | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

# b.刪除主鍵
mysql> alter table student10
    -> drop primary key;
Query OK, 0 rows affected (0.03 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> describe student10;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int(11)     | NO   |     | NULL    |       |
| name    | varchar(20) | NO   |     | NULL    |       |
| stu_num | varchar(10) | NO   |     | NULL    |       |
| age     | int(3)      | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)


# 8、添加主鍵(和刪除主鍵挪移了位置,由於不想新增一個表)
mysql> alter table student10
    -> modify name varchar(10) not null primary key;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> describe student10;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| id      | int(11)     | NO   |     | NULL    |       |
| name    | varchar(10) | NO   | PRI | NULL    |       |
| stu_num | varchar(10) | NO   |     | NULL    |       |
| age     | int(3)      | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

# 9、添加主鍵和自動增加(多主鍵是不容許的,主鍵是惟一,非空的可是能夠多個字段聯合成一個主鍵)
mysql> alter table student10
    -> modify id int not null primary key auto_increment;
ERROR 1068 (42000): Multiple primary key defined
mysql> alter table student10
    -> drop primary key;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> alter table student1
    -> modify id int not null primary key auto_increment;
ERROR 1146 (42S02): Table 'db2.student1' doesn't exist
mysql> alter table student10
    -> modify id int not null primary key auto_increment;    # 添加主鍵和自動增加成功
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> describe student10;
+---------+-------------+------+-----+---------+----------------+
| Field   | Type        | Null | Key | Default | Extra          |
+---------+-------------+------+-----+---------+----------------+
| id      | int(11)     | NO   | PRI | NULL    | auto_increment |
| name    | varchar(10) | NO   |     | NULL    |                |
| stu_num | varchar(10) | NO   |     | NULL    |                |
| age     | int(3)      | YES  |     | NULL    |                |
+---------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
示例和練習

 8、複製表

一、複製表結構+記錄

  key不會複製:主鍵、外鍵和索引。spa

  create table t1 select host,user from mysql.user;code

mysql> select host, user from mysql.user;  
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| localhost | mysql.session |
| localhost | mysql.sys     |
| localhost | root          |
+-----------+---------------+
3 rows in set (0.00 sec)

mysql> create table t1 select host,user from mysql.user;   # 查詢出結果不輸出屏幕直接傳給新表
Query OK, 3 rows affected (0.02 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from t1;
+-----------+---------------+
| host      | user          |
+-----------+---------------+
| localhost | mysql.session |
| localhost | mysql.sys     |
| localhost | root          |
+-----------+---------------+
3 rows in set (0.00 sec)

mysql> desc t1;   # 查看key是否複製
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| host  | char(60) | NO   |     |         |       |
| user  | char(32) | NO   |     |         |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)

二、只複製表結構

  方法一:故意設置一個不成立的條件

mysql> select host,user from mysql.user where 1>5;      # 條件不成立,查不到任何數據  
Empty set (0.00 sec)

mysql> create table t2 select host,user from mysql.user where 1>5;
Query OK, 0 rows affected (0.02 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc t2;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| host  | char(60) | NO   |     |         |       |
| user  | char(32) | NO   |     |         |       |
+-------+----------+------+-----+---------+-------+
2 rows in set (0.00 sec)

  方法二:利用like

    create table t3 like mysql.user;

mysql> create table t3 like mysql.user;
Query OK, 0 rows affected (0.03 sec)

mysql> desc t3;
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Field                  | Type                              | Null | Key | Default               | Extra |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
| Host                   | char(60)                          | NO   | PRI |                       |       |
| User                   | char(32)                          | NO   | PRI |                       |       |
| Select_priv            | enum('N','Y')                     | NO   |     | N                     |       |
| Insert_priv            | enum('N','Y')                     | NO   |     | N                     |       |
| Update_priv            | enum('N','Y')                     | NO   |     | N                     |       |
| Delete_priv            | enum('N','Y')                     | NO   |     | N                     |       |
| Create_priv            | enum('N','Y')                     | NO   |     | N                     |       |
| Drop_priv              | enum('N','Y')                     | NO   |     | N                     |       |
| Reload_priv            | enum('N','Y')                     | NO   |     | N                     |       |
| Shutdown_priv          | enum('N','Y')                     | NO   |     | N                     |       |
| Process_priv           | enum('N','Y')                     | NO   |     | N                     |       |
| File_priv              | enum('N','Y')                     | NO   |     | N                     |       |
| Grant_priv             | enum('N','Y')                     | NO   |     | N                     |       |
| References_priv        | enum('N','Y')                     | NO   |     | N                     |       |
| Index_priv             | enum('N','Y')                     | NO   |     | N                     |       |
| Alter_priv             | enum('N','Y')                     | NO   |     | N                     |       |
| Show_db_priv           | enum('N','Y')                     | NO   |     | N                     |       |
| Super_priv             | enum('N','Y')                     | NO   |     | N                     |       |
| Create_tmp_table_priv  | enum('N','Y')                     | NO   |     | N                     |       |
| Lock_tables_priv       | enum('N','Y')                     | NO   |     | N                     |       |
| Execute_priv           | enum('N','Y')                     | NO   |     | N                     |       |
| Repl_slave_priv        | enum('N','Y')                     | NO   |     | N                     |       |
| Repl_client_priv       | enum('N','Y')                     | NO   |     | N                     |       |
| Create_view_priv       | enum('N','Y')                     | NO   |     | N                     |       |
| Show_view_priv         | enum('N','Y')                     | NO   |     | N                     |       |
| Create_routine_priv    | enum('N','Y')                     | NO   |     | N                     |       |
| Alter_routine_priv     | enum('N','Y')                     | NO   |     | N                     |       |
| Create_user_priv       | enum('N','Y')                     | NO   |     | N                     |       |
| Event_priv             | enum('N','Y')                     | NO   |     | N                     |       |
| Trigger_priv           | enum('N','Y')                     | NO   |     | N                     |       |
| Create_tablespace_priv | enum('N','Y')                     | NO   |     | N                     |       |
| ssl_type               | enum('','ANY','X509','SPECIFIED') | NO   |     |                       |       |
| ssl_cipher             | blob                              | NO   |     | NULL                  |       |
| x509_issuer            | blob                              | NO   |     | NULL                  |       |
| x509_subject           | blob                              | NO   |     | NULL                  |       |
| max_questions          | int(11) unsigned                  | NO   |     | 0                     |       |
| max_updates            | int(11) unsigned                  | NO   |     | 0                     |       |
| max_connections        | int(11) unsigned                  | NO   |     | 0                     |       |
| max_user_connections   | int(11) unsigned                  | NO   |     | 0                     |       |
| plugin                 | char(64)                          | NO   |     | mysql_native_password |       |
| authentication_string  | text                              | YES  |     | NULL                  |       |
| password_expired       | enum('N','Y')                     | NO   |     | N                     |       |
| password_last_changed  | timestamp                         | YES  |     | NULL                  |       |
| password_lifetime      | smallint(5) unsigned              | YES  |     | NULL                  |       |
| account_locked         | enum('N','Y')                     | NO   |     | N                     |       |
+------------------------+-----------------------------------+------+-----+-----------------------+-------+
45 rows in set (0.00 sec)

mysql> select * from t3;
Empty set (0.01 sec)
create table t3 like mysql.user;

 9、刪除表

  DROP TABLE 表名;

mysql> drop table t2;
Query OK, 0 rows affected (0.01 sec)

mysql> show tables;
+---------------+
| Tables_in_db3 |
+---------------+
| t1            |
+---------------+
1 row in set (0.00 sec)
相關文章
相關標籤/搜索