建库、建表、修改表、复制表、字符类型、数值类型、枚举类型、日期时间类型、检索目录、数据导入命令、数据导入步骤、数据导出命令、非空、默认值、唯一索

Top

NSD DBA DAY04

  1. 案例1:表管理
  2. 案例2:数据类型
  3. 案例3:数据批量处理
  4. 案例4:表头基本约束

1 案例1:表管理

1.1 问题

  1. 建库练习
  2. 建表练习
  3. 修改表练习

1.2 方案

在MySQL50主机完成练习。

1.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:建库练习

库名命名规则:

仅可以使用数字、字母、下划线、不能纯数字

区分字母大小写,

具有唯一性

不可使用MySQL命令或特殊字符

命令操作如下所示:

  1. //库名区分字母大小写
  2. mysql> create database gamedb ;
  3. Query OK, 1 row affected (0.14 sec)
  4. mysql> create database GAMEDB ;
  5. Query OK, 1 row affected (0.08 sec)
  6. mysql> create database GAMEDB ;
  7. ERROR 1007 (HY000): Can't create database 'GAMEDB'; database exists //重名报错
  8. //加if not exists 命令避免重名报错
  9. mysql> create database if not exists gamedb ;
  10. Query OK, 1 row affected, 1 warning (0.03 sec) //正常
  11. mysql> show databases; //查看创建的库
  12. +--------------------+
  13. | Database |
  14. +--------------------+
  15. | GAMEDB |
  16. | gamedb |
  17. | information_schema |
  18. | mysql |
  19. | performance_schema |
  20. | sys |
  21. | tarena |
  22. +--------------------+
  23. 7 rows in set (0.00 sec)
  24. mysql> drop database gamedb; //删除库
  25. Query OK, 0 rows affected (0.11 sec)
  26. mysql> drop database gamedb; // 删除没有的库报错
  27. ERROR 1008 (HY000): Can’t drop database ‘gamedb’; database doesn’t exist
  28. //加if exists 删除没有的库,也不报错
  29. mysql> drop database if exists gamedb;
  30. Query OK, 0 rows affected, 1 warning (0.00 sec)

步骤二:建表练习

命令操作如下所示:

  1. mysql> create database 学生库; //建库
  2. Query OK, 1 row affected (0.11 sec)
  3. mysql> create table 学生库.学生信息表( //建表
  4. -> 姓名 char(10),
  5. -> 班级 char(9),
  6. -> 性别 char(4),
  7. -> 年龄 int
  8. -> );
  9. Query OK, 0 rows affected (0.47 sec)
  10. mysql> use 学生库; //进入库
  11. Reading table information for completion of table and column names
  12. You can turn off this feature to get a quicker startup with -A
  13. Database changed
  14. mysql> show tables; //查看表
  15. +---------------------+
  16. | Tables_in_学生库 |
  17. +---------------------+
  18. | 学生信息表 |
  19. +---------------------+
  20. 1 row in set (0.00 sec)
  21. mysql> desc 学生信息表; //查看表头
  22. +--------+----------+------+-----+---------+-------+
  23. | Field | Type | Null | Key | Default | Extra |
  24. +--------+----------+------+-----+---------+-------+
  25. | 姓名 | char(10) | YES | | NULL | |
  26. | 班级 | char(9) | YES | | NULL | |
  27. | 性别 | char(4) | YES | | NULL | |
  28. | 年龄 | int | YES | | NULL | |
  29. +--------+----------+------+-----+---------+-------+
  30. 4 rows in set (0.00 sec)
  31. //删除表
  32. mysql> drop table 学生库.学生信息表;
  33. //删除库
  34. mysql> drop database 学生库;

使用英文命名,重新建库、建表

  1. mysql> create database studb; //建库
  2. Query OK, 1 row affected (0.11 sec)
  3. mysql> create table studb.stu( //建表
  4. -> name char(10),
  5. -> class char(9),
  6. -> gender char(4),
  7. -> age int
  8. -> );
  9. Query OK, 0 rows affected (1.17 sec)
  10. mysql> desc studb.stu; //查看表头
  11. +--------+----------+------+-----+---------+-------+
  12. | Field | Type | Null | Key | Default | Extra |
  13. +--------+----------+------+-----+---------+-------+
  14. | name | char(10) | YES | | NULL | |
  15. | class | char(9) | YES | | NULL | |
  16. | gender | char(4) | YES | | NULL | |
  17. | age | int | YES | | NULL | |
  18. +--------+----------+------+-----+---------+-------+
  19. 4 rows in set (0.00 sec)

步骤三:修改表练习

