1、MySql的登錄方式
MySQL登錄的兩種方式:
mysql -uroot -pmysql --host=192.168.88.161 --user=root --password=123456
2、SQL語(yǔ)言介紹
- 操作數(shù)據(jù)庫(kù)數(shù)據(jù)表 DDL
- 對(duì)數(shù)據(jù)進(jìn)行增加刪除修改 DML
- 對(duì)數(shù)據(jù)庫(kù)進(jìn)行查詢 DQL
- 權(quán)限控制, 用戶創(chuàng)建管理 DCL
2.1、DDL
數(shù)據(jù)庫(kù)操作
- 創(chuàng)建數(shù)據(jù)庫(kù):
CREATE DATABASE 數(shù)據(jù)庫(kù)名; - 查看數(shù)據(jù)庫(kù):
SHOW DATABASES; - 刪除數(shù)據(jù)庫(kù):
DROP DATABASE 數(shù)據(jù)庫(kù)名; - 使用數(shù)據(jù)庫(kù):
USE 數(shù)據(jù)庫(kù)名;
數(shù)據(jù)表操作
- 創(chuàng)建表:
CREATE TABLE 表名(字段名 類型 約束…) - 查詢表:
SHOW TABLES;DESC 表名; - 刪除表:
DROP TABLE 表名; - 修改表:
-
alter table 表名 add 列名 類型(長(zhǎng)度) [約束]; 增加一列 -
alter table 表名 change 舊列名 新列名 類型(長(zhǎng)度) 約束; 修改一列名字 -
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;
主鍵
特點(diǎn):唯一標(biāo)志,不能重復(fù),不能為空。
// 創(chuàng)建主鍵
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 '北京');
// 主鍵設(shè)置
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降序 默認(rèn)是升序 ASC
select * from product order by price DESC ,category_id DESC ;
# 聚合函數(shù) count 計(jì)數(shù) 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;
多表查詢

多表查詢的連接方式
表之間關(guān)系:一對(duì)一、一對(duì)多
外鍵約束:
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))
- 內(nèi)連接(左表存在,右表也存在的數(shù)據(jù)被保留)
SELECT hname,kname FROM hero INNER JOIN kongfu ON hero.kongfu_id = kongfu.kid;
- 左連接(左表存在的數(shù)據(jù)被保留)
SELECT hname,kname FROM hero LEFT OUTER JOIN kongfu ON hero.kongfu_id = kongfu.kid;
- 右連接(右表存在的數(shù)據(jù)被保留)
SELECT hname,kname FROM hero RIGHT OUTER JOIN kongfu ON hero.kongfu_id = kongfu.kid;
子查詢
// 一個(gè)select語(yǔ)句的結(jié)果 是作為另外一個(gè)select 條件取值
select * from products where category_id =
(select cid from category where cname='化妝品');
// 一個(gè)select語(yǔ)句的結(jié)果也可以做為一張臨時(shí)表, 和另外一張表進(jìn)行關(guān)聯(lián)查詢
select * from products p, (select * from category where cname = '化妝品') c where p.category_id=c.cid;
自連接:兩張表進(jìn)行join 這兩張表實(shí)際上來(lái)自同一張表 就是自連接
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自定義分組,如果不滿足其他條件,則執(zhí)行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(字段) 區(qū)別
- 如果所有字段都沒(méi)有null count(*) count(字段) 取值都一樣, 在這個(gè)條件下, 分組之后, count任何一個(gè)字段取值都相同
- 如果 某個(gè)字段中包含了null count(字段) 不統(tǒng)計(jì)null值的 , count(*) 會(huì)統(tǒng)計(jì)null