标签(空格分隔): 未分类
在MySQL管理软件中,DDL已经定义了数据库结构。
那么如何对其中的数据进行管理呢?
可以通过SQL语句中的DML语言来实现数据的操作,包括使用
1.语法
insert into 表名 VALUES(值1,值2,值3…值n);
2.示例:
mysql> insert into student1 values (4,"laoliu","f",22);
Query OK, 1 row affected (0.00 sec)
查看所有数据
mysql> select * from student1;
+------+----------+------+------+
| id | name | sex | age |
+------+----------+------+------+
| 1 | zhangsan | m | 33 |
| 2 | lisi | f | 23 |
| 3 | wangwu | f | 20 |
| 4 | laoliu | f | 22 |
+------+----------+------+------+
4 rows in set (0.00 sec)
1.语法
insert into 表名(列名,列名) VALUES (值1,值2);
2.示例
mysql> insert into student1 (id) values (5);
Query OK, 1 row affected (0.00 sec)
mysql> insert into student1 (id,name) values (6,"yunjisuan");
Query OK, 1 row affected (0.01 sec)
mysql> insert into student1 (id,name,sex) values (7,"liuzi","m");
Query OK, 1 row affected (0.01 sec)
查看表所有数据
mysql> select * from student1;
+------+-----------+------+------+
| id | name | sex | age |
+------+-----------+------+------+
| 1 | zhangsan | m | 33 |
| 2 | lisi | f | 23 |
| 3 | wangwu | f | 20 |
| 4 | laoliu | f | 22 |
| 5 | NULL | NULL | NULL |
| 6 | yunjisuan | NULL | NULL |
| 7 | liuzi | m | NULL |
+------+-----------+------+------+
7 rows in set (0.00 sec)
1.语法
update 表名 set 列名=值 where condition;
2.示例
mysql> update student1 set name='kk' where id=5;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
查看表
mysql> select * from student1;
+------+-----------+------+------+
| id | name | sex | age |
+------+-----------+------+------+
| 1 | zhangsan | m | 33 |
| 2 | lisi | f | 23 |
| 3 | laoliu | f | 20 |
| 4 | laoliu | f | 22 |
| 5 | kk | NULL | NULL |
| 6 | yunjisuan | NULL | NULL |
| 7 | liuzi | m | NULL |
+------+-----------+------+------+
7 rows in set (0.00 sec)
2.2示例二
修改mysql数据库管理员root账户的密码。
mysql> update mysql.user set authentication_string=password('XuShengKai@1234') where user="root";
Query OK, 1 row affected, 1 warning (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 1
缓存
mysql> \q
Bye
更新,此时密码已经修改完成
[root@192 ~]# mysql -uroot -p'XuShengKai@123'
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
更新完成后再次登录,登录密码就是修改过的密码
[root@192 ~]# mysql -uroot -p'XuShengKai@1234'
mysql: [Warning] Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 13
Server version: 5.7.36 MySQL Community Server (GPL)
Copyright (c) 2000, 2021, Oracle and/or its affiliates.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>
1.语法
delete from 表名 where condition;
如果不加where,整个表将会被删除
2.示例
mysql> delete from student1 where name='laoliu';
Query OK, 2 rows affected (0.00 sec)
查看结果
mysql> select * from student1;
+------+-----------+------+------+
| id | name | sex | age |
+------+-----------+------+------+
| 1 | zhangsan | m | 33 |
| 2 | lisi | f | 23 |
| 5 | kk | NULL | NULL |
| 6 | yunjisuan | NULL | NULL |
| 7 | liuzi | m | NULL |
+------+-----------+------+------+
5 rows in set (0.00 sec)
在MySQL管理软件中,可以通过SQL语句中的DQL语言来实现数据的SELECT 查询操作
互联网用户查询余额,查询装备,查询商品的操作。
id int 序号
name varchar 姓名
age int 年龄
mysql> create table t3 (id int,name varchar(20),age int);
Query OK, 0 rows affected (0.00 sec)
1.插入数据
mysql> insert into t3 values (1,"zhangsan",23);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t3 values (2,"lisi",24);
Query OK, 1 row affected (0.00 sec)
mysql> insert into t3 values (3,"wangwu",18);
Query OK, 1 row affected (0.00 sec)
查看结果
mysql> select * from t3;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 1 | zhangsan | 23 |
| 2 | lisi | 24 |
| 3 | wangwu | 18 |
+------+----------+------+
3 rows in set (0.01 sec)
1.创建表
mysql> create database company;
Query OK, 1 row affected (0.01 sec)
2.创建数据表
mysql> CREATE TABLE company.employee5(
-> id int primary key AUTO_INCREMENT not null,
-> name varchar(30) not null,
-> sex enum('male','female') default 'male' not null,
-> hire_date date not null,
-> post varchar(50) not null,
-> job_description varchar(100),
-> salary double(15,2) not null,
-> office int,
-> dep_id int
-> );
Query OK, 0 rows affected (0.00 sec)
格式这个样子是因为数据库的语言,把每一列都敲成一行,看起来舒服一点
3.插入数据
mysql> insert into company.employee5(name,sex,hire_date,post,job_description,salary,office,dep_id) values
-> ('jack','male','20180202','instructor','teach',5000,501,100),
-> ('tom','male','20180203','instructor','teach',5500,501,100),
-> ('robin','male','20180202','instructor','teach',8000,501,100),
-> ('alice','female','20180202','instructor','teach',7200,501,100),
-> ('aofa','male','20180202','hr','hrcc',600,502,101),
-> ('harry','male','20180202','hr',NULL,6000,502,101),
-> ('emma','female','20180206','sale','salecc',20000,503,102),
-> ('christine','female','20180205','sale','salecc',2200,503,102),
-> ('zhuzhu','male','20180205','sale',NULL,2200,503,102),
-> ('gougou','male','20180205','sale','',2200,503,102);
Query OK, 10 rows affected (0.00 sec)
Records: 10 Duplicates: 0 Warnings: 0
查看所有列,前提是需要进入数据库。不进入数据库,就请输入库名。
语法:SELECT * FROM 表名;
查部分列
语法:SELECT 列1,列2,列3 FROM 表名;
1.查看所有列
mysql> select * from company.employee5;
+----+-----------+--------+------------+------------+-----------------+----------+--------+--------+
| id | name | sex | hire_date | post | job_description | salary | office | dep_id |
+----+-----------+--------+------------+------------+-----------------+----------+--------+--------+
| 1 | jack | male | 2018-02-02 | instructor | teach | 5000.00 | 501 | 100 |
| 2 | tom | male | 2018-02-03 | instructor | teach | 5500.00 | 501 | 100 |
| 3 | robin | male | 2018-02-02 | instructor | teach | 8000.00 | 501 | 100 |
| 4 | alice | female | 2018-02-02 | instructor | teach | 7200.00 | 501 | 100 |
| 5 | aofa | male | 2018-02-02 | hr | hrcc | 600.00 | 502 | 101 |
| 6 | harry | male | 2018-02-02 | hr | NULL | 6000.00 | 502 | 101 |
| 7 | emma | female | 2018-02-06 | sale | salecc | 20000.00 | 503 | 102 |
| 8 | christine | female | 2018-02-05 | sale | salecc | 2200.00 | 503 | 102 |
| 9 | zhuzhu | male | 2018-02-05 | sale | NULL | 2200.00 | 503 | 102 |
| 10 | gougou | male | 2018-02-05 | sale | | 2200.00 | 503 | 102 |
+----+-----------+--------+------------+------------+-----------------+----------+--------+--------+
10 rows in set (0.00 sec)
2.查询部分列
mysql> select id,name from company.employee5;
+----+-----------+
| id | name |
+----+-----------+
| 1 | jack |
| 2 | tom |
| 3 | robin |
| 4 | alice |
| 5 | aofa |
| 6 | harry |
| 7 | emma |
| 8 | christine |
| 9 | zhuzhu |
| 10 | gougou |
+----+-----------+
10 rows in set (0.00 sec)
3.通过四则运算查询,看看年薪
mysql> show databases; #查看库
mysql> use company #进入库
mysql> select name,salary,salary*14 from employee5;
+-----------+----------+-----------+
| name | salary | salary*14 |
+-----------+----------+-----------+
| jack | 5000.00 | 70000.00 |
| tom | 5500.00 | 77000.00 |
| robin | 8000.00 | 112000.00 |
| alice | 7200.00 | 100800.00 |
| aofa | 600.00 | 8400.00 |
| harry | 6000.00 | 84000.00 |
| emma | 20000.00 | 280000.00 |
| christine | 2200.00 | 30800.00 |
| zhuzhu | 2200.00 | 30800.00 |
| gougou | 2200.00 | 30800.00 |
+-----------+----------+-----------+
10 rows in set (0.00 sec)
1.语法:如果不写库名,得先进入库
select 列名 from 表名 where 条件;
2.查询hr部门的员工姓名
mysql> select name from employee5 where post="hr";
+-------+
| name |
+-------+
| aofa |
| harry |
+-------+
2 rows in set (0.00 sec)
1.语法
select 列名 from 表名 where 条件 and 条件;
2.查询hr部门的员工姓名,并且工资大于1000
mysql> select * from employee5 where post="hr" and salary>1000;
+----+-------+------+------------+------+-----------------+---------+--------+--------+
| id | name | sex | hire_date | post | job_description | salary | office | dep_id |
+----+-------+------+------------+------+-----------------+---------+--------+--------+
| 6 | harry | male | 2018-02-02 | hr | NULL | 6000.00 | 502 | 101 |
+----+-------+------+------------+------+-----------------+---------+--------+--------+
1 row in set (0.00 sec)
3.查询所有部门的员工姓名,并且工资是6000的员工或者8000的员工
mysql> select * from employee5 where salary=6000 or salary>8000;
+----+-------+--------+------------+------+-----------------+----------+--------+--------+
| id | name | sex | hire_date | post | job_description | salary | office | dep_id |
+----+-------+--------+------------+------+-----------------+----------+--------+--------+
| 6 | harry | male | 2018-02-02 | hr | NULL | 6000.00 | 502 | 101 |
| 7 | emma | female | 2018-02-06 | sale | salecc | 20000.00 | 503 | 102 |
+----+-------+--------+------------+------+-----------------+----------+--------+--------+
2 rows in set (0.00 sec)
1.语法
select 列名 from 表名 where 条件 (not可选)between 范围 and 范围;
2.查一查薪资在5000到15000
mysql> select name,salary from employee5 where salary between 5000 and 15000;
+-------+---------+
| name | salary |
+-------+---------+
| jack | 5000.00 |
| tom | 5500.00 |
| robin | 8000.00 |
| alice | 7200.00 |
| harry | 6000.00 |
+-------+---------+
5 rows in set (0.00 sec)
3.不在5000~15000呢?请使用NOT
mysql> select name,salary from employee5 where salary not between 5000 and 15000;
+-----------+----------+
| name | salary |
+-----------+----------+
| aofa | 600.00 |
| emma | 20000.00 |
| christine | 2200.00 |
| zhuzhu | 2200.00 |
| gougou | 2200.00 |
+-----------+----------+
5 rows in set (0.00 sec)
1.语法
select 列名 from 表名 where 条件 in (关键字);
2.查询工资可能是4000,也可能是5000,还有可能是9000。
mysql> select * from employee5 where salary in (4000,5000,6000,20000);
+----+-------+--------+------------+------------+-----------------+----------+--------+--------+
| id | name | sex | hire_date | post | job_description | salary | office | dep_id |
+----+-------+--------+------------+------------+-----------------+----------+--------+--------+
| 1 | jack | male | 2018-02-02 | instructor | teach | 5000.00 | 501 | 100 |
| 6 | harry | male | 2018-02-02 | hr | NULL | 6000.00 | 502 | 101 |
| 7 | emma | female | 2018-02-06 | sale | salecc | 20000.00 | 503 | 102 |
+----+-------+--------+------------+------------+-----------------+----------+--------+--------+
3 rows in set (0.00 sec)
3.取反,加not
mysql> select * from employee5 where salary not in (4000,5000,6000,200000);
+----+-----------+--------+------------+------------+-----------------+---------+--------+--------+
| id | name | sex | hire_date | post | job_description | salary | office | dep_id |
+----+-----------+--------+------------+------------+-----------------+---------+--------+--------+
| 2 | tom | male | 2018-02-03 | instructor | teach | 5500.00 | 501 | 100 |
| 3 | robin | male | 2018-02-02 | instructor | teach | 8000.00 | 501 | 100 |
| 4 | alice | female | 2018-02-02 | instructor | teach | 7200.00 | 501 | 100 |
| 5 | aofa | male | 2018-02-02 | hr | hrcc | 600.00 | 502 | 101 |
| 8 | christine | female | 2018-02-05 | sale | salecc | 2200.00 | 503 | 102 |
| 9 | zhuzhu | male | 2018-02-05 | sale | NULL | 2200.00 | 503 | 102 |
| 10 | gougou | male | 2018-02-05 | sale | | 2200.00 | 503 | 102 |
+----+-----------+--------+------------+------------+-----------------+---------+--------+--------+
7 rows in set (0.00 sec)
1.语法:没有写东西的空的
select 列名 from 表名 where 条件 is (not可选,取反)null;
2.没有岗位描述的job_description
mysql> select * from employee5 where job_description is null;
+----+--------+------+------------+------+-----------------+---------+--------+--------+
| id | name | sex | hire_date | post | job_description | salary | office | dep_id |
+----+--------+------+------------+------+-----------------+---------+--------+--------+
| 6 | harry | male | 2018-02-02 | hr | NULL | 6000.00 | 502 | 101 |
| 9 | zhuzhu | male | 2018-02-05 | sale | NULL | 2200.00 | 503 | 102 |
+----+--------+------+------------+------+-----------------+---------+--------+--------+
2 rows in set (0.00 sec)
1.语法
select 列名 from 表名 where 列名 like ‘条件%’;
这里%是任意的意思,在mysql中用%来代表任意的意思,放在前面就是以什么结尾的,放在后面就是以什么开头的
select 列名 from employee5 where 列名 like ‘条件_’;
mysql使用下划线,通配符"_"代表任意1个字符,写几个下划线就代表几个。
2.查找名字以a开头的员工
mysql> select * from employee5 where name like 'a%';
+----+-------+--------+------------+------------+-----------------+---------+--------+--------+
| id | name | sex | hire_date | post | job_description | salary | office | dep_id |
+----+-------+--------+------------+------------+-----------------+---------+--------+--------+
| 4 | alice | female | 2018-02-02 | instructor | teach | 7200.00 | 501 | 100 |
| 5 | aofa | male | 2018-02-02 | hr | hrcc | 600.00 | 502 | 101 |
+----+-------+--------+------------+------------+-----------------+---------+--------+--------+
2 rows in set (0.00 sec)
2.2查找名字是alice的员工,四个下划线
mysql> select * from employee5 where name like 'a____';
+----+-------+--------+------------+------------+-----------------+---------+--------+--------+
| id | name | sex | hire_date | post | job_description | salary | office | dep_id |
+----+-------+--------+------------+------------+-----------------+---------+--------+--------+
| 4 | alice | female | 2018-02-02 | instructor | teach | 7200.00 | 501 | 100 |
+----+-------+--------+------------+------------+-----------------+---------+--------+--------+
1 row in set (0.00 sec)
2.3查找名字是aofa的员工,三个下划线
mysql> select * from employee5 where name like 'a___';
+----+------+------+------------+------+-----------------+--------+--------+--------+
| id | name | sex | hire_date | post | job_description | salary | office | dep_id |
+----+------+------+------------+------+-----------------+--------+--------+--------+
| 5 | aofa | male | 2018-02-02 | hr | hrcc | 600.00 | 502 | 101 |
+----+------+------+------------+------+-----------------+--------+--------+--------+
1 row in set (0.00 sec)
1.语法
select 列名 from 表名 order by 列名 asc/desc;
order排序的意思,不加asc和desc默认升序
asc升序
deac降序,工作中常用
2.工资升序排列
mysql> select name,salary from employee5 order by salary asc;
+-----------+----------+
| name | salary |
+-----------+----------+
| aofa | 600.00 |
| christine | 2200.00 |
| zhuzhu | 2200.00 |
| gougou | 2200.00 |
| jack | 5000.00 |
| tom | 5500.00 |
| harry | 6000.00 |
| alice | 7200.00 |
| robin | 8000.00 |
| emma | 20000.00 |
+-----------+----------+
10 rows in set (0.00 sec)
3.工资最高前五名
mysql> select name,salary from employee5 order by salary desc limit 5;
+-------+----------+
| name | salary |
+-------+----------+
| emma | 20000.00 |
| robin | 8000.00 |
| alice | 7200.00 |
| harry | 6000.00 |
| tom | 5500.00 |
+-------+----------+
5 rows in set (0.00 sec)
参数解释:
limit:限制,限制几后面就写几