1. 程式人生 > >數據庫-數據表的增刪改查

數據庫-數據表的增刪改查

要求 指定 after warn efault eat code 數據表 --

查看數據表結構

  • 查看表的字段信息 DESC 表名

    • NULL表示該列可以存儲NULL值
    • key 指約束條件
    • Extra 指附加信息 例如主鍵自增
mysql> desc tb_empl;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int(11)     | NO   | PRI | NULL    |       |
| name   | varchar(25) | YES  |     | NULL    |       |
| deptId | int(11)     | YES  |     | NULL    |       |
| salary | float       | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
  • 查看更加詳細的表結構
mysql> show create table tb_dept2\G;
*************************** 1. row ***************************
       Table: tb_dept2
Create Table: CREATE TABLE `tb_dept2` (
  `id` int(11) NOT NULL,
  `name` varchar(22) DEFAULT NULL,
  `location` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)

ERROR:
No query specified

修改數據表

MySQL使用 ALTER TABLE語句修改表

  • 修改表名
mysql> ALTER TABLE tb_empl RENAME new_empl;
Query OK, 0 rows affected (0.35 sec)
# 可以看到tb_empl表名變為new_empl
mysql> SHOW TABLES;
+-------------------+
| Tables_in_test_db |
+-------------------+
| new_empl          |
| tb_dept2          |
| tb_dept3          |
| tb_dept7          |
| tb_dept8          |
| tb_emp5           |
| tb_temp6          |
+-------------------+
7 rows in set (0.02 sec)
  • 修改字段的數據類型
# 沒有修改之前的表結構數據類型
mysql> DESC NEW_EMPL;
+--------+-------------+------+-----+---------+-------+
| Field  | Type        | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| id     | int(11)     | NO   | PRI | NULL    |       |
| name   | varchar(25) | YES  |     | NULL    |       |
| deptId | int(11)     | YES  |     | NULL    |       |
| salary | float       | YES  |     | NULL    |       |
+--------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

# 修改之後的
mysql> ALTER TABLE new_empl MODIFY name char(50);
Query OK, 0 rows affected (0.28 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc new_empl;
+--------+----------+------+-----+---------+-------+
| Field  | Type     | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+-------+
| id     | int(11)  | NO   | PRI | NULL    |       |
| name   | char(50) | YES  |     | NULL    |       |
| deptId | int(11)  | YES  |     | NULL    |       |
| salary | float    | YES  |     | NULL    |       |
+--------+----------+------+-----+---------+-------+
4 rows in set (0.05 sec)

mysql>
  • 修改字段名

    • 修改後的數據類型可以與原來一樣但是不能為空
      ```sql
      mysql> ALTER TABLE new_empl change name new_name char(20);
      Query OK, 0 rows affected (0.17 sec)
      Records: 0 Duplicates: 0 Warnings: 0

mysql> desc new_empl;
+----------+----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------+------+-----+---------+-------+
| id | int(11) | NO | PRI | NULL | |
| new_name | char(20) | YES | | NULL | |
| deptId | int(11) | YES | | NULL | |
| salary | float | YES | | NULL | |
+----------+----------+------+-----+---------+-------+
4 rows in set (0.01 sec)
```

  • 添加字段
mysql> ALTER TABLE new_empl add country varchar(50);
Query OK, 0 rows affected (0.06 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc new_empl;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | NO   | PRI | NULL    |       |
| new_name | char(20)    | YES  |     | NULL    |       |
| deptId   | int(11)     | YES  |     | NULL    |       |
| salary   | float       | YES  |     | NULL    |       |
| country  | varchar(50) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
  • 刪除字段
mysql> ALTER TABLE new_empl DROP deptID;
Query OK, 0 rows affected (0.12 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> desc new_empl;
+----------+-------------+------+-----+---------+-------+
| Field    | Type        | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| id       | int(11)     | NO   | PRI | NULL    |       |
| new_name | char(20)    | YES  |     | NULL    |       |
| salary   | float       | YES  |     | NULL    |       |
| country  | varchar(50) | YES  |     | NULL    |       |
+----------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
  • 修改字段的排列位置

    • 修改字段為表的第一個字段
    
    # 將字段修改為表的第一個字段
    mysql> ALTER TABLE new_empl MODIFY country varchar(50) FIRST;
    Query OK, 0 rows affected (0.12 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    mysql> desc new_empl;
    +----------+-------------+------+-----+---------+-------+
    | Field    | Type        | Null | Key | Default | Extra |
    +----------+-------------+------+-----+---------+-------+
    | country  | varchar(50) | YES  |     | NULL    |       |
    | id       | int(11)     | NO   | PRI | NULL    |       |
    | new_name | char(20)    | YES  |     | NULL    |       |
    | salary   | float       | YES  |     | NULL    |       |
    +----------+-------------+------+-----+---------+-------+
    4 rows in set (0.02 sec)
    
    • 修改字段到表的指定列之後
    mysql> ALTER TABLE new_empl MODIFY country varchar(50) AFTER salary;
    Query OK, 0 rows affected (0.28 sec)
    Records: 0  Duplicates: 0  Warnings: 0
    
    mysql> DESC NEW_EMPL;
    +----------+-------------+------+-----+---------+-------+
    | Field    | Type        | Null | Key | Default | Extra |
    +----------+-------------+------+-----+---------+-------+
    | id       | int(11)     | NO   | PRI | NULL    |       |
    | new_name | char(20)    | YES  |     | NULL    |       |
    | salary   | float       | YES  |     | NULL    |       |
    | country  | varchar(50) | YES  |     | NULL    |       |
    +----------+-------------+------+-----+---------+-------+
    4 rows in set (0.01 sec)
  • 更改表的存儲引擎,如果有外鍵必須刪除外鍵

mysql> ALTER TABLE new_empl ENGINE=MyISAM;
ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails
# 刪除外鍵後

mysql> ALTER TABLE new_empl ENGINE=MyISAM;
Query OK, 0 rows affected (0.12 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show create table new_empl\G;
*************************** 1. row ***************************
       Table: new_empl
Create Table: CREATE TABLE `new_empl` (
  `id` int(11) NOT NULL COMMENT ‘員工編號‘,
  `new_name` char(20) DEFAULT NULL,
  `salary` float DEFAULT NULL COMMENT ‘工資‘,
  `country` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)

ERROR:
No query specified
  • 刪除表的外鍵約束
mysql> ALTER TABLE tb_emp5 DROP FOREIGN KEY fk_tb_empl;
Query OK, 0 rows affected (0.04 sec)
Records: 0  Duplicates: 0  Warnings: 0

刪除數據表

  • 刪除沒有被關聯的表
mysql> DROP TABLE IF EXISTS new_empl;
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> show tables;
+-------------------+
| Tables_in_test_db |
+-------------------+
| tb_dept2          |
| tb_dept3          |
| tb_dept7          |
| tb_dept8          |
| tb_emp5           |
| tb_temp6          |
+-------------------+
6 rows in set (0.00 sec)
  • 刪除被其他表關聯的主表

    • 外鍵關聯的情況下,想刪除主表,必須先刪除和他關聯的子表
    • 如果想保留字表,則必須解除外鍵關聯

最近公司要求做電商數據分析報告,未來有事情要做了

數據庫-數據表的增刪改查