1、MySql的登录方式
MySQL登录的两种方式:
mysql -uroot -pmysql --host=192.168.88.161 --user=root --password=123456
2、SQL语言介绍
- 操作数据库数据表 DDL
- 对数据进行增加删除修改 DML
- 对数据库进行查询 DQL
- 权限控制, 用户创建管理 DCL
2.1、DDL
数据库操作
- 创建数据库:
CREATE DATABASE 数据库名; - 查看数据库:
SHOW DATABASES; - 删除数据库:
DROP DATABASE 数据库名; - 使用数据库:
USE 数据库名;
数据表操作
- 创建表:
CREATE TABLE 表名(字段名 类型 约束…) - 查询表:
SHOW TABLES;DESC 表名; - 删除表:
DROP TABLE 表名; - 修改表:
-
alter table 表名 add 列名 类型(长度) [约束]; 增加一列 -
alter table 表名 change 旧列名 新列名 类型(长度) 约束; 修改一列名字 -
alter table 表名 drop 列名; 删除一列 -
rename table 表名 to 新表名; 修改表名
-
2.2、DML
- 插入记录:
insert into 表 (字段1,字段2,字段3...) values(值1,值2,值3...),(值1,值2,值3...)…; - 更新记录:
update 表名 set 字段名=值,字段名=值,...; - 删除记录:
delete from 表名 [where 条件];truncate category;
主键
特点:唯一标志,不能重复,不能为空。
// 创建主键
CREATE TABLE person(id INT PRIMARY KEY AUTO_INCREMENT,
last_name VARCHAR(100) UNIQUE,
first_name VARCHAR(100),
address VARCHAR(100),city VARCHAR(100) DEFAULT '北京');
// 主键设置
ALTER TABLE person CHANGE id id INT AUTO_INCREMENT NOT NULL
// 添加主键
ALTER TABLE person ADD PRIMARY KEY(id)
// 删除主键
ALTER TABLE person DROP PRIMARY KEY
2.3、DQL
单表查询
# 条件查询
select pname,price from product where price between 200 and 800;
select pname,price from product where price in(200,800);
# 逻辑查询
select * from product where price>=200 and price <=800;
select * from product where price=200 or price =800;
select * from product where not (price=200);
# 模糊查询
select * from product where pname like '香%';
select * from product where pname like '_想%';
# 非空查询
select * from product where product.category_id is not null ;
# 排序 order by DESC降序 默认是升序 ASC
select * from product order by price DESC ,category_id DESC ;
# 聚合函数 count 计数 sum 求和 max 最大 min最小 avg 平均
select count(*)
from product;
select count(*) from product where price>200;
select sum(price) from product where category_id='c001';
select avg(price) from product where category_id='c001';
select MAX(price),MIN(price) from product;
# 分组查询:SELECT 字段1,字段2… FROM 表名 GROUP BY 分组字段 HAVING 分组条件;
select category_id, count(*) from product group by category_id having count(*) > 1;
select category_id, max(price) from product group by category_id;
多表查询

多表查询的连接方式
表之间关系:一对一、一对多
外键约束:
create table category (cid varchar(32) primary key ,cname varchar(100));
create table products (pid varchar(32) primary key, pname varchar(40), price DOUBLE,
category_id varchar(32),
CONSTRAINT FOREIGN KEY (category_id) REFERENCES category(cid))
- 内连接(左表存在,右表也存在的数据被保留)
SELECT hname,kname FROM hero INNER JOIN kongfu ON hero.kongfu_id = kongfu.kid;
- 左连接(左表存在的数据被保留)
SELECT hname,kname FROM hero LEFT OUTER JOIN kongfu ON hero.kongfu_id = kongfu.kid;
- 右连接(右表存在的数据被保留)
SELECT hname,kname FROM hero RIGHT OUTER JOIN kongfu ON hero.kongfu_id = kongfu.kid;
子查询
// 一个select语句的结果 是作为另外一个select 条件取值
select * from products where category_id =
(select cid from category where cname='化妆品');
// 一个select语句的结果也可以做为一张临时表, 和另外一张表进行关联查询
select * from products p, (select * from category where cname = '化妆品') c where p.category_id=c.cid;
自连接:两张表进行join 这两张表实际上来自同一张表 就是自连接
select p.title province,c.title city, c.id from tb_areas as c join tb_areas as p on c.pid=p.id where p.title = '广东省';
CASE WHEN
CASE WHEN自定义分组,如果不满足其他条件,则执行ELSE
SELECT
order_id,
customer_id,
ship_country,
CASE
WHEN ship_country = 'USA' OR ship_country = 'Canada' THEN 0.0
ELSE 10.0
END AS shipping_cost
FROM orders
WHERE order_id BETWEEN 10720 AND 10730;
count(*) 和 count(字段) 区别
- 如果所有字段都没有null count(*) count(字段) 取值都一样, 在这个条件下, 分组之后, count任何一个字段取值都相同
- 如果 某个字段中包含了null count(字段) 不统计null值的 , count(*) 会统计null