一、标识列
DROP TABLE IF EXISTS tab_identity;
CREATE TABLE tab_identity(
id INT ,
NAME FLOAT UNIQUE AUTO_INCREMENT,
seat INT
);
TRUNCATE TABLE tab_identity;
INSERT INTO tab_identity(id,NAME) VALUES(NULL,'john');
INSERT INTO tab_identity(NAME) VALUES('lucy');
SELECT * FROM tab_identity;
SHOW VARIABLES LIKE '%auto_increment%';
SET auto_increment_increment=3;
二、事务
SHOW ENGINES;
SHOW VARIABLES LIKE 'autocommit';
SET autocommit=0;
START TRANSACTION;
UPDATE ACCOUNT SET balance = 1000 WHERE username='张无忌';
UPDATE ACCOUNT SET balance = 1000 WHERE username='赵敏';
ROLLBACK;
COMMIT;
SET autocommit=0;
START TRANSACTION;
DELETE FROM ACCOUNT WHERE id=25;
SAVEPOINT a;
DELETE FROM ACCOUNT WHERE id=28;
ROLLBACK TO a;
SELECT * FROM ACCOUNT;
三、视图
SELECT stuname,majorname
FROM stuinfo s
INNER JOIN major m ON s.`majorid`= m.`id`
WHERE s.`stuname` LIKE '张%';
CREATE VIEW v1
AS
SELECT stuname,majorname
FROM stuinfo s
INNER JOIN major m ON s.`majorid`= m.`id`;
SELECT * FROM v1 WHERE stuname LIKE '张%';
USE myemployees;
SELECT e.last_name,d.department_name,j.job_title
FROM employees e
INNER JOIN departments d
ON e.department_id = d.department_id
INNER JOIN jobs j
ON e.job_id = j.job_id
WHERE e.last_name LIKE '%a%'
CREATE VIEW v1
AS
SELECT e.last_name,d.department_name,j.job_title
FROM employees e
INNER JOIN departments d
ON e.department_id = d.department_id
INNER JOIN jobs j
ON e.job_id = j.job_id
SELECT * FROM v1 WHERE last_name LIKE '%a%'
CREATE VIEW v2
AS
SELECT AVG(salary) ag,department_id
FROM employees
GROUP BY department_id;
SELECT department_id,ag,grade_level
FROM v2
INNER JOIN job_grades
ON v2.ag BETWEEN lowest_sal AND highest_sal;
SELECT *
FROM v2
ORDER BY ag
LIMIT 1
SELECT d.*,v2.ag
FROM departments d
INNER JOIN v2
ON d.department_id = v2.department_id
ORDER BY v2.ag
LIMIT 1;
SELECT * FROM myv3
CREATE OR REPLACE VIEW myv3
AS
SELECT AVG(salary),job_id
FROM employees
GROUP BY job_id;
ALTER VIEW myv3
AS
SELECT * FROM employees;
DROP VIEW emp_v1,emp_v2,myv3;
DESC v2;
SHOW CREATE VIEW v2;
CREATE OR REPLACE VIEW myv1
AS
SELECT last_name,email,salary*12*(1+IFNULL(commission_pct,0)) "annual salary"
FROM employees;
SELECT * FROM myv1;
SELECT * FROM employees;
INSERT INTO myv1 VALUES('张飞','zf@qq.com');
UPDATE myv1 SET last_name = '张无忌' WHERE last_name='张飞';
DELETE FROM myv1 WHERE last_name = '张无忌';
CREATE OR REPLACE VIEW myv1
AS
SELECT MAX(salary) m,department_id
FROM employees
GROUP BY department_id;
SELECT * FROM myv1;
UPDATE myv1 SET m=9000 WHERE department_id=10;
CREATE OR REPLACE VIEW myv2
AS
SELECT 'john' NAME;
SELECT * FROM myv2;
UPDATE myv2 SET NAME='lucy';
CREATE OR REPLACE VIEW myv3
AS
SELECT department_id,(SELECT MAX(salary) FROM employees) 最高工资
FROM departments;
SELECT * FROM myv3;
UPDATE myv3 SET 最高工资=100000;
CREATE OR REPLACE VIEW myv4
AS
SELECT last_name,department_name
FROM employees e
JOIN departments d
ON e.department_id = d.department_id;
SELECT * FROM myv4;
UPDATE myv4 SET last_name = '张飞' WHERE last_name='Whalen';
INSERT INTO myv4 VALUES('陈真','xxxx');
CREATE OR REPLACE VIEW myv5
AS
SELECT * FROM myv3;
SELECT * FROM myv5;
UPDATE myv5 SET 最高工资=10000 WHERE department_id=60;
CREATE OR REPLACE VIEW myv6
AS
SELECT last_name,email,salary
FROM employees
WHERE employee_id IN(
SELECT manager_id
FROM employees
WHERE manager_id IS NOT NULL
);
SELECT * FROM myv6;
UPDATE myv6 SET salary=10000 WHERE last_name = 'k_ing';
四、变量
SHOW GLOBAL VARIABLES;
SHOW GLOBAL VARIABLES LIKE '%char%';
SELECT @@global.autocommit;
SET @@global.autocommit=0;
SET GLOBAL autocommit=0;
SHOW SESSION VARIABLES;
SHOW SESSION VARIABLES LIKE '%char%';
SELECT @@autocommit;
SELECT @@session.tx_isolation;
SET @@session.tx_isolation='read-uncommitted';
SET SESSION tx_isolation='read-committed';
SET @变量名=值;
SET @变量名:=值;
SELECT @变量名:=值;
SET @变量名=值;
SET @变量名:=值;
SELECT @变量名:=值;
SELECT 字段 INTO @变量名
FROM 表;
SELECT @变量名;
SET @name = 'lily'
SELECT @name
DECLARE 变量名 类型;
DECLARE 变量名 类型 【DEFAULT 值】;
SET 局部变量名=值;
SET 局部变量名:=值;
SELECT 局部变量名:=值;
SELECT 字段 INTO 具备变量名
FROM 表;
SELECT 局部变量名;
作用域 定义位置 语法
用户变量 当前会话 会话的任何地方 加@符号,不用指定类型
局部变量 定义它的BEGIN END中 BEGIN END的第一句话 一般不用加@,需要指定类型
SET @m = 1;
SET @n = 2;
SET @sum = @n + @m;
SELECT @sum;
BEGIN
DECLARE n INT DEFAULT 1;
DECLARE m INT DEFAULT 1;
DECLARE SUM INT;
SET SUM = n + m;
SELECT SUM;
END
|