命令操作如下所示:

 
  1. mysql> alter table studb.stu rename studb.stuinfo; //修改表名
  2. Query OK, 0 rows affected (0.28 sec)
  3. mysql> use studb; //进入库
  4. Reading table information for completion of table and column names
  5. You can turn off this feature to get a quicker startup with -A
  6. Database changed
  7. mysql> show tables; //查看表
  8. +-----------------+
  9. | Tables_in_studb |
  10. +-----------------+
  11. | stuinfo |
  12. +-----------------+
  13. 1 row in set (0.00 sec)
  14. mysql> alter table studb.stuinfo drop age ; //删除age表头
  15. Query OK, 0 rows affected (0.52 sec)
  16. Records: 0 Duplicates: 0 Warnings: 0
  17. mysql> desc stuinfo; //查看表头
  18. +--------+----------+------+-----+---------+-------+
  19. | Field | Type | Null | Key | Default | Extra |
  20. +--------+----------+------+-----+---------+-------+
  21. | name | char(10) | YES | | NULL | |
  22. | class | char(9) | YES | | NULL | |
  23. | gender | char(4) | YES | | NULL | |
  24. +--------+----------+------+-----+---------+-------+
  25. 3 rows in set (0.00 sec)
  26. //添加表头,默认添加在末尾
  27. mysql> alter table studb.stuinfo add mail char(30) ;
  28. Query OK, 0 rows affected (0.24 sec)
  29. Records: 0 Duplicates: 0 Warnings: 0
  30. //查看表头
  31. mysql> desc studb.stuinfo;
  32. +--------+----------+------+-----+---------+-------+
  33. | Field | Type | Null | Key | Default | Extra |
  34. +--------+----------+------+-----+---------+-------+
  35. | name | char(10) | YES | | NULL | |
  36. | class | char(9) | YES | | NULL | |
  37. | gender | char(4) | YES | | NULL | |
  38. | mail | char(30) | YES | | NULL | |
  39. +--------+----------+------+-----+---------+-------+
  40. 4 rows in set (0.00 sec)
  41. //first 把表头添加首位
  42. //after 添加在指定表头名的下方
  43. mysql> alter table studb.stuinfo add number char(9) first , add school char(10) after name;
  44. Query OK, 0 rows affected (0.48 sec)
  45. Records: 0 Duplicates: 0 Warnings: 0
  46. //查看表结构
  47. mysql> desc studb.stuinfo; //查看表头
  48. +--------+----------+------+-----+---------+-------+
  49. | Field | Type | Null | Key | Default | Extra |
  50. +--------+----------+------+-----+---------+-------+
  51. | number | char(9) | YES | | NULL | |
  52. | name | char(10) | YES | | NULL | |
  53. | school | char(10) | YES | | NULL | |
  54. | class | char(9) | YES | | NULL | |
  55. | gender | char(4) | YES | | NULL | |
  56. | mail | char(30) | YES | | NULL | |
  57. +--------+----------+------+-----+---------+-------+
  58. 6 rows in set (0.00 sec)
  59. //修改表头数据类型
  60. mysql> alter table studb.stuinfo modify mail varchar(50);
  61. Query OK, 0 rows affected (1.17 sec)
  62. Records: 0 Duplicates: 0 Warnings: 0
  63. mysql> desc studb.stuinfo;
  64. +--------+-------------+------+-----+---------+-------+
  65. | Field | Type | Null | Key | Default | Extra |
  66. +--------+-------------+------+-----+---------+-------+
  67. | number | char(9) | YES | | NULL | |
  68. | name | char(10) | YES | | NULL | |
  69. | school | char(10) | YES | | NULL | |
  70. | class | char(9) | YES | | NULL | |
  71. | gender | char(4) | YES | | NULL | |
  72. | mail | varchar(50) | YES | | NULL | |
  73. +--------+-------------+------+-----+---------+-------+
  74. 6 rows in set (0.01 sec)
  75. //修改表头名
  76. mysql> alter table studb.stuinfo change class 班级 char(9) ;
  77. Query OK, 0 rows affected (0.12 sec)
  78. Records: 0 Duplicates: 0 Warnings: 0
  79. //查看表头
  80. mysql> desc studb.stuinfo;
  81. +--------+-------------+------+-----+---------+-------+
  82. | Field | Type | Null | Key | Default | Extra |
  83. +--------+-------------+------+-----+---------+-------+
  84. | number | char(9) | YES | | NULL | |
  85. | name | char(10) | YES | | NULL | |
  86. | school | char(10) | YES | | NULL | |
  87. | 班级 | char(9) | YES | | NULL | |
  88. | gender | char(4) | YES | | NULL | |
  89. | mail | varchar(50) | YES | | NULL | |
  90. +--------+-------------+------+-----+---------+-------+
  91. 6 rows in set (0.00 sec)
  92. //一起删除多个表头
  93. mysql> alter table studb.stuinfo drop school , drop 班级 ,drop mail ;
  94. Query OK, 0 rows affected (0.73 sec)
  95. Records: 0 Duplicates: 0 Warnings: 0
  96. //查看表头
  97. mysql> desc studb.stuinfo;
  98. +--------+----------+------+-----+---------+-------+
  99. | Field | Type | Null | Key | Default | Extra |
  100. +--------+----------+------+-----+---------+-------+
  101. | number | char(9) | YES | | NULL | |
  102. | name | char(10) | YES | | NULL | |
  103. | gender | char(4) | YES | | NULL | |
  104. +--------+----------+------+-----+---------+-------+
  105. 3 rows in set (0.00 sec)
  106. mysql>
  107. //使用modify 修改表头的位置
  108. mysql> alter table studb.stuinfo modify gender char(4) after number;
  109. Query OK, 0 rows affected (0.77 sec)
  110. Records: 0 Duplicates: 0 Warnings: 0
  111. //查看表头
  112. mysql> desc studb.stuinfo;
  113. +--------+----------+------+-----+---------+-------+
  114. | Field | Type | Null | Key | Default | Extra |
  115. +--------+----------+------+-----+---------+-------+
  116. | number | char(9) | YES | | NULL | |
  117. | gender | char(4) | YES | | NULL | |
  118. | name | char(10) | YES | | NULL | |
  119. +--------+----------+------+-----+---------+-------+
  120. 3 rows in set (0.00 sec)
  121. //再修改回原来位置
  122. mysql> alter table studb.stuinfo modify gender char(4) after name;
  123. Query OK, 0 rows affected (0.50 sec)
  124. Records: 0 Duplicates: 0 Warnings: 0
  125. //查看表头
  126. mysql> desc studb.stuinfo;
  127. +--------+----------+------+-----+---------+-------+
  128. | Field | Type | Null | Key | Default | Extra |
  129. +--------+----------+------+-----+---------+-------+
  130. | number | char(9) | YES | | NULL | |
  131. | name | char(10) | YES | | NULL | |
  132. | gender | char(4) | YES | | NULL | |
  133. +--------+----------+------+-----+---------+-------+
  134. 3 rows in set (0.01 sec)

