7.20笔记
MySQL基础知识 1.MySQL的基础知识以及如何安装MySQL。 MySQL是一个数据库,即一个存储数据的仓库,数据在数据库中进行有组织的存储。 2.MySQL的操作语言SQL语言 我们可以通过SQL语言来操作数据库管理系统,通过系统操作数据库以及数据库中的数据。 3.常见的一些数据库 例如:Oracle、MySQL等等。 4. SQL通用语法 数据库管理 1.查看数据库:show databases 数据库名; 2.创建数据库:create database 数据库名; 3.选择数据库:use 数据库名; 4.删除数据库;drop databse 数据库名; 表管理 1.查看当前数据库中的表:show tables ; 2.创建表:create table 表名 (字段名1 类型,字段名2 类型); 3.查看表结构:desc 表名; 4.删除表:drop table 表名; 用户管理 1.查看当前登录的用户:select user (); 2.创建新用户:create user 用户名@‘目标主机IP’ 3.修改密码:alter user 用户名@‘目标主机IP’ indentified by '' ‘设置密码‘ ; 4.查询用户信息:select user,host from mysql.user; 5.新用户登录: mysql -u 用户名 [-h ip地址] -p ; 6.为用户授权:shou grants for 用户名@目标主机IP; 7.所有权限:grant all on 数据库名.名 to 用户名@目标主机IP; 8.删除用户:drop user 用户名@目标主机Ip;
7.21笔记
MYSQL数据类型
MySQL数据类型大多分为三类:数值型、字符型、日期型。常用的数据类型有int ,double,DATE,DATETIME ,TIMESATMP,VARCHAR等等。
char 与 varchar 都可以描述字符串,char是定长字符串,指定长度多长,就占用多少个字符,和字段值的长度无关 。而varchar是变长字符串,指定的长度为最大占用长度 。
DDL数据定义语言
DDL是对表结构的操作,不涉及表中的数据操作。
1.添加列
alter table 表名 字段名 字段类型;
2.修改列
alter table 表名 modify 字段名 字段类型;
3.修改列名称和类型
alter table 表名 change 原字段名 新字段名 新字段名类型;
4.删除列
alter table 表名 drop 字段名;
5.修改表名
rename table 旧名称 to 新名称;
alter table 旧名称 rename to 新名称;
6.删除表
drop table 表名;
7.截断表
truncate table 表名;
<!--删除表后创建一个新表-->
8.创建和某表结构一样的表
create table 表名 like 要复制的表名;
<!--也可以复制其他数据库中的表-->
DML数据操作语言
1.插入数据
insert into 表名 (字段名) vlaues (对应的数据);
<!--也可以不写字段名直接添加数据,就是按表中字段的顺序添加-->
2.插入多条数据
insert into 表名 values(),(),();
DQL数据查询语言
1.无条件查询
select * from 表名;
2.等值查询
select * from 表名 where 字段名=values;
3.设置列别名
`select 字段名 as 别名 from 表名 where 条件
4.剔除重复行
select distinct * from 表名;
5.修改数据
update 表名 set 字段 = value where 条件;
6.删除数据
delete *from 表名 where 条件;
数据备份
1.只备份表结构
mysqldump -u root -p -d 数据库名 表名 > 存放地址+存放名称;
2.备份结构+数据
mysqldump -u root -p 数据库名 表名 > 存放地址+存放名称;
3.备份多个数据库
mysqldump -u root -p -d --databases 数据库名1 数据库名2>存放地址+存放名称;
4.备份所有数据库
mysqldump -u root -p -d --all-databases 数据库名1 数据库名2 > 存放地址+存放名称;
5.将查询的结果保存为文件集
mysql -u root -p -e "select * from 表名" > 存放地址+存放名称;
6.还原数据结构和数据
source 文件地址;
<!--需要打开数据库应用-->
mysql -u root -p 数据库名< 文件地址;
<!--不需要打开数据库就可以使用-->
条件查询
1.条件过滤
select * from 表名;
<!--表示查询表中的所有字段-->
select * from 表名 where 条件;
<!--表示根据条件查询符合条件的字段-->
2.算数运算
select 字段1+字段2 from 表名;
<!--可以多查询出来的结果进行算术运算-->
3.NULL值查询
select * from 表名 where 字段 is null;
4.模糊查询
select * from 表名 where 字段 like _x%;
<!--_ b表示任意的一个字符,%表示零个或多个任意字符-->
5.逻辑运算
select * from 表名 WHERE 字段 >value;
6.排序
select * from 表名 order by 字段;
<!--默认为升序排序 DESC为降序排序-->
7.分页查询
select * from 表名 limit 0,2
<!--从第0行开始显示两条数据-->
计算公式:页码为n 页大小为s (n-1)*s,s
8.单行函数
select length("123");
<!--计算字段长度-->
select upper/lower("A");
<!--大小写转换-->
select concat(字段1,字段2);
<!--字符串拼接-->
练习题一
1.查询商品库存等于50的所有商品,显示商品编号,商品名称,商品售价,商品库存。
Select good_no “商品编号”,goods_name “商品名称”,price “商品售价”,count “商品库存” from goods where count=50;
2.查询商品成本等于60的所有商品,显示商品编号,商品名称,商品售价,商品库存。
Select good_no “商品编号”,goods_name “商品名称“,price “商品售价”,count “商品库存” from goods where cost=60;
3.修改购物车中id为5的num数量加一
Update cart set num=num+1 where id=5;
4.会员等级表添加一列字段为 id,类型为int
``
5.修改购物车表的id字段为cart_id
Update cart set num=num+1 where id=5;
6.查询购物车数量等于10的信息,显示购物车编号,商品编号,商品数量,创建时间,账户编号。
Select cart_id “购物车编号”,goods_no “商品编号”,num “商品数量”,create_time “用户创建时间”,account_id “用户编号” from.account,store.cart where num=10;
7. 删除用户表中账户余额等于9的用户
Delete from account where money=9
8.查询购物车中用户id为3的所有信息
Select * from cart where account_id=3;
9.查询goods表中第二页的数据,每页显示5条数据
Select * from goods limit 5,5;
10.查询goods表中的数据,按照商品价格升序排序,如果商品价格一样按照成本降序排序,并显示第三页,每页显示3条数据。
Select * from goods order by price,cost desc limit 6,3;
练习题二
1.修改emp表中sal字段为salary
alter table emp change SAL salary double;
2.查找年薪在20000到30000之间的所有员工信息并按照工资降序显示
select * from emp where salary*12 between 20000 and 30000;
3.查找员工姓名中包含'A'的所有员工信息
select * from emp where ENAME like "%A%";``
4.查找所有员工姓名中包含'A'及'E'的员工信息
select * from emp where ENAME like "%A%" and ENAME like "%E%";
5.查找所有的职位为SALESMAN的员工信息
select * from emp where JOB='SALESMAN'
;
6.将工资低于2000的员工工资涨薪200
update emp set salary=salary+200 where salary<2000;
7.查询没有上级领导的所有员工信息
select * from emp where MGR is NULL;
8. 查询没有奖金的所有员工信息
select * from emp where COMM is NULL;
9.将部门表中的40部门的地址修改成'xian'
update dept set LOC='xian' where DEPTNO=40;
10. 假设李华的工资是2000,请查询出他的工资等级
select GRADE from salgrade where 2000>LOSAL or 2000<HISAL;
11.将MILLER的入职日期修改为1982年2月23日
update emp set HIREDATE="1982-02-03" where ENAME='MILLER';
练习题三
1. 写一条SQL查询语句,找出所有属于"Electronics"类别的产品信息。
select * from products where category='Electronics';
2.写一条SQL查询语句,找出价格高于等于100.00的产品信息。
mysql> select * from products where price>=100.00;
3.写一条SQL查询语句,找出价格在20.00到1000.00之间的产品信息。
mysql> select * from products where price between 20.00 and 1000.00;
4.对于"products"表,有一个新的需求:将"category"列改名为"product_category"
alter table products change category product_category varchar(14);
5.将"price"列的数据类型从DECIMAL(10, 2)改为DECIMAL(12, 2)。请提供相应的SQL语句来执行这些修改。
alter table products change price price DECIMAL(12,2);
6.对于"products"表,又有一个新的需求:由于某些原因,我们不再销售名为"Laptop"的产品,需要从表中删除该记录。请提供一条SQL删除语句来执行此操作。
mysql> delete from products where product_name='Laptop';
7.写一条SQL查询语句,找出商品名称包含字母"e"的商品信息。
mysql> select * from products where product_name like '%e%';
8. 如果要购买10个Mouse,请显示出最终的价格。
select price*10 as '总价' from products where product_name = 'mouse'
9. 将商品的名字,商品的类别全部转换成小写形式并展示所有信息。
select *,lower(product_name),lower(product_category) from products;
10.将商品类别和名称拼接起来并显示其余的信息。比如:Elctronics-Mouse
select *,concat(product_category,product_name) from products;
纠错
首先是一个粗心大意,审题不好好审题,写单词少字母,一定注意
1.编写SQL查询语句,从students表中选取前三名成绩为A的学生信息.
select * from students where grade='A' limit 0,3;
<!--没有想到这里可以用limit,对limit理解不够深刻-->
2.编写SQL查询语句,从students表中选取前两名成绩为B的女生信息.
`select * from students where grade='B' gender='female' limit 0,2;
<!--没有想到这里可以用limit,对limit理解不够深刻-->
3.编写SQL查询语句,查找所有职位吧包含"Engineer"关键字的员工信息
select * from students where position like "%Engineer%"
<!--没有掌握like的用法-->