SQL 语句的学习
1. 创建数据库 和 创建表
CREATE DATABASE `sql_tutorial`;
SHOW DATABASES;
USE `sql_tutorial`;
SHOW TABLES;
DESCRIBE `student`;
SET SQL_SAFE_UPDATES = 0;
USE `sql_tutorial`;
CREATE TABLE `student`(
`student_id` INT AUTO_INCREMENT,
`name` VARCHAR(20) NOT NULL UNIQUE,
`major` VARCHAR(20) NOT NULL,
`score` INT NOT NULL,
PRIMARY KEY(`student_id`)
);
INT 整数
DECIMAL(m,n) 有小数点的数 m就是总共是几位 n就是小数点占几位 2.33
VALUES(n) 字符串 n
BLOB 图片
DATE YYYY-MM-DD 日期
TIMESTAMP YYYY-MM-DD HH:MM:SS 记录时间
PRIMARY KEY 声明这个字段为主键
NOT NULL 规定这个字段不能为空
UNIQUE 规定这个字段不能重复
DEFAULT(123546) 预设值 当我们没有规定的时候 那就自动填123456
AUTO_INCREMENT 设置整数类型的自增
DROP DATABASE `sql_tutorial`;
DROP TABLE `student`;
TRUNCATE TABLE `student`;
DELETE FROM `student`;
ALTER TABLE `student` ADD gpa DECIMAL(3,2);
ALTER TABLE `student` DROP COLUMN gpa;
ALTER TABLE `student` DROP INDEX `name`;
ALTER TABLE `student` ADD UNIQUE (name);
2. 表数据的插入
INSERT INTO `student` VALUES (1,'小白','历史',52);
INSERT INTO `student` VALUES (2,'小绿','化学',90);
INSERT INTO `student` VALUES (3,'小黄','生物',100);
INSERT INTO `student` VALUES (4,'小黑','化学',20);
INSERT INTO `student` VALUES (5,'小鬼','物理',70);
INSERT INTO `student`(`name`,`major`) VALUES('小11','HASWHAD');
3. 表数据的修改
UPDATE `student`
SET `major` = "数学"
WHERE `major` = "化学";
UPDATE `student`
SET `major` = "化学"
WHERE `student_id` = "5";
UPDATE `student`
SET `major` = "化学&历史"
WHERE `major` = "化学" OR `major` = "历史";
UPDATE `student`
SET `name` ="小红" ,`major` = "政治"
WHERE `student_id` = "5";
UPDATE `student`
SET `major` = "政治"
4. 表数据的删除
>
<
>=
<=
=
<> 不等于
DELETE FROM `student`
WHERE `student_id` =5;
5. 表数据的查询
SELECT * FROM `student`;
SELECT `name`,`major` from `student`;
SELECT * FROM `student` WHERE `major` != '历史';
SELECT * FROM `student` WHERE `major` = '数学' AND `name` = '小黑';
SELECT * FROM `student` ORDER BY `score`;
SELECT * FROM `student` ORDER BY `score` DESC;
SELECT * FROM `student` LIMIT 2;
SELECT * FROM `student` ORDER BY `score` LIMIT 2;
SELECT * FROM `student` ORDER BY `score` DESC LIMIT 2;
SELECT * FROM `student` WHERE `major` IN('数学','历史');
6. 实际案例
1. 创建表格
CREATE TABLE `employee`(
`emp_id` INT PRIMARY KEY,
`name` VARCHAR(20),
`birth_date` DATE,
`sex` VARCHAR(1),
`salary` INT,
`branch_id` INT,
`sup_id` INT
);
CREATE TABLE `branch`(
`branch_id` INT PRIMARY KEY,
`branch_name` VARCHAR(20),
`manager_id` INT,
FOREIGN KEY (`manager_id`) REFERENCES `employee`(`emp_id`)ON DELETE SET NULL
);
ALTER TABLE `employee`
ADD FOREIGN KEY(`branch_id`)
REFERENCES `branch`(`branch_id`)
ON DELETE SET NULL;
ALTER TABLE `employee`
ADD FOREIGN KEY(`sup_id`)
REFERENCES `employee`(`emp_id`)
ON DELETE SET NULL;
CREATE TABLE `client`(
`client_id` INT PRIMARY KEY,
`client_name` VARCHAR(20),
`phone` VARCHAR(20)
);
CREATE TABLE `works_with`(
`emp_id` INT,
`client_id` INT,
`total_sales` INT,
PRIMARY KEY(`emp_id`,`client_id`),
FOREIGN KEY(`emp_id`) REFERENCES `employee`(`emp_id`) ON DELETE CASCADE,
FOREIGN KEY(`client_id`) REFERENCES `client`(`client_id`) ON DELETE CASCADE
);
2. 相关查询
SELECT * FROM `employee`;
SELECT `name`,`salary` FROM `employee` ORDER BY `salary` DESC LIMIT 3;
SELECT DISTINCT `SEX` FROM `employee`;
SELECT COUNT(*) FROM `employee`;
SELECT COUNT(`sup_id`) FROM `employee`;
SELECT * FROM `employee` WHERE `birth_date` > '1970-01-01' AND `sex` = 'F';
SELECT COUNT(*) FROM `employee` WHERE `birth_date` > '1970-01-01' AND `sex` = 'F';
SELECT AVG(`salary`) FROM `employee`;
SELECT SUM(`salary`) FROM `employee`;
SELECT MAX(`salary`) FROM `employee`;
SELECT MIN(`salary`) FROM `employee`;
SELECT * FROM `employee`;
SELECT * FROM `client`;
SELECT * FROM `client` WHERE `phone` LIKE '%121';
SELECT * FROM `client` WHERE `client_name` LIKE '艾%';
SELECT * FROM `employee` WHERE `birth_date` LIKE '_____12%';
SELECT `name` FROM `employee` UNION SELECT `client_name` FROM `client`;
SELECT `name` AS `名字` FROM `employee` UNION SELECT `client_name` FROM `client`;
SELECT `emp_id`,`name`,`branch_name`
FROM `employee`
JOIN `branch`
ON `emp_id` = `manager_id`;
SELECT `emp_id`,`name`,`branch_name`
FROM `employee` LEFT JOIN `branch`
ON `emp_id` = `manager_id`;
SELECT `emp_id`,`name`,`branch_name`
FROM `employee` RIGHT JOIN `branch`
ON `emp_id` = `manager_id`;
SELECT `name`
FROM `employee`
WHERE `emp_id` =(
SELECT `manager_id`
FROM `branch`
WHERE `branch_name` = '研发'
);
SELECT `name`
FROM `employee`
WHERE `emp_id` IN(
SELECT `emp_id`
FROM `works_with`
WHERE `total_sales` > 50000
);