复制表 (拷贝已有的表 和系统命令 cp 的功能一样 )

 
  1. //复制tarena库salary表到 studb库 表名不变
  2. mysql> create table studb.salary select * from tarena.salary;
  3. Query OK, 8055 rows affected (2.66 sec)
  4. Records: 8055 Duplicates: 0 Warnings: 0
  5. //查看表头,源表的key 不会被复制
  6. mysql> desc studb.salary;
  7. +-------------+------+------+-----+---------+-------+
  8. | Field | Type | Null | Key | Default | Extra |
  9. +-------------+------+------+-----+---------+-------+
  10. | id | int | NO | | 0 | |
  11. | date | date | YES | | NULL | |
  12. | employee_id | int | YES | | NULL | |
  13. | basic | int | YES | | NULL | |
  14. | bonus | int | YES | | NULL | |
  15. +-------------+------+------+-----+---------+-------+
  16. 5 rows in set (0.00 sec)
  17. //查看表行数
  18. mysql> select count(*) from studb.salary;
  19. +----------+
  20. | count(*) |
  21. +----------+
  22. | 8055 |
  23. +----------+
  24. 1 row in set (0.00 sec)
  25. //仅仅复制表头
  26. mysql> create table studb.salary2 like tarena.salary;
  27. Query OK, 0 rows affected (0.95 sec)
  28. //查看表头
  29. mysql> desc studb.salary2;
  30. +-------------+------+------+-----+---------+----------------+
  31. | Field | Type | Null | Key | Default | Extra |
  32. +-------------+------+------+-----+---------+----------------+
  33. | id | int | NO | PRI | NULL | auto_increment |
  34. | date | date | YES | | NULL | |
  35. | employee_id | int | YES | MUL | NULL | |
  36. | basic | int | YES | | NULL | |
  37. | bonus | int | YES | | NULL | |
  38. +-------------+------+------+-----+---------+----------------+
  39. 5 rows in set (0.00 sec)
  40. //查看表行数
  41. mysql> select count(*) from studb.salary2;
  42. +----------+
  43. | count(*) |
  44. +----------+
  45. | 0 |
  46. +----------+
  47. 1 row in set (0.00 sec)
  48. mysql>

2 案例2:数据类型

2.1 问题

  1. 练习字符类型的使用
  2. 练习数值类型的使用
  3. 练习枚举类型的使用
  4. 练习日期时间类型的使用

2.2 方案

常用数据类型:数值类型、字符类型、日期时间类型、枚举类型,每种类型都有对应的命令表示、有具体的存储范围。

  • 比如存储: 身高、体重、工资、奖金,适合使用数值类型。
  • 比如存储: 姓名、家庭地址、收货地址,适合使用字符类型。
  • 比如存储: 生日、出生年份、入职时间、下班时间、注册时间,适合使用日期时间。
  • 比如存储: 爱好、性别、社保医院,适合使用枚举类型。

