是一种特定目的程序语言,用于管理关系数据库管理系统(RDBMS),或在关系流数据管理系统(RDSMS)中进行流处理。html
在数据库系统中,SQL语句不区分大小写(建议用大写)mysql
但字符串常量区分大小写正则表达式
SQL语句可单行或多行书写,以“;”结尾sql
关键词不能跨多行或简写数据库
用空格和缩进来提升语句的可读性centos
子句一般位于独立行,便于编辑,提升可读性性能
注释:测试
数据库对象的命名规则优化
- 必须以字母开头 - 可包括数字和三个特殊字符(# _ $) - 不要使用MySQL的保留字 - 同一database(Schema)下的对象不能同名
DDL: Data Defination Language 数据定义语言centos7
- CREATE, DROP, ALTER
DML: Data Manipulation Language 数据操做语言
- INSERT, DELETE, UPDATE
DCL:Data Control Language 数据控制语言
- GRANT, REVOKE
DQL:Data Query Language 数据查询语言
- SELECT
CREATE DATABASE [IF NOT EXISTS] db_name; 建立数据库
CHARACTER SET 'character set name' 设置字符集类型
COLLATE 'collate name' 设置排序规则
查看支持全部字符集:SHOW CHARACTER SET;
查看支持全部排序规则:SHOW COLLATION;
MariaDB [(none)]> CREATE DATABASE IF NOT EXISTS testdb;
我不会
SHOW DATABASES;
CREATE TABLE [IF NOT EXISTS] tbl_name (create_definition,...)
MariaDB [testdb]> CREATE TABLE IF NOT EXISTS students (id TINYINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,name VARCHAR(30) NOT NULL,phone CHAR(11),gender ENUM('M','F'));
CREATE TABLE [IF NOT EXISTS] tbl_name select_statement
MariaDB [testdb]> CREATE TABLE user SELECT user,host,password FROM mysql.user;
若是只想模仿查询旧表建立一个无记录的表咱们能够加入条件
WHERE 0=1;
MariaDB [testdb]> CREATE TABLE user2 SELECT user,host,password FROM mysql.user WHERE 0=1;
CREATE TABLE [IF NOT EXISTS] tbl_name LIKE old_tbl_name
MariaDB [testdb]> CREATE TABLE user3 LIKE mysql.user;
ALTER TABLE tbl_name [alter_specification [, alter_specification] ...]
MariaDB [testdb]> ALTER TABLE students ADD age TINYINT AFTER name;
MariaDB [testdb]> ALTER TABLE students DROP phone;
MariaDB [testdb]> ALTER TABLE students CHANGE age ages TINYINT(2) NOT NULL;
MariaDB [testdb]> ALTER TABLE students MODIFY gender ENUM('M','F');
MariaDB [testdb]> DROP TABLE user3;
SHOW TABLES; 列出库中全部的表
DESC [db_name.]tb_name; 查看表结构
SHOW CREATE TABLE tbl_name; 查看建立表的命令
SHOW TABLE STATUS LIKE 'tbl_name'; 查看表状态
SHOW TABLE STATUS FROM db_name; 查看指定库中全部表状态
SHOW ENGINES; 查看全部存储引擎
MariaDB [testdb]> DESC students; #示例表 +--------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +--------+---------------------+------+-----+---------+----------------+ | id | tinyint(3) unsigned | NO | PRI | NULL | auto_increment | | name | varchar(30) | NO | | NULL | | | ages | tinyint(2) | NO | | NULL | | | gender | enum('M','F') | YES | | NULL | | +--------+---------------------+------+-----+---------+----------------+
INSERT INTO tb_name (col1,col2,...) VALUES (val1,val2,...);
MariaDB [testdb]> INSERT students(id,name,ages,gender) VALUES (1,'tom',26,'M'); MariaDB [testdb]> INSERT students(name,ages,gender) VALUES ('jerry',19,'M'); MariaDB [testdb]> INSERT students(name,ages,gender) VALUES ('maria',19,'M'); MariaDB [testdb]> INSERT students SET name='ouyangfeng',ages=56,gender='M';
INSERT INTO tb_name (col1,col2,...) VALUES (val1,val2,...)[,(val1,val2,...),...];
MariaDB [testdb]> INSERT students(name,ages,gender) VALUES ('xiaolongnv',18,'F'),('dongfangbubai',28,'F');
MariaDB [testdb]> SELECT * FROM students; +----+---------------+------+--------+ | id | name | ages | gender | +----+---------------+------+--------+ | 1 | tom | 26 | M | | 2 | jerry | 19 | M | | 3 | maria | 19 | M | | 4 | xiaolongnv | 18 | F | | 5 | dongfangbubai | 28 | F | | 6 | ouyangfeng | 56 | M | +----+---------------+------+--------+
MariaDB [testdb]> ALTER TABLE students ADD address TEXT; #加个字段作测试用
MariaDB [testdb]> INSERT students(name,address) SELECT user,host FROM mysql.user;
MariaDB [testdb]> SELECT * FROM students; +----+---------------+------+--------+-----------+ | id | name | ages | gender | address | +----+---------------+------+--------+-----------+ | 1 | tom | 26 | M | NULL | | 2 | jerry | 19 | M | NULL | | 3 | maria | 19 | M | NULL | | 4 | xiaolongnv | 18 | F | NULL | | 5 | dongfangbubai | 28 | F | NULL | | 6 | ouyangfeng | 56 | M | NULL | | 7 | root | 0 | NULL | 127.0.0.1 | | 8 | root | 0 | NULL | ::1 | | 9 | | 0 | NULL | centos7 | | 10 | root | 0 | NULL | centos7 | | 11 | | 0 | NULL | localhost | | 12 | root | 0 | NULL | localhost | +----+---------------+------+--------+-----------+
UPDATE tbl_name SET col1=value1,col2=value2,... WHERE col=value;
MariaDB [testdb]> UPDATE students SET gender='F' WHERE id=3;
MariaDB [testdb]> DELETE FROM students WHERE name=''; #删除名字为空的记录 MariaDB [testdb]> TRUNCATE TABLE user; #状况表记录
注意:必定要有限制条件(WHERE | LIMIT),不然将修改全部行的指定字段
MariaDB [testdb]> SELECT * FROM students WHERE name='maria'; #查询maria的信息 MariaDB [testdb]> SELECT * FROM students WHERE id BETWEEN 2 AND 5; #查询2到5号学生的信息 MariaDB [testdb]> SELECT * FROM students WHERE name IN ('jerry','xiaolongnv'); #查询jerry和xiaolongnv的信息 MariaDB [testdb]> SELECT * FROM students WHERE gender IS NOT NULL; #查询年龄不为空的信息 MariaDB [testdb]> SELECT * FROM students WHERE name LIKE '%o%'; #查询姓名中包含'o'的信息
MariaDB [testdb]> SELECT user AS 用户,host AS 主机,password AS 密码 FROM mysql.user;
MariaDB [testdb]> SELECT gender,AVG(ages) FROM students GROUP BY gender; #查询男生、女生年龄的平均值 MariaDB [testdb]> SELECT gender,AVG(ages) FROM students GROUP BY gender HAVING gender='M'; #只显示男生的平均年龄信息
MariaDB [testdb]> SELECT * FROM students ORDER BY ages DESC; #按年龄排序,倒序显示 MariaDB [testdb]> SELECT * FROM students WHERE ages > 0 ORDER BY ages LIMIT 3; #按年龄排序,过滤年龄大于0的,正序排序,取前三条记录
为了练习,咱们将表在扩展一下
MariaDB [testdb]> DELETE FROM students WHERE id BETWEEN 7 AND 12; MariaDB [testdb]> CREATE TABLE score (id TINYINT(2) UNSIGNED AUTO_INCREMENT PRIMARY KEY,score TINYINT(3)); MariaDB [testdb]> ALTER TABLE students ADD sid TINYINT(2); MariaDB [testdb]> UPDATE students SET sid=6 WHERE id=6; MariaDB [testdb]> INSERT score SET score=87; MariaDB [testdb]> SELECT * FROM students; +----+---------------+------+--------+---------+------+ | id | name | ages | gender | address | sid | +----+---------------+------+--------+---------+------+ | 1 | tom | 26 | M | NULL | 1 | | 2 | jerry | 19 | M | NULL | 2 | | 3 | maria | 19 | F | NULL | 3 | | 4 | xiaolongnv | 18 | F | NULL | 4 | | 5 | dongfangbubai | 28 | F | NULL | 5 | | 6 | ouyangfeng | 56 | M | NULL | 6 | +----+---------------+------+--------+---------+------+ MariaDB [testdb]> SELECT * FROM score; +----+-------+ | id | score | +----+-------+ | 1 | 99 | | 2 | 98 | | 3 | 88 | | 4 | 68 | | 5 | 78 | | 6 | 87 | +----+-------+
JOIN ON:交叉链接
INNER JOIN ON:内链接
LEFT OUTER JOIN ON:左外链接
RIGHT OUTER JOIN ON:右外链接
UNION ON:彻底外链接
MariaDB [testdb]> SELECT * FROM students AS s,score AS o WHERE s.sid=o.id; #俩张表取交集
MariaDB [testdb]> SELECT * FROM students JOIN score;
MariaDB [testdb]> SELECT t.name,s.score FROM students AS t INNER JOIN score AS s ON t.sid=s.id; +---------------+-------+ | name | score | +---------------+-------+ | tom | 99 | | jerry | 98 | | maria | 88 | | xiaolongnv | 68 | | dongfangbubai | 78 | | ouyangfeng | 87 | +---------------+-------+
MariaDB [testdb]> SELECT t.name,s.score FROM students AS t LEFT JOIN score AS s ON t.sid=s.id; #左外链接 +---------------+-------+ | name | score | +---------------+-------+ | tom | 99 | | jerry | 98 | | maria | 88 | | xiaolongnv | 68 | | dongfangbubai | 78 | | ouyangfeng | 87 | +---------------+-------+
MariaDB [testdb]> SELECT * FROM students AS t RIGHT JOIN score AS s ON t.sid=s.id; #右外链接
MariaDB [testdb]> SELECT name,address FROM students -> UNION -> SELECT user,host FROM mysql.user; +---------------+-----------+ | name | address | +---------------+-----------+ | tom | NULL | | jerry | NULL | | maria | NULL | | xiaolongnv | NULL | | dongfangbubai | NULL | | ouyangfeng | NULL | | root | 127.0.0.1 | | root | ::1 | | | centos7 | | root | centos7 | | | localhost | | root | localhost | +---------------+-----------+
MariaDB [testdb]> ALTER TABLE students ADD tid TINYINT(2); #再加一个tid字段 MariaDB [testdb]> SELECT * FROM students; +----+---------------+------+--------+---------+------+------+ | id | name | ages | gender | address | sid | tid | +----+---------------+------+--------+---------+------+------+ | 1 | tom | 26 | M | NULL | 1 | 2 | | 2 | jerry | 19 | M | NULL | 2 | 1 | | 3 | maria | 19 | F | NULL | 3 | 4 | | 4 | xiaolongnv | 18 | F | NULL | 4 | 5 | | 5 | dongfangbubai | 28 | F | NULL | 5 | 4 | | 6 | ouyangfeng | 56 | M | NULL | 6 | 4 | +----+---------------+------+--------+---------+------+------+
MariaDB [testdb]> SELECT s1.name AS studentname,s2.name AS teachername FROM students AS s1 INNER JOIN students AS s2 ON s1.id=s2.tid; +---------------+---------------+ | studentname | teachername | +---------------+---------------+ | jerry | tom | | tom | jerry | | xiaolongnv | maria | | dongfangbubai | xiaolongnv | | xiaolongnv | dongfangbubai | | xiaolongnv | ouyangfeng | +---------------+---------------+
子查询:在查询语句嵌套着查询语句,性能较差,基于某语句的查询结果再次进行的查询
MariaDB [testdb]> SELECT name,ages FROM students WHERE ages > (SELECT AVG(ages) FROM students); #查询大于平均年龄的同窗
SELECT tb_alias.col1,... FROM (SELECT clause) AS tb_alias WHERE Clause;
选择正确的数据类型对于得到高性能相当重要,三大原则:
精确数值
INT
TINYINT 微整型 1
SMALLINT 小整型 2
MEDIUMINT 中整型 3
INT 整型 4
BIGINT 大整型 8
DECIMAL 精肯定点型
近似数值
FLOAT 单精度浮点型 4
DOUBLE 双精度浮点型 8
REAL
BIT
定长
- CHAR(不区分大小写)255 - BINARY(区分大小写)
变长
VARCHAR(不区分大小写)65,535
VARBINNARY(区分大小写)
TEXT(不区分大小写)
TINYTEXT 255
TEXT 65,535
MEDIUMTEXT 16,777,215
LONGTEXT 4,294,967,295
BLOB(区分大小写)
TINYBLOB 微二进制大对象 255
BLOB 二进制大对象 64K
MEDIUMBLOB 中二进制大对象 16M
LONGBLOB 长二进制大对象 4G
ENUM 枚举 65535种变化
SET 集合 1-64个字符串,能够随意组合
DATE 3
TIME 3
DATETIME 8
TIMESTAMP 4
YEAR{2|4} 1
参考官方文档:https://dev.mysql.com/doc/refman/5.5/en/data-types.html
@^_^@ 2018.06.07 13:37