ToB企服应用市场:ToB评测及商务社交产业平台

标题: 数据库系统原理之数据库安全与保护 [打印本页]

作者: 灌篮少年    时间: 2023-4-4 14:33
标题: 数据库系统原理之数据库安全与保护
数据库安全与保护

第一节 数据库完整性

一、完整性约束条件的作用对象

(1)列级约束

列级约束主要指对列的类型、取值范围、精度等的约束,具体包括如下内容:
(2)元组约束

元组约束指元组中各个字段之间的相互约束。
(3)表级约束

表级约束指若干元组之间、关系之间的联系的约束。
二、定义与实现完整性约束

1 实体完整性

在MySQL中,实体完整性是通过主键约束和候选键约束来实现的。
(1)主键约束

主键可以是表中的某一列,也可以是表中多个列所构成的一个组合。
其中,由多个列组合而成的主键也称为复合主键。
在MySQL中,主键列必须遵守如下一些规则:
主键约束可以在 CREATE TABLE 或 ALTER TABLE 语句中使用关键字“PRIMARY KEY”来实现。
其方式有两种:
注意:
如果主键仅由一个表中的某一列所构成,上述两种方法均可以定义主键约束。
如果主键是由表中多个列所构成的一个组合,则只能用上述第二种方法定义主键约束。
定义主键约束后,MySQL 会自动为主键创建一个唯一性索引,用于在查询中使用主键对数据进行快速检索,该索引名默认为 PRIMARY,也可以重新自定义命名。
(2)候选键约束

任何时候,候选键的值必须是唯一的,且不能为 NULL。
候选键可以在 CREATE TABLE 或 ALTER TABLE 语句中使用关键字“UNIQUE”来定义,其实现方法与主键约束相似,同样可作为列或者表(关系)的完整性约束两种方式。
MySQL中候选键与主键之间存在以下几点区别:
2 参照完整性

外键声明的方式:
"reference_definition" 语法项的定义:
  1. REFERENCES tbl_name (index_col_name, ...)
  2.         [ON DELETE reference_option]
  3.         [ON UPDATE reference_option]
复制代码
index_col_name 的语法格式:
  1. col_name [(length)] [ASC | DESC]
复制代码
reference_option 的语法格式:
  1. RESTRICT | CASCADE | SET NULL | NO ACTION
  2. 限制策略    级联策略    置空策略    不采取实施策略
复制代码
例子:在数据库 mysql_test 中创建一个商品订单表 orders,该表包含的订单信息有:订单号 oder_id、订购商品名 order_product、订购商品类型 order_product_type、订购客户 id 号 cust_id、订购时间 order_date、订购价格 order_price、订购数量 order_amount。要求商品订单表 orders 中的所有订购客户信息均已在表 customers 中记录在册。
  1. mysql> use mysql_test;
  2. Reading table information for completion of table and column names
  3. You can turn off this feature to get a quicker startup with -A
  4. Database changed
  5. mysql> create table orders
  6.     -> (
  7.     -> order_id int not null auto_increment,
  8.     -> order_product char(50) not null,
  9.     -> order_product_type char(50) not null,
  10.     -> cust_id int not null,
  11.     -> order_date datetime not null,
  12.     -> order_price double not null,
  13.     -> order_amount int not null,
  14.     -> primart key (order_id),
  15.     -> foreign key (cust_id)
  16.     -> references customers(cust_id)
  17.     -> on delete restrict
  18.     -> on update restrict
  19.     -> );
  20. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'key (order_id),
  21. foreign key (cust_id)
  22. references customers(cust_id)
  23. on delete r' at line 10
  24. mysql> desc customers;
  25. +--------------+----------+------+-----+---------+----------------+
  26. | Field        | Type     | Null | Key | Default | Extra          |
  27. +--------------+----------+------+-----+---------+----------------+
  28. | cust_id      | int      | NO   | PRI | NULL    | auto_increment |
  29. | cust_name    | char(20) | YES  |     | NULL    |                |
  30. | cust_sex     | char(1)  | YES  |     | M       |                |
  31. | cust_city    | char(10) | NO   |     | Beijing |                |
  32. | cust_address | char(50) | YES  |     | NULL    |                |
  33. | cust_contact | char(50) | YES  |     | NULL    |                |
  34. +--------------+----------+------+-----+---------+----------------+
  35. 6 rows in set (0.01 sec)
  36. mysql> create table orders ( order_id int not null auto_increment, order_product char(50) not null, order_product_type char(50) not null, cust_id int not null, order_date datetime not null, order_price double not null, order_amount int not null, primary key (order_id), foreign key (cust_id)  references customers(cust_id) on delete restrict on update restrict );
  37. Query OK, 0 rows affected (0.05 sec)
  38. mysql> desc orders;
  39. +--------------------+----------+------+-----+---------+----------------+
  40. | Field              | Type     | Null | Key | Default | Extra          |
  41. +--------------------+----------+------+-----+---------+----------------+
  42. | order_id           | int      | NO   | PRI | NULL    | auto_increment |
  43. | order_product      | char(50) | NO   |     | NULL    |                |
  44. | order_product_type | char(50) | NO   |     | NULL    |                |
  45. | cust_id            | int      | NO   | MUL | NULL    |                |
  46. | order_date         | datetime | NO   |     | NULL    |                |
  47. | order_price        | double   | NO   |     | NULL    |                |
  48. | order_amount       | int      | NO   |     | NULL    |                |
  49. +--------------------+----------+------+-----+---------+----------------+
  50. 7 rows in set (0.00 sec)
  51. mysql>