2.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:练习字符类型的使用

命令操作如下所示:

 
  1. //建表
  2. mysql> create table studb.t2(name char(3) , address varchar(5) );
  3. Query OK, 0 rows affected (0.30 sec)
  4. //查看表头
  5. mysql> desc studb.t2;
  6. +---------+------------+------+-----+---------+-------+
  7. | Field | Type | Null | Key | Default | Extra |
  8. +---------+------------+------+-----+---------+-------+
  9. | name | char(3) | YES | | NULL | |
  10. | address | varchar(5) | YES | | NULL | |
  11. +---------+------------+------+-----+---------+-------+
  12. 2 rows in set (0.00 sec)
  13. //插入记录
  14. mysql> insert into studb.t2 values ("a","a"); //正常
  15. Query OK, 1 row affected (0.05 sec)
  16. mysql> insert into studb.t2 values ("ab","ab"); //正常
  17. Query OK, 1 row affected (0.08 sec)
  18. mysql> insert into studb.t2 values ("abc","abc");//正常
  19. Query OK, 1 row affected (0.04 sec)
  20. mysql> insert into studb.t2 values ("abcd","abcd"); //超出字符个数报错
  21. ERROR 1406 (22001): Data too long for column 'name' at row 1
  22. mysql>

mysql8 建表默认支持中文字符集

 
  1. //查看字符集
  2. mysql> show create table studb.t2 \G
  3. *************************** 1. row ***************************
  4. Table: t2
  5. Create Table: CREATE TABLE `t2` (
  6. `name` char(3) DEFAULT NULL,
  7. `address` varchar(5) DEFAULT NULL
  8. ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
  9. 1 row in set (0.00 sec)
  10. 说明 :
  11. ENGINE=InnoDB 定义存储引擎(存储引擎课程里讲)
  12. DEFAULT CHARSET=定义表使用的字符集
  13. //插入记录
  14. mysql> insert into studb.t2 values ("张翠山","武当山");
  15. Query OK, 1 row affected (0.07 sec)
  16. //查看表记录
  17. mysql> SELECT * FROM studb.t2;
  18. +-----------+-----------+
  19. | name | address |
  20. +-----------+-----------+
  21. | a | a |
  22. | ab | ab |
  23. | abc | abc |
  24. | 张翠山 | 武当山 |
  25. +-----------+-----------+
  26. 4 rows in set (0.00 sec)

步骤二:练习数值类型的使用

命令操作如下所示:

 
  1. name 姓名
  2. level 游戏级别
  3. money 游戏币
  4. //建表
  5. mysql> create table studb.t1(name char(10) , level tinyint unsigned , money double );
  6. Query OK, 0 rows affected (0.72 sec)
  7. //查看表头
  8. mysql> desc studb.t1;
  9. +-------+------------------+------+-----+---------+-------+
  10. | Field | Type | Null | Key | Default | Extra |
  11. +-------+------------------+------+-----+---------+-------+
  12. | name | char(10) | YES | | NULL | |
  13. | level | tinyint unsigned | YES | | NULL | |
  14. | money | double | YES | | NULL | |
  15. +-------+------------------+------+-----+---------+-------+
  16. 3 rows in set (0.00 sec)
  17. //插入数据
  18. mysql> insert into studb.t1 values("法师",80,88);
  19. Query OK, 1 row affected (0.04 sec)
  20. //超出范围报错
  21. mysql> insert into studb.t1 values("战士",301,1.292);
  22. ERROR 1264 (22003): Out of range value for column 'level' at row 1
  23. mysql>
  24. mysql> insert into studb.t1 values("猎人",255,1.292);
  25. Query OK, 1 row affected (0.06 sec)
  26. //整数类型 不存储小数位
  27. mysql> insert into studb.t1 values ("英雄",1.292,6.78);
  28. Query OK, 1 row affected (0.07 sec)
  29. //查看表记录
  30. mysql> select * from studb.t1 ;
  31. +--------+-------+-------+
  32. | name | level | money |
  33. +--------+-------+-------+
  34. | 法师 | 80 | 88 |
  35. | 猎人 | 255 | 1.292 |
  36. | 英雄 | 1 | 6.78 |
  37. +--------+-------+-------+
  38. 3 rows in set (0.00 sec)

步骤三:练习枚举类型的使用

 
  1. //建表
  2. mysql> create table studb.t8(
  3. -> 姓名 char(10),
  4. -> 性别 enum("男","女","保密"),
  5. -> 爱好 set("帅哥","金钱","吃","睡")
  6. -> );
  7. Query OK, 0 rows affected (0.29 sec)
  8. //查看表头
  9. mysql> desc studb.t8 ;
  10. +--------+------------------------------------+------+-----+---------+-------+
  11. | Field | Type | Null | Key | Default | Extra |
  12. +--------+------------------------------------+------+-----+---------+-------+
  13. | 姓名 | char(10) | YES | | NULL | |
  14. | 性别 | enum('男','女','保密') | YES | | NULL | |
  15. | 爱好 | set('帅哥','金钱','吃','睡') | YES | | NULL | |
  16. +--------+------------------------------------+------+-----+---------+-------+
  17. 3 rows in set (0.01 sec)
  18. //插入记录超出范围报错
  19. mysql> insert into studb.t8 values ("小包总","男人","帅哥,睡,金钱");
  20. ERROR 1265 (01000): Data truncated for column '性别' at row 1
  21. mysql> insert into studb.t8 values ("小包总","男","美女,睡,金钱");
  22. ERROR 1265 (01000): Data truncated for column '爱好' at row 1
  23. mysql>
  24. //在范围内插入成功
  25. mysql> insert into studb.t8 values ("丫丫","女","帅哥,吃");
  26. Query OK, 1 row affected (0.09 sec)
  27. mysql> select * from studb.t8;
  28. +--------+--------+------------+
  29. | 姓名 | 性别 | 爱好 |
  30. +--------+--------+------------+
  31. | 丫丫 | 女 | 帅哥,吃 |
  32. +--------+--------+------------+
  33. 1 row in set (0.00 sec)

步骤四:练习日期时间类型的使用

命令操作如下所示:

 
  1. //建表
  2. mysql> create table studb.t6(
  3. -> 姓名 char(10),
  4. -> 生日 date ,
  5. -> 出生年份 year ,
  6. -> 家庭聚会 datetime ,
  7. -> 聚会地点 varchar(15),
  8. -> 上班时间 time
  9. -> );
  10. Query OK, 0 rows affected (0.25 sec)
  11. //查看表头
  12. mysql> desc studb.t6 ;
  13. +--------------+-------------+------+-----+---------+-------+
  14. | Field | Type | Null | Key | Default | Extra |
  15. +--------------+-------------+------+-----+---------+-------+
  16. | 姓名 | char(10) | YES | | NULL | |
  17. | 生日 | date | YES | | NULL | |
  18. | 出生年份 | year | YES | | NULL | |
  19. | 家庭聚会 | datetime | YES | | NULL | |
  20. | 聚会地点 | varchar(15) | YES | | NULL | |
  21. | 上班时间 | time | YES | | NULL | |
  22. +--------------+-------------+------+-----+---------+-------+
  23. 6 rows in set (0.00 sec)
  24. //插入表头
  25. mysql> insert into studb.t6
  26. -> values ("翠花",20211120,1990,20220101183000,"天坛校区",090000);
  27. Query OK, 1 row affected (0.05 sec)
  28. //查看表记录
  29. mysql> select * from studb.t6;
  30. +--------+------------+--------------+---------------------+--------------+--------------+
  31. | 姓名 | 生日 | 出生年份 | 家庭聚会 | 聚会地点 | 上班时间 |
  32. +--------+------------+--------------+---------------------+--------------+--------------+
  33. | 翠花 | 2021-11-20 | 1990 | 2022-01-01 18:30:00 | 天坛校区 | 09:00:00 |
  34. +--------+------------+--------------+---------------------+--------------+--------------+
  35. 1 row in set (0.00 sec)

3 案例3:数据批量处理

3.1 问题

  1. 修改检索目录为/myload。
  2. 将/etc/passwd文件导入db1库的user3表里,并添加行号字段。
  3. 将db1库user3表所有记录导出, 存到/myload/user.txt文件里。

3.2 方案

在mysql50主机完成练习。

3.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:修改检索目录为/myload。

检查目录存放导入导出数据时存放数据的文件

 
  1. [root@mysql50 ~]# mysql -uroot -pNSD2023...a
  2. mysql> show variables like "%file%"; 查看与文件相关的配置项
  3. +---------------------------------------+---------------------------------+
  4. | Variable_name | Value |
  5. +---------------------------------------+---------------------------------+
  6. | character_set_filesystem | binary |
  7. | core_file | OFF |
  8. | ft_stopword_file | (built-in) |
  9. | general_log_file | /var/lib/mysql/mysql50.log |
  10. | init_file | |
  11. | innodb_buffer_pool_filename | ib_buffer_pool |
  12. | innodb_buffer_pool_in_core_file | ON |
  13. | innodb_data_file_path | ibdata1:12M:autoextend |
  14. | innodb_disable_sort_file_cache | OFF |
  15. | innodb_doublewrite_files | 2 |
  16. | innodb_file_per_table | ON |
  17. | innodb_log_file_size | 50331648 |
  18. | innodb_log_files_in_group | 2 |
  19. | innodb_open_files | 4000 |
  20. | innodb_temp_data_file_path | ibtmp1:12M:autoextend |
  21. | keep_files_on_create | OFF |
  22. | large_files_support | ON |
  23. | local_infile | OFF |
  24. | lower_case_file_system | OFF |
  25. | myisam_max_sort_file_size | 9223372036853727232 |
  26. | open_files_limit | 10000 |
  27. | performance_schema_max_file_classes | 80 |
  28. | performance_schema_max_file_handles | 32768 |
  29. | performance_schema_max_file_instances | -1 |
  30. | pid_file | /run/mysqld/mysqld.pid |
  31. | relay_log_info_file | relay-log.info |
  32. | secure_file_priv | /var/lib/mysql-files/ |
  33. | slow_query_log_file | /var/lib/mysql/mysql50-slow.log |
  34. +---------------------------------------+---------------------------------+
  35. 28 rows in set (0.00 sec)
  36. 查看默认检索目录
  37. mysql> show variables like "secure_file_priv";
  38. +------------------+-----------------------+
  39. | Variable_name | Value |
  40. +------------------+-----------------------+
  41. | secure_file_priv | /var/lib/mysql-files/ |
  42. +------------------+-----------------------+
  43. 1 row in set (0.00 sec)
  44. mysql> exit
  45. 安装MySQL服务软件时自动创建
  46. [root@mysql50 ~]# ls -ld /var/lib/mysql-files/
  47. drwxr-x--- 2 mysql mysql 6 Sep 22 2021 /var/lib/mysql-files/
  48. [root@mysql50 ~]#
  49. 修改主配置文件
  50. [root@mysql50 ~]# vim /etc/my.cnf.d/mysql-server.cnf
  51. [mysqld]
  52. secure_file_priv=/myload 添加此行
  53. :wq
  54. 创建目录并修改所有者为mysql用户 ,并保证mysql用户对父目录有rx
  55. [root@mysql50 ~]# mkdir /myload
  56. [root@mysql50 ~]# chown mysql /myload
  57. 关闭selinux
  58. root@mysql50 ~]# setenforce 0
  59. setenforce: SELinux is disabled
  60. 重启服务
  61. [root@mysql50 ~]# systemctl restart mysqld
  62. 管理员员登陆查看目录
  63. [root@mysql50 ~]# mysql -uroot -pNSD2023...a
  64. mysql> show variables like "secure_file_priv";
  65. +------------------+----------+
  66. | Variable_name | Value |
  67. +------------------+----------+
  68. | secure_file_priv | /myload/ |
  69. +------------------+----------+
  70. 1 row in set (0.01 sec)

