MYSQL学习整理|高级SQL语句

MYSQL的三种语言:

DDL DML DCL

DML(data manipulation language):

它们是SELECT、UPDATE、INSERT、DELETE,就象它的名字一样,这4条命令是用来对数据库里的数据进行操作的语言

DDL(data definition language):

DDL比DML要多,主要的命令有CREATE、ALTER、DROP等,DDL主要是用在定义或改变表(TABLE)的结构,数据类型,表之间的链接和约束等初始化工作上,他们大多在建立表时使用

DCL(Data Control Language):

是数据库控制功能。是用来设置或更改数据库用户或角色权限的语句,包括(grant,deny,revoke等)语句。在默认状态下,只有sysadmin,dbcreator,db_owner或db_securityadmin等人员才有权力执行DCL

数据库模型:

概念模型,逻辑模型,物理模型 ER图
数据库设计的步骤:
需求分析,概念结构设计,逻辑结构设计,物理结构设计,数据库实施,数据库运行和维护
数据库的引擎决定了数据在数据库中的存储方式

数学函数:

ABS SUM AVG MAX MIN FLOOR(返回小于或等于x的最大整数) RAND TRUNCATE SQRT(平方根)
UPPER UCASE LEFT SUBSTRINGSORT COUNT ASCII CHAR
NOW YEAR CURDATE CURRENT_DATE CURTIME CURRENT_TIME LOCALTIME SYSDATE LOCALTIMESTAMP
ENCODE ENCRYPT IF IFNULL FORMAT CAST USER VERSION

关于整个数据库的操作:

CREATE ALTER DROP
SHOW DATABASE
SHOW TABLES FROM db_school

修改数据库:

ALTER TABLE … ADD … VARCHAR
e.g. ALTER TABLE … ADD COLUMN id INT NOT NULL UNIQUE AUTO_INCREMENT FIRST;
CHANGE 同时修改列名和数据类型
ALTER
MODIFY 只修改数据类型,不会干涉列名

主键外键的设计:

CONSTRAINT FK_student FOREIGN KEY (classNo) REFERENCES tb_class(classNo)
CONSTRAINT PK_student ORIMARY KEY(studentNo)
DROP FOREIGN KEY
DROP PRIMARY KEY
DROP INDEX KEY
ALTER TABLE ADD … CONSTRAINT …

使用SELECT FROM WHERE可能用到的其他关键字:

NOT
IN
NOT IN
LIKE ‘王%’
IS NULL
AND OR
ORDER BY
GROUP BY…HAVING

数据更新:

INSERT INTO … VALUES …
REPLACE INTO … VALUES…
UPDATE …SET … WHERE…
DELETE FROM… WHERE…
TRUNCATE

查询修改索引:

SHOW INDEX FROM …
CREATE TABLE … INDEX(studentname)
CREATE INDEX index_stu ON db.tb(studentNo)
ALTER TABLE … ADD INDEX …
DROP INDEX … ON …

视图:

作用:集中分散的data,简化查询语句,重用SQL语句,保护DATA SAFE,共享所需数据,更改数据样式
创建视图不能再FROM字句中使用子查询
视图不可以添加索引,触发器,默认值

视图的相关操作:

创建视图:
CREATE OR REPLACE VIEW db.v AS
SELECT * FROM db.tb WHERE …
WITH CHECK OPTION;
删除视图:
DROP VIEW IF EXISTS db.v
修改视图:
ALTER VIEW db.v AS
SELECT * FROM db.tb WHERE …
WITH CHECK OPTION;
INSERT INTO db.v VALUES ();
SELECT …FROM db.v
UPDAPTE db.v SET …
DELETE FROM … WHERE

触发器相关:

CREATE TRIGGER db.tbtrigger AFTER INSERT
ON db.tb FOR EACH ROW SET @str=’one student added’// SET NEW.nation= OLD.native
DROP TRIGGER IF EXISTS …

事件相关:

DELIMITER $$
CREATE EVENT IF NOT EXISTS event_insert
ON SCHEDULE EVERY 1 MONTH
STARTS CURDATE()+INTERVAL 1 MONTH
ENDS ‘2016-12-31’
DO
BEGIN
IF YEAR(CURDATE())<2013 THEN
INSERT INTO tb_student
VALUES();
END IF;
END $$

ALTER EVENT event_insert DISABLE;
ALTER EVENT event_insert ENABLE;
ALTER EVENT event_insert RENAME TO e_insert;
DROP EVENT IF EXISTS event_name;

存储过程相关:

增强SQL功能和灵活性,封装,高性能,减少网络流量,作为一种安全机制
SHOW PROCEDUCE STATUS 查看数据库中存在哪些存储过程
DROP PROCEDUCE FUNCTION IF EXISTS sp_name

DELIMITER $$
CREATE PROCEDUCE sp_update_sex(IN sno CHAR(20),IN ssex CHAR(2))
BEGIN
UPDATE tb SET sex=ssex WHERE studentNo = sno;
END $$

存储函数:

USE DB_SCHOOL
DELIMITER $$
CREATE FUNCTION fn_search (sno CHAR(10))
RETURNS CHAR(2)
DETERMINISTIC
BEGIN
DECLARE SSEX CHAR(2);
SELECT sex INTO SSEX FROM tb_student
WHERE studentNo = sno;
IF SSEX IS NULL THEN
RETURN (SELECT ‘没有该生’);
ELSE IF SSEX = ‘女’THEN
RETURN (SELECT ‘女’);
ELSE RETURN (SELECT ‘男’);
END IF;
END IF;
END $$

SELECT fn_search(‘2013210101’)
DROP FUNCTION IF EXISTS fn_search

游标相关:

DECLARE cursor_name CURSOR FOR (SELECT语句)
OPEN cursor_name
FETCH cursor_name INTO var_name
CLOSE cursor_name

访问控制和安全管理:

SELECT PASSWORD(‘456’)
CREATE USER ‘zhangsan’@’localhost’
INENTIFIED BY ‘123’;

DROP USER zhangsan@ localhost;
RENAME USER … TO …
SET PASSWORD FOR ‘wangwu’@‘localhost’=’xxxxxxxx’;
账户权限:
SHOW GRANTS FOR ‘zhangsan’@’localhost’
GRANT SELECT,UPDATE
ON db.tb
TO ‘liming’@‘localhost’ INENTIFIED BY ‘123’;

GRANT ALL ON … TO …;
回收权限:
REVOKE SELECT
ON db.tb
FROM ‘liming’@‘localhost’;

备份和恢复:

SELECT * FROM db.tb
INTO OUTFILE ‘c:\backup\backupfile,txt’
FIELDS TERMINATED BY’,’
OPTIONALLY ENCLOSED BY ‘”’
LINES TERMINATED BY ‘?’;

LOAD DATA INFILE …
INTO TABLE …
FIELDS TERMINATED BY’,’
OPTIONALLY ENCLOSED BY ‘”’
LINES TERMINATED BY ‘?’;

    原文作者:lasebella
    原文地址: https://www.jianshu.com/p/a9f0c63d7601
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