复制代码
当指定一个外键时,需要遵守下列原则:
3 用户定义的完整性

三、命名完整性约束
  1. CONSTRAINT [symbol]
复制代码
四、更新完整性约束

添加约束:ALTER TABLE 语句中使用 ADD CONSTRAINT子句
第二节 触发器

触发器(Trigger)是用户定义在关系表上的一类由事件驱动的数据库对象,也是一种保证数据完整性的方法。
触发器一旦定义,无须用户调用,任何对表的修改操作均由数据库服务器自动激活相应的触发器。
一、创建触发器
  1. CREATE TRIGGER trigger_name trigger_time trigger_event
  2.         ON tbl_name FOR EACH ROW trigger_body
复制代码
例子:在数据库 mysql_test 的表 customers 中创建一个触发器 customers_insert_trigger,用于每次向表 customers 插入一行数据时,将用户变量 str 的值设置为 “one customer added!”。
  1. mysql> use mysql_test;
  2. Reading table information for completion of table and column names
  3. You can turn off this feature to get a quicker startup with -A
  4. Database changed
  5. mysql> create trigger mysql_test.customers_insert_trigger after insert
  6.     -> on mysql_test.customers for each row set @str='one customer added!';
  7. Query OK, 0 rows affected (0.02 sec)
  8. mysql> insert into mysql_test.customers
  9.     -> values(null,'万华','F','长沙市','芙蓉区');
  10. ERROR 1136 (21S01): Column count doesn't match value count at row 1
  11. mysql> desc customers;
  12. +--------------+----------+------+-----+---------+----------------+
  13. | Field        | Type     | Null | Key | Default | Extra          |
  14. +--------------+----------+------+-----+---------+----------------+
  15. | cust_id      | int      | NO   | PRI | NULL    | auto_increment |
  16. | cust_name    | char(20) | YES  |     | NULL    |                |
  17. | cust_sex     | char(1)  | YES  |     | M       |                |
  18. | cust_city    | char(10) | NO   |     | Beijing |                |
  19. | cust_address | char(50) | YES  |     | NULL    |                |
  20. | cust_contact | char(50) | YES  |     | NULL    |                |
  21. +--------------+----------+------+-----+---------+----------------+
  22. 6 rows in set (0.01 sec)
  23. mysql> alter table mysql_test.customers drop column cust_contact;
  24. Query OK, 0 rows affected (0.03 sec)
  25. Records: 0  Duplicates: 0  Warnings: 0
  26. mysql> desc customers;
  27. +--------------+----------+------+-----+---------+----------------+
  28. | Field        | Type     | Null | Key | Default | Extra          |
  29. +--------------+----------+------+-----+---------+----------------+
  30. | cust_id      | int      | NO   | PRI | NULL    | auto_increment |
  31. | cust_name    | char(20) | YES  |     | NULL    |                |
  32. | cust_sex     | char(1)  | YES  |     | M       |                |
  33. | cust_city    | char(10) | NO   |     | Beijing |                |
  34. | cust_address | char(50) | YES  |     | NULL    |                |
  35. +--------------+----------+------+-----+---------+----------------+
  36. 5 rows in set (0.01 sec)
  37. mysql> insert into mysql_test.customers values(null,'万华','F','长沙市','芙蓉区');
  38. Query OK, 1 row affected (0.00 sec)
  39. mysql> select @str;
  40. +---------------------+
  41. | @str                |
  42. +---------------------+
  43. | one customer added! |
  44. +---------------------+
  45. 1 row in set (0.00 sec)
  46. mysql>