步骤二:将/etc/passwd文件导入db1库的user3表里。

命令操作如下所示:

 
  1. 建库
  2. [root@mysql50 ~]# mysql -uroot -pNSD2023...a
  3. mysql> create database db1;
  4. 建表( 根据导入的文件内容 创建表头)
  5. mysql> create table db1.user3(name varchar(30),password char(1),uid int , gid int , comment varchar(200),homedir varchar(50),shell varchar(30));
  6. Query OK, 0 rows affected (0.41 sec)
  7. 查看表头
  8. mysql> desc db1.user3;
  9. +----------+--------------+------+-----+---------+-------+
  10. | Field | Type | Null | Key | Default | Extra |
  11. +----------+--------------+------+-----+---------+-------+
  12. | name | varchar(30) | YES | | NULL | |
  13. | password | char(1) | YES | | NULL | |
  14. | uid | int | YES | | NULL | |
  15. | gid | int | YES | | NULL | |
  16. | comment | varchar(200) | YES | | NULL | |
  17. | homedir | varchar(50) | YES | | NULL | |
  18. | shell | varchar(30) | YES | | NULL | |
  19. +----------+--------------+------+-----+---------+-------+
  20. 7 rows in set (0.01 sec)
  21. 没有数据
  22. mysql> select * from db1.user3;
  23. Empty set (0.01 sec)
  24. mysql>
  25. 拷贝文件到检索目录 system 在MySQL 里执行系统命令
  26. mysql> system cp /etc/passwd /myload/
  27. mysql> system ls /myload/ 查看文件
  28. passwd
  29. mysql>
  30. 导入数据
  31. mysql> load data infile "/myload/passwd" into table db1.user3 fields terminated by ":" lines terminated by "\n" ;
  32. Query OK, 23 rows affected (0.06 sec)
  33. Records: 23 Deleted: 0 Skipped: 0 Warnings: 0
  34. 查看表记录
  35. mysql> select count(*) from db1.user3;
  36. +----------+
  37. | count(*) |
  38. +----------+
  39. | 23 |
  40. +----------+
  41. 1 row in set (0.00 sec)
  42. mysql> select * from db1.user3;
  43. +------------------+----------+-------+-------+-----------------------------+-----------------+----------------+
  44. | name | password | uid | gid | comment | homedir | shell |
  45. +------------------+----------+-------+-------+-----------------------------+-----------------+----------------+
  46. | root | x | 0 | 0 | root | /root | /bin/bash |
  47. | bin | x | 1 | 1 | bin | /bin | /sbin/nologin |
  48. | daemon | x | 2 | 2 | daemon | /sbin | /sbin/nologin |
  49. | adm | x | 3 | 4 | adm | /var/adm | /sbin/nologin |
  50. | lp | x | 4 | 7 | lp | /var/spool/lpd | /sbin/nologin |
  51. | sync | x | 5 | 0 | sync | /sbin | /bin/sync |
  52. | shutdown | x | 6 | 0 | shutdown | /sbin | /sbin/shutdown |
  53. | halt | x | 7 | 0 | halt | /sbin | /sbin/halt |
  54. | mail | x | 8 | 12 | mail | /var/spool/mail | /sbin/nologin |
  55. | operator | x | 11 | 0 | operator | /root | /sbin/nologin |
  56. | games | x | 12 | 100 | games | /usr/games | /sbin/nologin |
  57. | ftp | x | 14 | 50 | FTP User | /var/ftp | /sbin/nologin |
  58. | nobody | x | 65534 | 65534 | Kernel Overflow User | / | /sbin/nologin |
  59. | dbus | x | 81 | 81 | System message bus | / | /sbin/nologin |
  60. | systemd-coredump | x | 999 | 997 | systemd Core Dumper | / | /sbin/nologin |
  61. | systemd-resolve | x | 193 | 193 | systemd Resolver | / | /sbin/nologin |
  62. | polkitd | x | 998 | 995 | User for polkitd | / | /sbin/nologin |
  63. | unbound | x | 997 | 994 | Unbound DNS resolver | /etc/unbound | /sbin/nologin |
  64. | tss | x | 59 | 59 | Account used for TPM access | /dev/null | /sbin/nologin |
  65. | chrony | x | 996 | 993 | | /var/lib/chrony | /sbin/nologin |
  66. | sshd | x | 74 | 74 | Privilege-separated SSH | /var/empty/sshd | /sbin/nologin |
  67. | tcpdump | x | 72 | 72 | | / | /sbin/nologin |
  68. | mysql | x | 27 | 27 | MySQL Server | /var/lib/mysql | /sbin/nologin |
  69. +------------------+----------+-------+-------+-----------------------------+-----------------+----------------+
  70. 23 rows in set (0.00 sec)
  71. mysql>

