mysql入门篇
-
mysql常用命令
mysql> SELECT VERSION(); 查看当前MYSQL版本
mysql> SELECT USER();查看当前用户
mysql> SELECT NOW();查看当前时间
mysql> SELECT DATABASE();查看当前数据库 -
MySQL的语句规范
关键字、函数名称大写
数据库名、表明、字段名小写
分号结尾 -
操作数据库
mysql> CREATE DATABASE t2;创建数据库
mysql> SHOW DATABASES;查看所有数据库
mysql> SHOW CREATE DATABASE t2;查看某个具体的数据库
mysql> ALTER DATABASE t2 CHARACTER SET=utf8;修改编码为utf8
mysql> DROP DATABASE IF EXISTS t2;删除数据库
mysql> SHOW WARNINGS;显示warning原因 -
数据类型
4.1 整型
整型 image.png
4.2 浮点型
FLOAT[(M,D)]
DOUBLE[(M,D)]
其中M为全部位数,D为小数点位数
4.3 日期时间型
image.png image.png4.4 字符型
image.png
- 表格常用操作
$ mysql -uroot -p123456 -P3306 -h127.0.0.1;
mysql> USE test;打开数据库
mysql> SHOW TABLES;查看数据库有哪些表格
mysql> SHOW TABLES FROM mysql;查看mysql数据库的表格
mysql> CREATE TABLE tb1(
-> username varchar(20),
-> age tinyint unsigned,
-> salary FLOAT(8,2)
-> );
mysql> SHOW COLUMNS FROM tb1;查看数据表结构
mysql> INSERT INTO tb1(username, age, salary) values('xiaoming', 21, 123.3);插入记录
mysql> CREATE TABLE tb2(
-> username VARCHAR(20) NOT NULL,
-> age TINYINT NOT NULL,
-> salary FLOAT(8,2) NOT NULL
-> );
image.png
CREATE TABLE tb6(
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(20) NOT NULL,
age TINYINT NOT NULL,
salary FLOAT(8,2) NOT NULL
);
必须定义为主键,才可以为auto_increment
auto_increment默认情况下,起始值为1,每次的增量为1
每张表只能存在一个主键,主键保证记录唯一性,主键自动为NOT NULL
mysql> CREATE TABLE tb10(
-> id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> username VARCHAR(20) NOT NULL UNIQUE,
-> age TINYINT DEFAULT 20
-> );
mysql> INSERT INTO tb10(username) VALUES('小明');
image.png
UNIQUE KEY:唯一约束,可以保证记录唯一性,每张表可以存在多个唯一约束;
DEFAULT:默认值;
- 创建外键约束
CREATE TABLE province(
id SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(20) NOT NULL
)ENGINE=INNODB CHARSET=utf8;
CREATE TABLE user(
id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(20) NOT NULL,
province_id SMALLINT NOT NULL,
FOREIGN KEY (province_id) REFERENCES province(id)
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
外键约束的要求:
父表和子表使用相同的存储引擎,数据表的存储引擎只能为InNODB;
外键列和参照列必须具有相似的数据类型。其中数字的长度和符号位必须相同,而字符的长度则可以不同;
外键列和参照列必须创建索引。如果外键列不存在索引的话,MySQL将自动创建索引。
外键约束的参照操作:
CASCADE:从父表删除或更新且自动删除或更新子表中匹配的行
SET NULL:从父表删除或更新行,会设置子表中的外键列为NULL。必须保证子表外键列没有指定NOT NULL;
RESTRICT:拒绝对父表的删除或更新操作;
NO ACTION:标准SQL关键字,在MySQL中与RESTRICT相同。
mysql> CREATE TABLE user1(
-> id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
-> username VARCHAR(20) NOT NULL,
-> province_id SMALLINT NOT NULL,
-> FOREIGN KEY (province_id) REFERENCES province(id) ON DELETE CASCADE
-> )ENGINE=InnoDB DEFAULT CHARSET=utf8;
mysql> INSERT INTO province(name) values('henan');
mysql> INSERT INTO province(name) values('shandong');
mysql> INSERT INTO province(name) values('shanghai');
mysql> INSERT INTO user1(username, province_id) VALUES('ROSE', 1);
mysql> INSERT INTO user1(username, province_id) VALUES('JACK', 3);
mysql> INSERT INTO user1(username, province_id) VALUES('TOM', 3);
mysql> INSERT INTO user1(username, province_id) VALUES('BLUZE', 7);
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (test
.user1
, CONSTRAINT user1_ibfk_1
FOREIGN KEY (province_id
) REFERENCES province
(id
) ON DELETE CASCADE)
mysql> DELETE FROM province WHERE id=3;
image.png image.png-
修改表格字段
mysql> ALTER TABLE user1 ADD score BIGINT NOT NULL FIRST;在表格首部增加一个字段;
mysql> ALTER TABLE user1 ADD password VARCHAR(20) NOT NULL AFTER username;在username后面增加一个字段;
mysql> ALTER TABLE user1 DROP score;删除一个字段 -
修改约束
image.png
添加主键约束:
mysql> ALTER TABLE tb1 ADD PRIMARY KEY(username);
image.png
添加唯一约束:
mysql> ALTER TABLE tb1 ADD UNIQUE KEY(age);
image.png
删除主键/唯一约束:
mysql> ALTER TABLE tb1 DROP PRIMARY KEY;
image.png
mysql> ALTER TABLE tb1 DROP INDEX age;
image.png
添加删除默认约束
mysql> ALTER TABLE tb1 ALTER age SET DEFAULT 3;
mysql> ALTER TABLE tb1 ALTER age DROP DEFAULT;
image.png删除外键约束:
image.png
mysql> ALTER TABLE user1 DROP FOREIGN KEY user1_ibfk_1;
image.png
修改列定义:
image.pngmysql> ALTER TABLE user1 MODIFY id BIGINT NOT NULL AUTO_INCREMENT FIRST;
image.png修改列名字:
image.png
mysql> ALTER TABLE user1 CHANGE username user_name VARCHAR(20) NOT NULL;
image.png
修改表名字:
mysql> ALTER TABLE user1 RENAME user2;
- 操作数据表中的记录
9.1 插入记录:
mysql> CREATE TABLE users(
-> id SMALLINT AUTO_INCREMENT PRIMARY KEY,
-> username VARCHAR(20) NOT NULL,
-> password VARCHAR(20) NOT NULL,
-> age TINYINT NOT NULL DEFAULT 10,
-> sex BOOLEAN NOT NULL
-> )ENGINE=INNODB DEFAULT CHARSET=utf8;
mysql> INSERT INTO users VALUES(NULL, 'xiaoming', '123', 22, 1);
mysql> INSERT INTO users VALUES(DEFAULT, 'jack', '123', 22, 1);
mysql> INSERT INTO users VALUES(DEFAULT, 'rose', '123', DEFAULT, 1);
mysql> INSERT INTO users VALUES(DEFAULT, 'bluze', '123', DEFAULT, 1),(DEFAULT, 'huahua', '321', 32, 0);
mysql> INSERT INTO users(username, password, sex) VALUES('niuniu', '643', 0),('gougou', '777', 1);
mysql> INSERT INTO users SET username='haha', password='321', sex=0;
image.png
9.2 更新记录操作
mysql> UPDATE users SET age = age + 5;
image.png
mysql> UPDATE users SET age = age -10, sex = 0;
image.png
mysql> UPDATE users SET age = age - id;
image.png
mysql> UPDATE users SET age = age + 10 where id % 2 = 0;
image.png
9.3 删除一条记录
mysql> DELETE FROM users WHERE id=6;
image.png
mysql> INSERT INTO users VALUES(DEFAULT, 'TOM', '2323', DEFAULT, 1);
image.png
9.4 查找记录
mysql> SELECT id, username FROM users;
image.png
mysql> SELECT username, id FROM users;
image.png
mysql> SELECT id AS user_id, username AS user_name FROM users;
image.png
mysql> SELECT users.id, users.username FROM users;
image.png
mysql> SELECT sex FROM users GROUP BY sex;
image.png
mysql> SELECT sex FROM users GROUP BY sex ASC;
image.png
mysql> SELECT sex FROM users GROUP BY sex DESC;
image.png
mysql> SELECT sex,age FROM users GROUP BY sex HAVING age>7;
image.png
mysql> SELECT * FROM users ORDER BY age DESC;
image.png
mysql> SELECT * FROM users ORDER BY age DESC,id DESC;
image.png
mysql> SELECT * FROM users LIMIT 2;
image.png
mysql> SELECT * FROM users LIMIT 2,3;
image.png
mysql> SELECT * FROM users ORDER BY id DESC LIMIT 2,3;
image.png
mysql> CREATE TABLE test(
-> id SMALLINT AUTO_INCREMENT PRIMARY KEY,
-> username VARCHAR(20) NOT NULL
-> )CHARSET=utf8;
mysql> INSERT INTO test(username) SELECT username FROM users;
image.png
- 子查询与连接
10.1数据准备
mysql> CREATE TABLE IF NOT EXISTS tbd_goods(
-> goods_id SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
-> goods_name varchar(150) NOT NULL,
-> goods_cate VARCHAR(40) NOT NULL,
-> brand_name VARCHAR(40) NOT NULL,
-> goods_price DECIMAL(15,3) UNSIGNED NOT NULL DEFAULT 0,
-> is_show BOOLEAN NOT NULL DEFAULT 1,
-> is_saleoff BOOLEAN NOT NULL DEFAULT 0
-> )CHARSET=utf8;
INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('R510VC 15.6英寸笔记本','笔记本','华硕','3399',DEFAULT,DEFAULT);
INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('Y400N 14.0英寸笔记本电脑','笔记本','联想','4899',DEFAULT,DEFAULT);
INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('G150TH 15.6英寸游戏本','游戏本','雷神','8499',DEFAULT,DEFAULT);
INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('X550CC 15.6英寸笔记本','笔记本','华硕','2799',DEFAULT,DEFAULT);
INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('X240(20ALA0EYCD) 12.5英寸超极本','超级本','联想','4999',DEFAULT,DEFAULT);
INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('U330P 13.3英寸超极本','超级本','联想','4299',DEFAULT,DEFAULT);
INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('SVP13226SCB 13.3英寸触控超极本','超级本','索尼','7999',DEFAULT,DEFAULT);
INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('iPad mini MD531CH/A 7.9英寸平板电脑','平板电脑','苹果','1998',DEFAULT,DEFAULT);
INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('iPad Air MD788CH/A 9.7英寸平板电脑 (16G WiFi版)','平板电脑','苹果','3388',DEFAULT,DEFAULT);
INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(' iPad mini ME279CH/A 配备 Retina 显示屏 7.9英寸平板电脑 (16G WiFi版)','平板电脑','苹果','2788',DEFAULT,DEFAULT);
INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('IdeaCentre C340 20英寸一体电脑 ','台式机','联想','3499',DEFAULT,DEFAULT);
INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('Vostro 3800-R1206 台式电脑','台式机','戴尔','2899',DEFAULT,DEFAULT);
INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('iMac ME086CH/A 21.5英寸一体电脑','台式机','苹果','9188',DEFAULT,DEFAULT);
INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('AT7-7414LP 台式电脑 (i5-3450四核 4G 500G 2G独显 DVD 键鼠 Linux )','台式机','宏碁','3699',DEFAULT,DEFAULT);
INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('Z220SFF F4F06PA工作站','服务器/工作站','惠普','4288',DEFAULT,DEFAULT);
INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('PowerEdge T110 II服务器','服务器/工作站','戴尔','5388',DEFAULT,DEFAULT);
INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('Mac Pro MD878CH/A 专业级台式电脑','服务器/工作站','苹果','28888',DEFAULT,DEFAULT);
INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(' HMZ-T3W 头戴显示设备','笔记本配件','索尼','6999',DEFAULT,DEFAULT);
INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('商务双肩背包','笔记本配件','索尼','99',DEFAULT,DEFAULT);
INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('X3250 M4机架式服务器 2583i14','服务器/工作站','IBM','6888',DEFAULT,DEFAULT);
INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('玄龙精英版 笔记本散热器','笔记本配件','九州风神','',DEFAULT,DEFAULT);
INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES(' HMZ-T3W 头戴显示设备','笔记本配件','索尼','6999',DEFAULT,DEFAULT);
INSERT tdb_goods (goods_name,goods_cate,brand_name,goods_price,is_show,is_saleoff) VALUES('商务双肩背包','笔记本配件','索尼','99',DEFAULT,DEFAULT);
10.2 比较运算符子查询
mysql> SELECT goods_id, goods_name,goods_price FROM tdb_goods WHERE goods_price > (SELECT ROUND(AVG(goods_price)) FROM tdb_goods);
image.png
mysql> SELECT goods_id, goods_name, goods_price FROM tdb_goods WHERE goods_cate='超级本';
image.png
mysql> SELECT goods_id,goods_price FROM tdb_goods WHERE goods_price > ANY(SELECT goods_price FROM tdb_goods WHERE goods_cate='超级本');
image.png
mysql> SELECT goods_id,goods_price FROM tdb_goods WHERE goods_price = ANY(SELECT goods_price FROM tdb_goods WHERE goods_cate='超级本');
image.png
mysql> SELECT goods_id,goods_price FROM tdb_goods WHERE goods_price <> ALL(SELECT goods_price FROM tdb_goods WHERE goods_cate='超级本');
image.png image.png
10.3 NOT IN子查询
mysql> mysql> SELECT goods_id,goods_price FROM tdb_goods WHERE goods_price IN (SELECT goods_price FROM tdb_goods WHERE goods_cate='超级本');
image.png
mysql> mysql> SELECT goods_id,goods_price FROM tdb_goods WHERE goods= ANY(SELECT goods_price FROM tdb_goods WHERE goods_cate='超级本');
image.png
mysql> SELECT goods_id,goods_price FROM tdb_goods WHERE goods_price NOT IN (SELECT goods_price FROM tdb_goods WHERE goods_cate='超级本');
image.png
mysql> SELECT goods_id,goods_price FROM tdb_goods WHERE goods_price <>ALL(SELECT goods_price FROM tdb_goods WHERE goods_cate='超级本');
image.png
10.4 插入子句
mysql> CREATE TABLE tdb_goods_cate(
-> id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
-> goods_cate VARCHAR(40) NOT NULL
-> )CHARSET=utf8;
mysql> INSERT INTO tdb_goods_cate(goods_cate) SELECT goods_cate FROM tdb_goods GROUP BY goods_cate;
image.png10.5 多表连接
mysql> UPDATE tdb_goods INNER JOIN tdb_goods_cate ON tdb_goods.goods_cate = tdb_goods_cate.goods_cate SET tdb_goods.goods_cate = id;
mysql> CREATE TABLE tdb_goods_brand(
-> id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
-> brand_name VARCHAR(40) NOT NULL
-> )
-> SELECT brand_name FROM tdb_goods GROUP BY brand_name;
mysql> UPDATE tdb_goods AS t INNER JOIN tdb_goods_brand AS g ON t.brand_name = g.brand_name SET t.brand_name = g.id;
image.png
mysql> ALTER TABLE tdb_goods CHANGE goods_cate cate_id SMALLINT UNSIGNED NOT NULL,CHANGE brand_name brand_id SMALLINT UNSIGNED NOT NULL;
image.pngmysql> INSERT tdb_goods_cate(goods_cate) VALUES('路由器'),('交换机'),('网卡');
mysql> INSERT tdb_goods(goods_name,cate_id,brand_id,goods_price) VALUES(' LaserJet Pro P1606dn 黑白激光打印机','12','4','1849');
内连接:
image.png
mysql> SELECT goods_id,goods_name,goods_cate FROM tdb_goods INNER JOIN tdb_goods_cate ON cate_id = id;
image.png左外连接:
image.png
mysql> SELECT goods_id,goods_name,goods_cate FROM tdb_goods LEFT JOIN tdb_goods_cate ON cate_id = id;
image.png
右外连接:
image.png
mysql> SELECT goods_id,goods_name,goods_cate FROM tdb_goods RIGHT JOIN tdb_goods_cate ON cate_id = id;
image.png多表连接举例:
mysql> SELECT goods_id,goods_name,goods_cate,brand_name FROM tdb_goods AS t
INNER JOIN tdb_goods_cate as g ON t.cate_id = g.id
INNER JOIN tdb_goods_brand AS b ON t.brand_id = b.id;
自身连接:
CREATE TABLE tdb_goods_types(
type_id SMALLINT UNSIGNED PRIMARY KEY AUTO_INCREMENT,
type_name VARCHAR(20) NOT NULL,
parent_id SMALLINT UNSIGNED NOT NULL DEFAULT 0
);
INSERT tdb_goods_types(type_name,parent_id) VALUES('家用电器',DEFAULT);
INSERT tdb_goods_types(type_name,parent_id) VALUES('电脑、办公',DEFAULT);
INSERT tdb_goods_types(type_name,parent_id) VALUES('大家电',1);
INSERT tdb_goods_types(type_name,parent_id) VALUES('生活电器',1);
INSERT tdb_goods_types(type_name,parent_id) VALUES('平板电视',3);
INSERT tdb_goods_types(type_name,parent_id) VALUES('空调',3);
INSERT tdb_goods_types(type_name,parent_id) VALUES('电风扇',4);
INSERT tdb_goods_types(type_name,parent_id) VALUES('饮水机',4);
INSERT tdb_goods_types(type_name,parent_id) VALUES('电脑整机',2);
INSERT tdb_goods_types(type_name,parent_id) VALUES('电脑配件',2);
INSERT tdb_goods_types(type_name,parent_id) VALUES('笔记本',9);
INSERT tdb_goods_types(type_name,parent_id) VALUES('超级本',9);
INSERT tdb_goods_types(type_name,parent_id) VALUES('游戏本',9);
INSERT tdb_goods_types(type_name,parent_id) VALUES('CPU',10);
INSERT tdb_goods_types(type_name,parent_id) VALUES('主机',10);
-- 查找所有分类及其父类
mysql> SELECT s.type_id, s.type_name, p.type_name FROM tdb_goods_types AS s LEFT JOIN tdb_goods_types AS p ON s.parent_id = p.type_id;
image.png
-- 查找所有分类及其子类
mysql> SELECT p.type_id,p.type_name,s.type_name FROM tdb_goods_types AS p LEFT JOIN tdb_goods_types s ON s.parent_id=p.type_id;
-- 查找所有分类及其子类数目
mysql> SELECT p.type_id,p.type_name,COUNT(s.type_name) FROM tdb_goods_types AS p LEFT JOIN tdb_goods_types s ON s.parent_id=p.type_id GROUP BY p.type_name ORDER BY type_id;
--删除重复记录
mysql> SELECT goods_id, goods_name FROM tdb_goods GROUP BY(goods_name) HAVING COUNT(goods_name)>=2;
DELETE t1 FROM tdb_goods AS t1 LEFT JOIN (SELECT goods_id,goods_name FROM tdb_goods GROUP BY(goods_name) HAVING COUNT(goods_name)>=2) AS t2 ON t1.goods_name=t2.goods_name WHERE t1.goods_id > t2.goods_id;
image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png11 存储过程
11.1 不带参数的存储过程
mysql> CREATE PROCEDURE sp1() SELECT VERSION();
mysql> CALL sp1();
image.png
mysql> CALL sp1;
image.png11.2 带IN参数的存储过程
mysql> DELIMITER //
mysql> CREATE PROCEDURE removeUserById(IN USER_ID INT UNSIGNED)
-> BEGIN
-> DELETE FROM users WHERE id = USER_ID;
-> END
-> //
mysql> DELIMITER ;
image.png
mysql> CALL removeUserById(2);
image.pngmysql> DROP PROCEDURE removeUserById;删除存储过程
11.3 创建带IN和OUT类型的存储过程
image.pngmysql> DELIMITER //
mysql> CREATE PROCEDURE removeUserAndReturnNums(IN p_id INT UNSIGNED, OUT nums INT UNSIGNED)
-> BEGIN
-> DELETE FROM users WHERE id = p_id;
-> SELECT COUNT(id) FROM users INTO nums;
-> END
-> //
mysql> DELIMITER ;
mysql> CALL removeUserAndReturnNums(3, @num);
11.4 创建带IN和多个OUT类型的存储过程
image.pngmysql> CREATE PROCEDURE removeUserByAgeAndReturnInfos(IN delAge SMALLINT UNSIGNED, OUT delNum SMALLINT UNSIGNED, OUT restNum SMALLINT UNSIGNED)
-> BEGIN
-> DELETE FROM users WHERE age = delAge;
-> SELECT ROW_COUNT() INTO delNum;
-> SELECT COUNT(id) FROM users INTO restNum;
-> END
-> //
mysql> DELIMITER ;
mysql> CALL removeUserByAgeAndReturnInfos(10,@delNum,@restNum);
mysql> DROP PROCEDURE removeUserByAgeAndReturnInfos;
image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png