复制代码
二、删除触发器
  1. DROP TRIGGER [IF EXISTS] [schema_name.] trigger_name
复制代码
例子:删除数据库 mysql_test 中的触发器 customers_insert_trigger。
  1. mysql> drop trigger if exists mysql_test.customers_insert_trigger;
  2. Query OK, 0 rows affected (0.00 sec)
  3. mysql>
复制代码
三、使用触发器

1 INSERT 触发器

例子:在数据库 mysql_test 的表 customers 中重新创建触发器 customers_insert_trigger,用于每次向表 customers插入一行数据时,将用户变量 str 的值设置为新插入客户的 id 号。
  1. mysql> create trigger mysql_test.customers_insert_trigger after insert
  2.     -> on mysql_test.customers for each row set @str=new.cust_id;
  3. Query OK, 0 rows affected (0.00 sec)
  4. mysql> insert into mysql_test.customers values(null,'曾伟','F','长沙市','芙蓉区');
  5. Query OK, 1 row affected (0.00 sec)
  6. mysql> select @str;
  7. +------+
  8. | @str |
  9. +------+
  10. |  911 |
  11. +------+
  12. 1 row in set (0.00 sec)
  13. mysql> select * from customers;
  14. +---------+-----------+----------+-----------+--------------+
  15. | cust_id | cust_name | cust_sex | cust_city | cust_address |
  16. +---------+-----------+----------+-----------+--------------+
  17. |     901 | 张三      | F        | 北京市    | 武汉市       |
  18. |     902 | 李四      | M        | 武汉市    | 上海市       |
  19. |     903 | 李四      | M        | Beijing   | 上海市       |
  20. |     904 | 李四      | M        | Beijing   | 上海市       |
  21. |     910 | 万华      | F        | 长沙市    | 芙蓉区       |
  22. |     911 | 曾伟      | F        | 长沙市    | 芙蓉区       |
  23. +---------+-----------+----------+-----------+--------------+
  24. 6 rows in set (0.00 sec)
  25. mysql>
复制代码
2 DELETE 触发器

3 UPDATE 触发器

例子:在数据库 mysql_test 的表 customers 中创建一个触发器 customers_update_trigger,用于每次更新表 customers时,将该表中 cust_address 列的值设置为 cust_contact 列的值。
  1. mysql> create trigger mysql_test.customers_update_trigger before update
  2.     -> on mysql_test.customers for each row
  3.     -> set new.cust_address=old.cust_contact;
  4. ERROR 1054 (42S22): Unknown column 'cust_contact' in 'OLD'
  5. mysql> alter table mysql_test.customers
  6.     -> add column cust_contact char(50) null;
  7. Query OK, 0 rows affected (0.02 sec)
  8. Records: 0  Duplicates: 0  Warnings: 0
  9. mysql> create trigger mysql_test.customers_update_trigger before update on mysql_test.customers for each row set new.cust_address=old.cust_contact;
  10. Query OK, 0 rows affected (0.01 sec)
  11. mysql> update mysql_test.customers set cust_address='武汉市' where cust_name='曾伟';
  12. Query OK, 1 row affected (0.00 sec)
  13. Rows matched: 1  Changed: 1  Warnings: 0
  14. mysql> select cust_address from mysql_test.customers where cust_name='曾伟';
  15. +--------------+
  16. | cust_address |
  17. +--------------+
  18. | NULL         |
  19. +--------------+
  20. 1 row in set (0.00 sec)
  21. mysql> select * from customers;
  22. +---------+-----------+----------+-----------+--------------+--------------+
  23. | cust_id | cust_name | cust_sex | cust_city | cust_address | cust_contact |
  24. +---------+-----------+----------+-----------+--------------+--------------+
  25. |     901 | 张三      | F        | 北京市    | 武汉市       | NULL         |
  26. |     902 | 李四      | M        | 武汉市    | 上海市       | NULL         |
  27. |     903 | 李四      | M        | Beijing   | 上海市       | NULL         |
  28. |     904 | 李四      | M        | Beijing   | 上海市       | NULL         |
  29. |     910 | 万华      | F        | 长沙市    | 芙蓉区       | NULL         |
  30. |     911 | 曾伟      | F        | 长沙市    | NULL         | NULL         |
  31. +---------+-----------+----------+-----------+--------------+--------------+
  32. 6 rows in set (0.00 sec)
  33. mysql>