步骤三:将db1库user3表所有记录导出, 存到/myload/user.txt文件里。

命令操作如下所示:

 
  1. mysql> select * from db1.user3 into outfile "/myload/user.txt" ;
  2. Query OK, 23 rows affected (0.00 sec)
  3. mysql> system ls /myload/
  4. passwd user.txt
  5. mysql> system wc -l /myload/user.txt
  6. 23 /myload/user.txt
  7. mysql>
  8. mysql> system vim /myload/user.txt
  9. root x 0 0 root /root /bin/bash
  10. bin x 1 1 bin /bin /sbin/nologin
  11. daemon x 2 2 daemon /sbin /sbin/nologin
  12. adm x 3 4 adm /var/adm /sbin/nologin
  13. lp x 4 7 lp /var/spool/lpd /sbin/nologin
  14. sync x 5 0 sync /sbin /bin/sync
  15. shutdown x 6 0 shutdown /sbin /sbin/shutdown
  16. halt x 7 0 halt /sbin /sbin/halt
  17. mail x 8 12 mail /var/spool/mail /sbin/nologin
  18. operator x 11 0 operator /root /sbin/nologin
  19. games x 12 100 games /usr/games /sbin/nologin
  20. ftp x 14 50 FTP User /var/ftp /sbin/nologin
  21. nobody x 65534 65534 Kernel Overflow User / /sbin/nologin
  22. dbus x 81 81 System message bus / /sbin/nologin
  23. systemd-coredump x 999 997 systemd Core Dumper / /sbin/nologin
  24. systemd-resolve x 193 193 systemd Resolver / /sbin/nologin
  25. polkitd x 998 995 User for polkitd / /sbin/nologin
  26. unbound x 997 994 Unbound DNS resolver /etc/unbound /sbin/nologin
  27. tss x 59 59 Account used for TPM access /dev/null /sbin/nologin
  28. chrony x 996 993 /var/lib/chrony /sbin/nologin
  29. sshd x 74 74 Privilege-separated SSH /var/empty/sshd /sbin/nologin
  30. tcpdump x 72 72 / /sbin/nologin
  31. mysql x 27 27 MySQL Server /var/lib/mysql /sbin/nologin

