库:存储表的文件夹
mysql> SHOW CREATE DATABASE tarena \G
*************************** 1. row ***************************
Database: tarena
Create Database: CREATE DATABASE `tarena` /*!40100 DEFAULT CHARACTER SET latin1 */ /*!80016 DEFAULT ENCRYPTION='N' */
1 row in set (0.00 sec)
create database [if not exists] <数据库名> [[DEFAULT] CHARACTER SET <字符集名>] [[DEFAULT] COLLATE <校对规则名>];
[ ] 表示可选项
if not exists 假设库不存在 避免建库重名报错
character set 指定表使用的字符集(存储字符串的方式)
collate 指定校对规则(比较字符串的方式)
查看已有的库 show databases;
切换库 use 库名;
删除库 drop database 库名; (drop database if exists 库名; )
创建库 create database 库名;
所在库 select database();
仅可以使用数字、字母、下划线,不能纯数字
区分大小写
具有唯一性
不可使用MySQL命令或特殊字符
就是创建存储数据的表头名
create table 库名.表名(
表头名 数据类型,
表头名 数据类型,
......
);
删除表 drop table 库名.表名;
删除表记录 delete from 库名.表名;
修改表记录 update 库名.表名 set 表头名="表头新值" where;
插入表记录 insert into 表名 values(表头的值,...,...),( )...;
insert into 库.表(表头名,...) values(表头的值,...);
alter table 库名.表名 执行动作;
添加新字段,一起添加多个字段使用‘ ,’分隔 add 命令(first,after)
alter table 库名.表名 add 表头名 数据类型;
mysql> alter table user add email char(30);
mysql> select * from user limit 1;
+----+--------+----------+------+------+---------+----------------+---------------+-------+
| id | name | password | uid | gid | comment | homedir | shell | email |
+----+--------+----------+------+------+---------+----------------+---------------+-------+
| 1 | root | x | 0 | 0 | root | /root | /bin/bash | NULL |
mysql> alter table user add sex char(3),add banji char(7);
mysql> select * from user limit 1;
+----+--------+----------+------+------+---------+----------------+---------------+-------+------+-------+
| id | name | password | uid | gid | comment | homedir | shell | email | sex | banji |
+----+--------+----------+------+------+---------+----------------+---------------+-------+------+-------+
| 1 | root | x | 0 | 0 | root | /root | /bin/bash | NULL | NULL | NULL |
mysql> alter table user add school char(10) first;
mysql> select * from user limit 1;
+--------+----+--------+----------+------+------+---------+----------------+---------------+-------+------+-------+
| school | id | name | password | uid | gid | comment | homedir | shell | email | sex | banji |
+--------+----+--------+----------+------+------+---------+----------------+---------------+-------+------+-------+
| NULL | 1 | root | x | 0 | 0 | root | /root | /bin/bash | NULL | NULL | NULL |
mysql> alter table user add shenfenzheng char(18) after name;
mysql> select * from user limit 1;
+----+------+--------------+----------+---------+---------+-----------+-------+------+-------+
| id | name | shenfenzheng | password | comment | homedir | shell | email | sex | banji |
+----+------+--------------+----------+---------+---------+-----------+-------+------+-------+
| 1 | root | NULL | x | root | /root | /bin/bash | NULL | NULL | NULL |
+----+------+--------------+----------+---------+---------+-----------+-------+------+-------+
删除字段,删除多个字段使用‘ ,’分隔drop命令
alter table 库名.表名 drop 表头名;
mysql> alter table user drop school;
mysql> select * from user limit 1;
+----+------+----------+------+------+---------+---------+-----------+-------+------+-------+
| id | name | password | uid | gid | comment | homedir | shell | email | sex | banji |
+----+------+----------+------+------+---------+---------+-----------+-------+------+-------+
| 1 | root | x | 0 | 0 | root | /root | /bin/bash | NULL | NULL | NULL |
+----+------+----------+------+------+---------+---------+-----------+-------+------+-------+
mysql> alter table user drop uid,drop gid;
mysql> select * from user limit 1;
+----+------+----------+---------+---------+-----------+-------+------+-------+
| id | name | password | comment | homedir | shell | email | sex | banji |
+----+------+----------+---------+---------+-----------+-------+------+-------+
| 1 | root | x | root | /root | /bin/bash | NULL | NULL | NULL |
+----+------+----------+---------+---------+-----------+-------+------+-------+
alter table 库名.表名 rename 库名.新表名;
mysql> show tables;
+------------------+
| Tables_in_tarena |
+------------------+
......
| wage_grade |
+------------------+
5 rows in set (0.00 sec)
mysql> alter table wage_grade rename 工资表级表;
mysql> show tables;
+------------------+
| Tables_in_tarena |
+------------------+
......
| 工资表级表 |
+------------------+
5 rows in set (0.00 sec)
修改字段名,也可以同时修改字段类型
alter table 库名.表名 change 原表头名 新表头名 数据类型;
mysql> desc 工资表级表;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| grade | char(1) | YES | | NULL | |
| low | int | YES | | NULL | |
| high | int | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> alter table 工资表级表 change low 最低值 int;
mysql> desc 工资表级表;
+-----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| grade | char(1) | YES | | NULL | |
| 最低值 | int | YES | | NULL | |
| high | int | YES | | NULL | |
+-----------+---------+------+-----+---------+-------+
4 rows in set (0.00 sec)
修改字段类型,也可修改字段的位置
alter table 库名.表名 modify 表头名 数据类型 after 表头名;
mysql> alter table tarena.工资表级表 modify high int after grade;
mysql> select * from tarena.工资表级表;
+------+-------+---------+-----------+
| id | grade | high | 最低值 |
+------+-------+---------+-----------+
| 1 | A | 8000 | 5000 |
| 2 | B | 10000 | 8001 |
| 3 | C | 15000 | 10001 |
| 4 | D | 20000 | 15001 |
| 5 | E | 1000000 | 20001 |
+------+-------+---------+-----------+
5 rows in set (0.00 sec)
alter table 库名.表名 modify 表头名 新数据类型;
如果表头下没有存储数据,类型随便改;反之 修改的新类型不能与已经存储的数据冲突,冲突的话不许修改
mysql> desc user;
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+----------------+
......
| name | char(20) | YES | | NULL | |
......
+--------------+-------------+------+-----+---------+----------------+
10 rows in set (0.01 sec)
mysql> alter table user modify name varchar(3);
ERROR 1406 (22001): Data too long for column 'name' at row 1
mysql> alter table user modify name varchar(12);
ERROR 1406 (22001): Data too long for column 'name' at row 14
mysql> alter table user modify name varchar(15);
Query OK, 27 rows affected (0.71 sec)
Records: 27 Duplicates: 0 Warnings: 0
mysql> desc user; # 此时 shenfenzheng 表头值为空
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+----------------+
......
| shenfenzheng | char(18) | YES | | NULL | |
......
+--------------+-------------+------+-----+---------+----------------+
10 rows in set (0.00 sec)
mysql> alter table user modify shenfenzheng char(6);
Query OK, 27 rows affected (1.02 sec)
Records: 27 Duplicates: 0 Warnings: 0
和系统的cp命令功能一样
表由表头和数据组成
create table 备份到哪个库.备份到哪个表 like 库名.原表;
mysql> create database db1;
mysql> create table db1.wage_garde like tarena.工资表级表;
mysql> use db1;
mysql> show tables;
+---------------+
| Tables_in_db1 |
+---------------+
| wage_garde |
+---------------+
1 row in set (0.00 sec)
mysql> desc wage_garde;
+-----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| grade | char(1) | YES | | NULL | |
| 最低值 | int | YES | | NULL | |
| high | int | YES | | NULL | |
+-----------+---------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> select * from wage_garde;
Empty set (0.00 sec)
mysql> create table db1.gzdj2 select * from tarena.工资表级表;
mysql> show tables
-> ;
+---------------+
| Tables_in_db1 |
+---------------+
| gzdj2 |
| wage_garde |
+---------------+
2 rows in set (0.01 sec)
mysql> desc gzdj2;
+-----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+---------+------+-----+---------+-------+
| id | int | YES | | NULL | |
| grade | char(1) | YES | | NULL | |
| 最低值 | int | YES | | NULL | |
| high | int | YES | | NULL | |
+-----------+---------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> select * from gzdj2;
+------+-------+-----------+---------+
| id | grade | 最低值 | high |
+------+-------+-----------+---------+
| 1 | A | 5000 | 8000 |
| 2 | B | 8001 | 10000 |
| 3 | C | 10001 | 15000 |
| 4 | D | 15001 | 20000 |
| 5 | E | 20001 | 1000000 |
+------+-------+-----------+---------+
5 rows in set (0.00 sec)
# 仅备份符合条件的数据
mysql> select employee_id,basic from tarena.salary where year(date)=2018 and month(date)=7 and basic > 15000;
+-------------+-------+
| employee_id | basic |
+-------------+-------+
......
62 rows in set (0.00 sec)
mysql> create table db1.salary7 select employee_id,basic from tarena.salary where year(date)=2018 and month(date)=7 and basic > 15000;
mysql> select * from salary7;
+-------------+-------+
| employee_id | basic |
+-------------+-------+
......
62 rows in set (0.00 sec)
每种数据类型使用固定的命令表示且由固定的存储数据范围
存储数字的表头使用数值类型
只存储数值的整数部分(遇小数四舍五入)
既存整数又能存储有小数点的数
mysql> create table db1.t12(
-> 人物 varchar(10),
-> 等级 tinyint unsigned,
-> 点劵 float
-> );
Query OK, 0 rows affected (0.33 sec)
mysql> insert into db1.t12 values("法师",-1,-98);
ERROR 1264 (22003): Out of range value for column '等级' at row 1
mysql> insert into db1.t12 values("法师",0,-98);
Query OK, 1 row affected (0.04 sec)
mysql> insert into db1.t12 values("战士",256,12.25);
ERROR 1264 (22003): Out of range value for column '等级' at row 1
mysql> insert into db1.t12 values("战士",25,12.25);
Query OK, 1 row affected (0.10 sec)
mysql> select * from db1.t12;
+--------+--------+--------+
| 人物 | 等级 | 点劵 |
+--------+--------+--------+
| 法师 | 0 | -98 |
| 战士 | 25 | 12.25 |
+--------+--------+--------+
2 rows in set (0.00 sec)
mysql> insert into db1.t12 values("士兵",19.25,12.25);
Query OK, 1 row affected (0.05 sec)
mysql> select * from db1.t12;
+--------+--------+--------+
| 人物 | 等级 | 点劵 |
+--------+--------+--------+
| 法师 | 0 | -98 |
| 战士 | 25 | 12.25 |
| 士兵 | 19 | 12.25 |
+--------+--------+--------+
3 rows in set (0.00 sec)
mysql> insert into db1.t12 values("士兵2",19.56,12.25);
Query OK, 1 row affected (0.05 sec)
mysql> select * from db1.t12;
+---------+--------+--------+
| 人物 | 等级 | 点劵 |
+---------+--------+--------+
| 法师 | 0 | -98 |
| 战士 | 25 | 12.25 |
| 士兵 | 19 | 12.25 |
| 士兵2 | 20 | 12.25 |
+---------+--------+--------+
4 rows in set (0.00 sec)
定长字符类型 最多255个字符
不够指定字符个数时在右边用空格补全字符个数,超出时无法写入数据
变长字符类型 最多65532个字符
按数据实际大小分配存储空间,字符个数超出时无法写入数据
mysql> create table db1.t11(
-> 姓名 char(4),
-> 住址 varchar(20)
-> );
Query OK, 0 rows affected (0.58 sec)
mysql> insert into db1.t11 values("牛牛","天桥下");
Query OK, 1 row affected (0.05 sec)
mysql> insert into db1.t11 values("牛牛爱锤子","天桥下");
ERROR 1406 (22001): Data too long for column '姓名' at row 1
mysql> select * from db1.t11;
+--------+-----------+
| 姓名 | 住址 |
+--------+-----------+
| 牛牛 | 天桥下 |
+--------+-----------+
1 row in set (0.00 sec)
mysql> desc db1.t11;
+--------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+-------------+------+-----+---------+-------+
| 姓名 | char(4) | YES | | NULL | |
| 住址 | varchar(20) | YES | | NULL | |
+--------+-------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
性别、爱好
表头的值必须在定义的范围内选择
单选 enum(值列表)
多选 set(值列表)
mysql> create table db1.t13( 姓名 char(10),性别 enum("男","女"),爱好 set("吃","睡","玩","钱") );
Query OK, 0 rows affected (0.56 sec)
mysql> desc db1.t13;
+--------+------------------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+------------------------------+------+-----+---------+-------+
| 姓名 | char(10) | YES | | NULL | |
| 性别 | enum('男','女') | YES | | NULL | |
| 爱好 | set('吃','睡','玩','钱') | YES | | NULL | |
+--------+------------------------------+------+-----+---------+-------+
3 rows in set (0.01 sec)
mysql> insert into db1.t13 values("a","男","吃,睡");
Query OK, 1 row affected (0.06 sec)
mysql> insert into db1.t13 values("b","公公","it,学习");
ERROR 1265 (01000): Data truncated for column '性别' at row 1
mysql> insert into db1.t13 values("b","女","it,学习");
ERROR 1265 (01000): Data truncated for column '爱好' at row 1
mysql> insert into db1.t13 values("b","女","玩");
Query OK, 1 row affected (0.05 sec)
mysql> select * from db1.t13;
+--------+--------+---------+
| 姓名 | 性别 | 爱好 |
+--------+--------+---------+
| a | 男 | 吃,睡 |
| b | 女 | 玩 |
+--------+--------+---------+
2 rows in set (0.00 sec)
类型 | 名称 | 范围 | 赋值格式 | 20(0-69) 19(70-99) |
year | 年 | 1901~2155 | YYYY | |
date | 日期 | 0001-01-01~9999-12-31 | YYYYMMDD | |
time | 时间 | 01:00:00~23:59:59 | HHMMSS | |
datetime | 日期时间 | 1000-01-01 00:00:00~9999-12-31 23:59:59 | YYYYMMDDHHMMSS | |
timestamp | 1970-01-01 00:00:00~2038-01-19 00:00:00 |
mysql> create table db1.byb( 姓名 char(4), 班级 char(7), 出生年份 year, 生日 date, 上课时间 time, 双选会时间 datetime, 结业聚餐 timestamp );
Query OK, 0 rows affected (0.82 sec)
mysql> desc byb;
+-----------------+-----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-----------+------+-----+---------+-------+
| 姓名 | char(4) | YES | | NULL | |
| 班级 | char(7) | YES | | NULL | |
| 出生年份 | year | YES | | NULL | |
| 生日 | date | YES | | NULL | |
| 上课时间 | time | YES | | NULL | |
| 双选会时间 | datetime | YES | | NULL | |
| 结业聚餐 | timestamp | YES | | NULL | |
+-----------------+-----------+------+-----+---------+-------+
7 rows in set (0.00 sec)
mysql> insert into byb values("牛牛","nsd2310",1990,19901120,090000,20240228083000,20240215203000);
Query OK, 1 row affected (0.06 sec)
mysql> select * from byb;
+--------+---------+--------------+------------+--------------+---------------------+---------------------+
| 姓名 | 班级 | 出生年份 | 生日 | 上课时间 | 双选会时间 | 结业聚餐 |
+--------+---------+--------------+------------+--------------+---------------------+---------------------+
| 牛牛 | nsd2310 | 1990 | 1990-11-20 | 09:00:00 | 2024-02-28 08:30:00 | 2024-02-15 20:30:00 |
+--------+---------+--------------+------------+--------------+---------------------+---------------------+
1 row in set (0.00 sec)
mysql> insert into byb(姓名,出生年份) values("a",69),("b",70),("c",81);
Query OK, 3 rows affected (0.06 sec)
Records: 3 Duplicates: 0 Warnings: 0
mysql> select * from byb;
+--------+---------+--------------+------------+--------------+---------------------+---------------------+
| 姓名 | 班级 | 出生年份 | 生日 | 上课时间 | 双选会时间 | 结业聚餐 |
+--------+---------+--------------+------------+--------------+---------------------+---------------------+
| 牛牛 | nsd2310 | 1990 | 1990-11-20 | 09:00:00 | 2024-02-28 08:30:00 | 2024-02-15 20:30:00 |
| a | NULL | 2069 | NULL | NULL | NULL | NULL |
| b | NULL | 1970 | NULL | NULL | NULL | NULL |
| c | NULL | 1981 | NULL | NULL | NULL | NULL |
+--------+---------+--------------+------------+--------------+---------------------+---------------------+
4 rows in set (0.00 sec)
存储数据的文件必须存放在检索目录里
show variables;
show variables like "%关键字%";
比如 show variables like "%file%";
show variables like "%password%";
show variables like "secure_file_priv";
mysql> show variables like "secure_file_priv";
+------------------+-----------------------+
| Variable_name | Value |
+------------------+-----------------------+
| secure_file_priv | /var/lib/mysql-files/ |
+------------------+-----------------------+
1 row in set (0.01 sec)
[root@mysql50 ~]# ll -d /var/lib/mysql-files/
drwxr-x--- 2 mysql mysql 6 Sep 22 2021 /var/lib/mysql-files/
[root@mysql50 ~]# ls /var/lib/mysql-files/ #空目录
[root@mysql50 ~]# mkdir /myload
[root@mysql50 ~]# chown mysql /myload/
[root@mysql50 ~]# getenforce # 模式不能为 enforcing
Disabled
[root@mysql50 ~]# vim /etc/my.cnf.d/mysql-server.cnf
[mysqld]
......
secure_file_priv=/myload # 添加此行
[root@mysql50 ~]# systemctl restart mysqld.service
# 查看验证
mysql> show variables like "secure_file_priv";
+------------------+----------+
| Variable_name | Value |
+------------------+----------+
| secure_file_priv | /myload/ |
+------------------+----------+
1 row in set (0.01 sec)
mysql用户对 检索目录的父目录 必须有 r-x 权限
把系统文件的内容,存储到数据库的表里
load data infile "/myload/文件名" into table 库.表 field terminated by "文件里列的间隔符号" lines terminated by "\n";
步骤:
创建数据库
mysql> create database db3;
创建表(根据文件的内容创建表头和数据类型)
mysql> create table db3.user(
-> name varchar(20),
-> password char(1),
-> uid int,gid int,
-> comment varchar(200),
-> homedir varchar(30),
-> shell varchar(30)
-> );
mysql> use db3;
mysql> show tables;
+---------------+
| Tables_in_db3 |
+---------------+
| user |
+---------------+
1 row in set (0.00 sec)
mysql> desc user;
+----------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+--------------+------+-----+---------+-------+
| name | varchar(20) | YES | | NULL | |
| password | char(1) | YES | | NULL | |
| uid | int | YES | | NULL | |
| gid | int | YES | | NULL | |
| comment | varchar(200) | YES | | NULL | |
| homedir | varchar(30) | YES | | NULL | |
| shell | varchar(30) | YES | | NULL | |
+----------+--------------+------+-----+---------+-------+
7 rows in set (0.00 sec)
mysql> select * from user;
Empty set (0.00 sec)
把文件拷贝到检索目录里
mysql> system cp /etc/passwd /myload
mysql> system ls /myload
passwd
指定导入数据的命令
mysql> system wc -l /myload/passwd
24 /myload/passwd
mysql> load data infile "/myload/passwd" into table db3.user fields terminated by ":" lines terminated by "\n";
查看表里的行
mysql> select count(*) from user;
+----------+
| count(*) |
+----------+
| 24 |
+----------+
1 row in set (0.00 sec)
把表里的行,存放到系统文件里
select查询命令 into outfile "/检索目录/文件名";
mysql> select * from tarena.salary where year(date)=2018 and employee_id=6;
mysql> select * from tarena.salary where year(date)=2018 and employee_id=6 into outfile "/myload/salary.txt";
mysql> system vim /myload/salary.txt
改变列和列的间隔符号
select查询命令 into outfile "/检索目录/文件名" fields terminated "符号";
mysql> select employee_id,basic,bonus from tarena.salary where year(date)=2018 and employee_id=6 into outfile "/myload/salary_2.txt" fields terminated by ":";
约束设置在表头上 作用是如何给表头赋值
desc 库.表;
Null | Key | Default | Extra
空 键值 默认值 额外设置
建表时设置约束
create table db3.t1( name char(3) not null,age int default 19,class char(7) default "nsd2310";
create table db3.t3( number int,name char(3) not null,age int default 19,class char(7) default "nsd2310" unique(number);
mysql> create table db3.t6(
-> number int,
-> name char(3),
-> age int,
-> class char(7)
-> );
Query OK, 0 rows affected (0.32 sec)
mysql> desc db3.t6;
+--------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------+------+-----+---------+-------+
| number | int | YES | | NULL | |
| name | char(3) | YES | | NULL | |
| age | int | YES | | NULL | |
| class | char(7) | YES | | NULL | |
+--------+---------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> alter table db3.t6 modify name char(3) not null;
Query OK, 0 rows affected (0.55 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> alter table db3.t6 modify age int not null default 19;;
Query OK, 0 rows affected (0.81 sec)
Records: 0 Duplicates: 0 Warnings: 0
ERROR:
No query specified
mysql> create unique index a on db3.t6(number); # 设置唯一索引
Query OK, 0 rows affected (0.18 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc db3.t6;
+--------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------+---------+------+-----+---------+-------+
| number | int | YES | UNI | NULL | |
| name | char(3) | NO | | NULL | |
| age | int | NO | | 19 | |
| class | char(7) | YES | | NULL | |
+--------+---------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> show index from db3.t6\G # 查看索引
*************************** 1. row ***************************
Table: t6
Non_unique: 0
Key_name: a
Seq_in_index: 1
Column_name: number
Collation: A
Cardinality: 0
Sub_part: NULL
Packed: NULL
Null: YES
Index_type: BTREE
Comment:
Index_comment:
Visible: YES
Expression: NULL
1 row in set (0.05 sec)
mysql> drop index a on db3.t6; # 删除索引
Query OK, 0 rows affected (0.19 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> show index from db3.t6\G
Empty set (0.00 sec)