复制代码
第三节 安全性与访问控制

一、用户账号管理

查看MySQL数据库的使用者账号
  1. mysql> select user from mysql.user;
  2. +------------------+
  3. | user             |
  4. +------------------+
  5. | root             |
  6. | mysql.infoschema |
  7. | mysql.session    |
  8. | mysql.sys        |
  9. +------------------+
  10. 4 rows in set (0.01 sec)
复制代码
1 创建用户账号
  1. CREATE USER user[IDENTIFIED BY [PASSWORD] 'password']
复制代码
例子:在MySQL服务器中添加两个新的用户,其用户名分别为 zhangsan 和 lisi,他们的主机名均为 localhost,用户  zhangsan的口令设置为明文 123,用户 lisi的口令设置为对明文456使用 PASSWORD()函数加密返回的散列值。
  1. # 查看 mysql 初始的密码策略
  2. mysql> show variables like 'validate_password%';
  3. +--------------------------------------+--------+
  4. | Variable_name                        | Value  |
  5. +--------------------------------------+--------+
  6. | validate_password.check_user_name    | ON     |
  7. | validate_password.dictionary_file    |        |
  8. | validate_password.length             | 8      |
  9. | validate_password.mixed_case_count   | 1      |
  10. | validate_password.number_count       | 1      |
  11. | validate_password.policy             | MEDIUM |
  12. | validate_password.special_char_count | 1      |
  13. +--------------------------------------+--------+
  14. 7 rows in set (0.01 sec)
  15. # 设置密码的验证强度等级,设置 validate_password_policy 的全局参数为 LOW
  16. mysql> set global validate_password.policy=LOW;
  17. Query OK, 0 rows affected (0.01 sec)
  18. mysql>  create user 'zhangsan'@'localhost' identified by '12345678';
  19. Query OK, 0 rows affected (0.01 sec)
  20. mysql> select md5(12345678);  # MySQL 8.0+以上版本 password() 不可用
  21. +----------------------------------+
  22. | md5(12345678)                    |
  23. +----------------------------------+
  24. | 25d55ad283aa400af464c76d713c07ad |
  25. +----------------------------------+
  26. 1 row in set (0.00 sec)
  27. mysql> create user 'lisi'@'localhost' identified by '12345678';
  28. Query OK, 0 rows affected (0.00 sec)
  29. mysql>
复制代码
官网:https://dev.mysql.com/doc/refman/8.0/en/create-user.html
  1. CREATE USER
  2.   'jeffrey'@'localhost' IDENTIFIED WITH mysql_native_password
  3.                                    BY 'new_password1',
  4.   'jeanne'@'localhost' IDENTIFIED WITH caching_sha2_password
  5.                                   BY 'new_password2'
  6.   REQUIRE X509 WITH MAX_QUERIES_PER_HOUR 60
  7.   PASSWORD HISTORY 5
  8.   ACCOUNT LOCK;
复制代码
2 删除用户
  1. DROP USER user [,user]...
复制代码
例子:删除lisi用户
  1. mysql> drop user lisi;
  2. ERROR 1396 (HY000): Operation DROP USER failed for 'lisi'@'%'
  3. mysql> drop user lisi@localhost;
  4. Query OK, 0 rows affected (0.00 sec)
  5. mysql>
复制代码
3 修改用户账号
  1. RENAME USER old_user TO new_user [, old_user TO new_user] ...
复制代码
例子:将用户 zhangsan 的名字修改成 wangwu
  1. mysql> rename user 'zhangsan'@'localhost' to 'wangwu'@'localhost';
  2. Query OK, 0 rows affected (0.00 sec)
  3. mysql>
复制代码
4 修改用户口令
  1. SET PASSWORD [FOR user] =
  2.         {
  3.         PASSWORD('new_password') | 'encrypted password'
  4.         }