4 案例4:表头基本约束

4.1 问题

  1. 表头不允许赋null值练习
  2. 表头加默认值练习
  3. 表头加唯一索引练习

4.2 方案

约束是一种限制,设置在表头上,用来控制表头的赋值,包括以下几种:

  1. NOT NULL :非空,用于保证该字段的值不能为空。
  2. DEFAULT:默认值,用于保证该字段有默认值。
  3. UNIQUE:唯一索引,用于保证该字段的值具有唯一性,可以为空。
  4. PRIMARY KEY:主键,用于保证该字段的值具有唯一性并且非空。
  5. FOREIGN KEY:外键,用于限制两个表的关系,用于保证该字段的值必须来自于主表的关联列的值,在从表添加外键约束,用于引用主表中某些的值。

4.3 步骤

实现此案例需要按照如下步骤进行。

步骤一:表头不允许赋空值练习

 
  1. //建表时给表头设置默认和不允许赋null值    
  2. mysql> create database if not exists db1;
  3. Query OK, 1 row affected (0.07 sec)
  4. //建表
  5. mysql> create table db1.t31(
  6. -> name char(10) not null ,
  7. -> class char(7) default "nsd",
  8. -> likes set("money","game","film","music") not null default "film,music" );
  9. Query OK, 0 rows affected (0.43 sec)
  10. //查看表头
  11. mysql> desc db1.t31;
  12. +-------+------------------------------------+------+-----+------------+-------+
  13. | Field | Type | Null | Key | Default | Extra |
  14. +-------+------------------------------------+------+-----+------------+-------+
  15. | name | char(10) | NO | | NULL | |
  16. | class | char(7) | YES | | nsd | |
  17. | likes | set('money','game','film','music') | NO | | film,music | |
  18. +-------+------------------------------------+------+-----+------------+-------+
  19. 3 rows in set (0.01 sec)
  20. //验证默认值和不允许为null
  21. mysql> insert into db1.t31 values (null, null , null);
  22. ERROR 1048 (23000): Column 'name' cannot be null //表头name赋null值 报错
  23. //表头likes赋null值 报错
  24. mysql> insert into db1.t31 values ("bob", null , null);
  25. ERROR 1048 (23000): Column 'likes' cannot be null
  26. //符合约束不报错
  27. mysql> insert into db1.t31 values ("bob",null,"money,game,film");
  28. Query OK, 1 row affected (0.06 sec)
  29. //不赋值的表头使用默认值赋值
  30. mysql> insert into db1.t31(name) values("jim");
  31. //根据需要自定义表头的值
  32. mysql> insert into db1.t31 values ("lucy","nsd2108","game,film");
  33. //查看表记录
  34. mysql> select * from db1.t31;
  35. +------+---------+-----------------+
  36. | name | class | likes |
  37. +------+---------+-----------------+
  38. | bob | NULL | money,game,film |
  39. | jim | nsd | film,music |
  40. | lucy | nsd2108 | game,film |
  41. +------+---------+-----------------+
  42. 3 rows in set (0.00 sec)

