该博文与另一篇博文(有运行截图)相联系:mysql基本操作截图+sql语句+注释
即是另一篇博文的源sql语句
# ##############创建hero表并插入数据##########
DROP TABLE IF EXISTS `hero`;
CREATE TABLE `hero` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`sex` char(1) DEFAULT NULL,
`price` int(11) DEFAULT NULL,
`type_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=18 DEFAULT CHARSET=utf8;
INSERT INTO `hero`
(`id`,`name`,`age`,`sex`,`price`,`type_id`)
VALUES
(1,'李白',18,'男',18888,2),
(2,'孙尚香',17,'女',6888,4),
(3,'韩信',15,'男',NULL,2),
(4,'小乔',5,'女',NULL,3),
(5,'西施',5,'女',13888,3),
(7,'张飞',1,'男',13888,5),
(8,'花木兰',1,'女',NULL,1),
(9,'虞姬',16,'女',13888,4),
(10,'凯',NULL,NULL,13888,1),
(11,'孙策',NULL,NULL,NULL,NULL),
(12,'曹操',30,'男',NULL,4),
(13,'孙膑',5,NULL,NULL,5);
# ##################创表插入数据完成##############
-- ——————————————————crud语法————————————————————
-- 增
INSERT INTO hero VALUES(14,'杨玉环',18,'女',13888,3);
INSERT INTO hero (name,price,type_id) VALUES('马可波罗',13888,4);
INSERT INTO hero
VALUES
(16,'露娜',18,'女',NULL,2),
(17,'张良',20,'男',NULL,3);
-- 删
DELETE FROM hero WHERE`name`='张良';
DELETE FROM hero ;
DELETE FROM hero WHERE 1=1 ;
DELETE FROM hero WHERE TRUE ;
-- 改
UPDATE hero SET `name`='百里守约' WHERE `name`='马可波罗';
-- 查
SELECT * FROM hero ;
SELECT * FROM hero WHERE sex = '男' ;
SELECT name,id,price ,sex FROM hero WHERE sex = '男';
SELECT * FROM hero WHERE `name`='百里守约';
SELECT * FROM hero WHERE name='百里守约';
SELECT * FROM hero WHERE NAME='百里守约';
-- ——————————————常用查询——————————————-———--
-- 条件查询
SELECT * FROM hero WHERE age>5 AND age<20;
SELECT * FROM hero WHERE age BETWEEN 5 AND 20;
SELECT * FROM hero WHERE age IN (5,16,17);
-- 聚合函数
-- MAX 与 MIN 最大最小
SELECT MIN(age) FROM hero ;
SELECT * FROM hero WHERE age = (SELECT MIN(age) FROM hero) ;
-- SUM函数
SELECT SUM(age) FROM hero;
-- COUNT 函数
SELECT COUNT(*) FROM hero;
-- AVG 函数
SELECT AVG(age) FROM hero WHERE sex = '男';
-- 排序查询
SELECT * FROM hero ORDER BY age;
SELECT * FROM hero ORDER BY age DESC;
-- 模糊查询
-- 查询姓名为三个字的英雄:(三个下划线)
SELECT * FROM hero WHERE `name` LIKE '___';
-- 查询姓李的英雄:
SELECT * FROM hero WHERE `name` LIKE '李%';
-- 查询姓名至少为三个字的英雄:
SELECT * FROM hero WHERE `name` LIKE '___%';
-- 分页查询
SELECT * FROM hero LIMIT 3,2 ;
# ################################新建type表 ,插入五条数据################################
-- 如果存在tpye表就删除
DROP TABLE IF EXISTS `tpye`;
-- 创建type表
CREATE TABLE `type` (
`tid` int(11) NOT NULL AUTO_INCREMENT,
`tname` varchar(20) DEFAULT NULL,
`route` varchar(50) DEFAULT NULL,
PRIMARY KEY (`tid`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;
-- 插入数据
INSERT INTO
`type`(`tid`,`tname`,`route`)
VALUES
(1,'战士','上单'),
(2,'刺客','打野'),
(3,'法师','中路'),
(4,'射手','下路'),
(5,'辅助','下路');
# ############################################建表操作完成####################################
-- ————————————多表操作————————————————————————
-- 内连接
-- 笛卡尔积 h与t是hero表与type表的别名
-- mysql“方言版”
SELECT * FROM hero h, type t ;
-- 标准版
SELECT * FROM hero h INNER JOIN type t ;
-- 从笛卡尔积中取出指定的行 (mysql“方言版”)
SELECT * FROM hero h, type t WHERE h.type_id = t.tid ;
-- 从笛卡尔积中取出指定的行 (标准版)
SELECT * FROM hero h INNER JOIN type t ON h.type_id = t.tid ;
# #########向type表中插入一行数据,方便观察##############
INSERT INTO type VALUES(6,'新插入的','方便外连接观察');
# ##############插入完毕#######################
-- 外连接
-- 左外连接
SELECT * FROM hero h LEFT JOIN type t ON h.type_id = t.tid;
-- 右外连接
SELECT * FROM hero h RIGHT JOIN type t ON h.type_id = t.tid;
-- 全外连接
SELECT * FROM hero h LEFT JOIN type t ON h.type_id = t.tid
UNION
SELECT * FROM hero h RIGHT JOIN type t ON h.type_id = t.tid;