复制代码
例子:
  1. mysql> set password for 'wangwu'@'localhost' = '88888888';
  2. Query OK, 0 rows affected (0.00 sec)
  3. mysql>
复制代码
二、账户权限管理
  1. mysql> select user from mysql.user;
  2. +------------------+
  3. | user             |
  4. +------------------+
  5. | root             |
  6. | mysql.infoschema |
  7. | mysql.session    |
  8. | mysql.sys        |
  9. | wangwu           |
  10. +------------------+
  11. 5 rows in set (0.00 sec)
  12. mysql> show grants for 'wangwu'@'localhost';
  13. +--------------------------------------------+
  14. | Grants for wangwu@localhost                |
  15. +--------------------------------------------+
  16. | GRANT USAGE ON *.* TO `wangwu`@`localhost` |
  17. +--------------------------------------------+
  18. 1 row in set (0.01 sec)
  19. mysql>
复制代码
1 权限的授予
  1. GRANT
  2.         priv_type [(column_list)]
  3.                 [, priv_type [(column_list)]] ...
  4.   ON [object_type] priv_level
  5.   TO user_specification [, user_specification] ...
  6.   [WITH GRANT OPTION]
复制代码
例子:授予用户 zhangsan 在数据库 mysql_test 的表 customers 上拥有对列 cust_id 和列 cust_name 的 SELECT 权限
  1. ➜ mysql -uroot -p
  2. Enter password:
  3. Welcome to the MySQL monitor.  Commands end with ; or \g.
  4. Your MySQL connection id is 1105
  5. Server version: 8.0.32 Homebrew
  6. Copyright (c) 2000, 2023, Oracle and/or its affiliates.
  7. Oracle is a registered trademark of Oracle Corporation and/or its
  8. affiliates. Other names may be trademarks of their respective
  9. owners.
  10. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  11. mysql> grant select (cust_id, cust_name) on mysql_test.customers to 'zhangsan'@'localhost';
  12. ERROR 1410 (42000): You are not allowed to create a user with GRANT
  13. mysql> select user from mysql.user;
  14. +------------------+
  15. | user             |
  16. +------------------+
  17. | root             |
  18. | mysql.infoschema |
  19. | mysql.session    |
  20. | mysql.sys        |
  21. | wangwu           |
  22. +------------------+
  23. 5 rows in set (0.00 sec)
  24. mysql> rename user 'wangwu'@'localhost' to 'zhangsan'@'localhost';
  25. Query OK, 0 rows affected (0.01 sec)
  26. mysql> grant select (cust_id, cust_name) on mysql_test.customers to 'zhangsan'@'localhost';
  27. Query OK, 0 rows affected (0.01 sec)
  28. ➜ mysql -uzhangsan -p
  29. Enter password:
  30. Welcome to the MySQL monitor.  Commands end with ; or \g.
  31. Your MySQL connection id is 1107
  32. Server version: 8.0.32 Homebrew
  33. Copyright (c) 2000, 2023, Oracle and/or its affiliates.
  34. Oracle is a registered trademark of Oracle Corporation and/or its
  35. affiliates. Other names may be trademarks of their respective
  36. owners.
  37. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  38. mysql> select * from mysql_test.customers;
  39. ERROR 1142 (42000): SELECT command denied to user 'zhangsan'@'localhost' for table 'customers'
  40. mysql> select cust_id,cust_name from mysql_test.customers;
  41. +---------+-----------+
  42. | cust_id | cust_name |
  43. +---------+-----------+
  44. |     901 | 张三      |
  45. |     902 | 李四      |
  46. |     903 | 李四      |
  47. |     904 | 李四      |
  48. |     910 | 万华      |
  49. |     911 | 曾伟      |
  50. +---------+-----------+
  51. 6 rows in set (0.00 sec)