步骤二:表头加唯一索引练习

唯一索引 (unique)

约束的方式:表头值唯一 , 但可以赋null 值

 
  1. //建表
  2. create      table db1.t43 (姓名 char(10) , 护照 char(18) unique );
  3. //查看表头 唯一索引标志UNI
  4. mysql> desc db1.t32 ;
  5. +--------+----------+------+-----+---------+-------+
  6. | Field | Type | Null | Key | Default | Extra |
  7. +--------+----------+------+-----+---------+-------+
  8. | 姓名 | char(10) | YES | | NULL | |
  9. | 护照 | char(18) | YES | UNI | NULL | |
  10. +--------+----------+------+-----+---------+-------+
  11. 2 rows in set (0.00 sec)
  12. //赋null值 可以
  13. mysql> insert into db1.t32 values("bob",null);
  14. Query OK, 1 row affected (0.07 sec)
  15. //表头值重复不可以
  16. mysql> insert into db1.t32 values("tom","666888");
  17. Query OK, 1 row affected (0.08 sec)
  18. mysql> insert into db1.t32 values("jim","666888");
  19. ERROR 1062 (23000): Duplicate entry '666888' for key 't32.护照'
  20. //不重复 可以
  21. mysql> insert into db1.t32 values("jim","766888");
  22. Query OK, 1 row affected (0.05 sec)
  23. //查看表记录
  24. mysql> select * from DB1.t43;
  25. +------+--------+
  26. | 姓名 | 护照 |
  27. +------+--------+
  28. | bob | NULL |
  29. | tom | 666888 |
  30. | jim | 766888 |
  31. +------+--------+
  32. 3 rows in set (0.00 sec)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值