复制代码
例子2:创建 liming 和 huang 两个用户,并设置对应的系统登录口令,同时授予他们在数据库 mysql_test 的表 customers 上拥有 SELECT 和 UPDATE 的权限。
  1. mysql> grant select, update on mysql_test.customers to 'liming'@'localhost';
  2. ERROR 1410 (42000): You are not allowed to create a user with GRANT
  3. # mysql 8 最新的MySQL8不允许直接创建并授权,必须先让自己有GRANT权限,然后创建用户,再授权。
  4. mysql> SELECT host,user,Grant_priv,Super_priv FROM mysql.user;
  5. +-----------+------------------+------------+------------+
  6. | host      | user             | Grant_priv | Super_priv |
  7. +-----------+------------------+------------+------------+
  8. | %         | root             | Y          | Y          |
  9. | localhost | mysql.infoschema | N          | N          |
  10. | localhost | mysql.session    | N          | Y          |
  11. | localhost | mysql.sys        | N          | N          |
  12. | localhost | zhangsan         | N          | N          |
  13. +-----------+------------------+------------+------------+
  14. 5 rows in set (0.00 sec)
  15. mysql>
复制代码
例子3:授予zhangsan 可以在数据库 mysql_test 中执行所有数据库操作的权限
  1. mysql> grant all on mysql_test.* to 'zhangsan'@'localhost';
  2. Query OK, 0 rows affected (0.01 sec)
复制代码
例子4:授予 zhangsan 创建用户的权限
  1. mysql> grant create user on *.* to 'zhangsan'@'localhost';
  2. Query OK, 0 rows affected (0.01 sec)
  3. mysql>
复制代码
"priv_type"  的使用
2 权限的转移

例子:
  1. ~
  2. ➜ mysql -uroot -p
  3. Enter password:
  4. Welcome to the MySQL monitor.  Commands end with ; or \g.
  5. Your MySQL connection id is 1173
  6. Server version: 8.0.32 Homebrew
  7. Copyright (c) 2000, 2023, Oracle and/or its affiliates.
  8. Oracle is a registered trademark of Oracle Corporation and/or its
  9. affiliates. Other names may be trademarks of their respective
  10. owners.
  11. Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
  12. mysql> grant select,update on mysql_test.customers to 'zhou'@'localhost' identified by '123' with grant option;
  13. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'identified by '123' with grant option' at line 1
  14. # mysql 8.0+ 报错 先创建用户 后授权
  15. mysql> create user 'zhou'@'localhost' identified by '12345678';
  16. Query OK, 0 rows affected (0.01 sec)
  17. mysql> grant select,update on mysql_test.customers to 'zhou'@'localhost' identified by '12345678' with grant option;
  18. ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'identified by '12345678' with grant option' at line 1
  19. mysql> grant select,update on mysql_test.customers to 'zhou'@'localhost'  with grant option;
  20. Query OK, 0 rows affected (0.01 sec)
  21. mysql>
复制代码
3权限的撤销

  1. REVOKE
  2.         priv_type [(column_list)]
  3.                 [, priv_type [(column_list)]] ...
  4.   ON [object_type] priv_level
  5.   FROM user [, user] ...
复制代码
  1. REVOKE ALL PRIVILEGES, GRANT OPTION FROM user [, user]...
复制代码
例子:
  1. mysql> revoke select on mysql_test.customers from 'zhou'@'localhost';
  2. Query OK, 0 rows affected (0.00 sec)
  3. mysql>
复制代码
第四节 事物与并发控制

事务就是为保证数据的一致性而产生的一个概念和基本手段
一、事务的概念

二、事务的特征 ACID

例子:依据事物的ACID特性,分析并编写银行数据库系统中的转账事务T:从账户A转账S金额资金到账户B。
  1. BEGIN TRANSACTION
  2.         read(A);
  3.         A=A-S;
  4.         write(A);
  5.         if(A<0) ROLLBACK
  6.         else {
  7.         read(B);
  8.         B=B+S
  9.         write(B);
  10.         COMMIT;
  11.         }
复制代码
mysql复制表的两种方式:
第一、只复制表结构到新表
create table 新表 select * from 旧表 where 1=2
或者
create table 新表 like 旧表
第二、复制表结构及数据到新表
create table新表 select * from 旧表
  1. SELECT * INTO OUTFILE 'file_name' export_options
  2.         | INTO DUMPFILE 'file_name'
复制代码
将备份数据导入到一个和customers表结构相同的空表 customers_copy 中
  1. [FIELDS
  2.         [TERMINATED BY 'string']
  3.         [[OPTIONALLY] ENCLOSED BY 'char']
  4.         [ESCAPED BY 'char']
  5. ]
  6. [LINES TERMINATED BY 'string']
复制代码

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!




欢迎光临 ToB企服应用市场:ToB评测及商务社交产业平台 (https://dis.qidao123.com/) Powered by Discuz! X3.4