管理员身份登录mysql mysql -uroot -p
退出mysql
mysql
quit
\q
修改mysql命令前置显示
prompt \u@\h \d> // root@localhost>
prompt mysql> // 默认
参数 | 描述 |
---|---|
\D | 完整的日期 |
\d | 当前数据库 |
\h | 服务器名称 |
\u | 当前用户 |
关键字与函数名称全部大写
数据库名称,表名称,字段名称全部小写写
sql语句必须以分号结尾
CREATE { DATABASE | SCHEMA } [ IF NOT EXISTS] db_name
[DEFAULT] CHARACTER SET [=] CHARSET_name
CREATE DATABASE IF NOT EXISTS test; // 创建数据库
SHOW DATABASES; // 查看数据库
SHOW CREATE DATABASE test; // 查看数据库编码方式
CREATE DATABASE IF NOT EXISTS test CHARACTER SET gbk; // 带编码方式的创建
ALTER {DATABASE | SCHEMA} [db_name]; // 修改数据库
ALTER DATABASE test CHARACTER SET = utf8; // 修改数据库编码格式
DROP {DATABASE | SCHEMA} [IF EXISTS] db_name; // 删除数据库
MySQL数据类型 | 含义 |
---|---|
tinyint(m) | 1个字节 范围(-128~127)1 字节 |
smallint(m) | 2个字节 范围(-32768~32767) |
mediumint(m) | 3个字节 范围(-8388608~8388607) |
int(m) | 4个字节 范围(-2147483648~2147483647) |
bigint(m) | 8个字节 范围(±9.22*10的18次方) |
MySQL数据类型 | 含义 |
---|---|
float(m,d) | 单精度浮点型 8位精度(4字节) m总个数,d小数位 |
double(m,d) | 双精度浮点型 16位精度(8字节) m总个数,d小数位 |
MySQL数据类型 | 含义 |
---|---|
date | 日期 ‘2008-12-2’ |
time | 时间 ‘12:25:36’ |
datetime | 日期时间 ‘2008-12-2 22:06:44’ |
timestamp | 自动存储记录修改时间 1970-01-01 00:00:00 |
MySQL数据类型 | 含义 |
---|---|
char(n) | 固定长度,最多255个字符 |
varchar(n) | 固定长度,最多65535个字符 |
tinytext | 可变长度,最多255个字符 |
text | 可变长度,最多65535个字符 |
mediumtext | 可变长度,最多2的24次方-1个字符 |
longtext | 可变长度,最多2的32次方-1个字符 |
USE db_name; // 进入数据库
SELECT DATABASES(); // 显示当前打开的数据库
// 创建表
CREATE TABLE [IF NOT EXISTS] table_name (column_name data_type);
// 示例, UNSIGNED(指的是无符号位)
CREATE TABLE IF NOT EXISTS table1 (
username VARCHAR(20),
age TINYINT UNSIGNED,
salary FLOAT(8,2) UNSIGNED
);
// 查看数据表的结构
SHOW COLUMNS FROM tbl_name;
INSERT [INTO] tbl_nama [(col_name, ...) VALUES (val, ...)];
SELECT expr,... FROM tbl_name;
SELECT * FROM table1; // 查询所有
CREATE TABLE table2 (
username VARCHAR(20) NOT NULL,
age TINYINT NULL
)
INSERT table2 (username,age) VALUES(NULL, 11); //ERROR 1048 (23000): Column 'username' cannot be null
id SMALLINT UNSIGNED AUTO_INCREMENT PRIMARY KEY, // 主键 , 自增长
// 主键是可以赋值的
// 主键不一定要和AUTO_INCREMENT一起使用
// AUTO_INCREMENT必须和PRIMARY KEY一起使用
+----------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+----------------------+------+-----+---------+----------------+
| id | smallint(5) unsigned | NO | PRI | NULL | auto_increment |
| username | varchar(30) | NO | | NULL | |
+----------+----------------------+------+-----+---------+----------------+
UNIQUE KRY
唯一约束
唯一越是可以保证记录的唯一性
唯一约束的字段可以为空值
每张数据可以保存多个唯一的约束
username VARCHAR(20) NOT NULL UNIQUE KEY
DEFAULT
sex ENUM('1','2','3') DEFAULT '3'
方法一:
标准的插入方法
INSERT [INTO] tbl_name [(col_name,...)] {VALUES | VALUE}({expr | DEFAULT}, ...), (...),...
// 示例
INSERT INTO user_list VALUES(NULL, 'Tom', '123456', 'wells tom',20);
INSERT INTO user_list VALUES(DEFAULT, 'Tom', '123456', 'wells tom',36);
INSERT INTO user_list VALUES(DEFAULT, 'Tom', '123456', 'wells tom',3*6+1);
INSERT INTO user_list VALUES(DEFAULT, 'Tom', '123456', 'wells tom',3*6+1),(DEFAULT, 'Bob', '123456', 'wells bob',3*6+1);
方法二:
INSERT INTO tbl_name SET col_name = {expr | DEFAULT };
// 示例
INSERT user_list SET username='Jack', password=‘123456’,real_name='wells jack',age=45;
方法三:
可以将查询结果写入到指定的数据表中
INSERT [INTO] tbl_name [(col_name,...)] SELECT ...
1、单表更新
UPDATE [LOW_PRIOROTY] [IGNORE] table_reference SET col_name1={expr1 | DEFAULT} [,col_name2={expr2 | DEFAULT}]... WHERE where_condition]
// 示例
UPDATE user_list set age = age + 5;
UPDATE user_list set age = age - id, sex = '1';
UPDATE user_list set age = age + 10 WHERE id % 2 = 0;
2、多表更新
DELETE FROM tbl_name [WHERE where_condition];
// 示例
DELETE FROM user_list FROM id = 6;
SELECT NOW();
SELECT VERSION();
SELECT select_expr[,select_expr] [
FROM table_references
[WHERE where_condition]
[GROUP BY {col_name | position} [ASC | DESC], ...]
[HAVING where_condition]
[ORDER BY {col_name | expr | position} [ASC | DESC], ...]
[{LIMIT {[offset,] row_count | row_count OFFSET offset}]
]
查询表达式
// 示例
SELECT *FROM user_list;
SELECT id, username FROM user_list;
SELECT username, id FROM user_list;
SELECT user_list.id, user_list.username FROM user_list;
SELECT id AS userId, username AS uname FROM user_list;
where 条件表达式
GROUP BY
查询结果分组
ASC : 升序
DESC : 降序
[ GROUP BY {col_name | position} [ASC | DESC], ...]
// 示例
SELECT sex FROM user_list GROUP BY sex;
SELECT sex FROM user_list GROUP BY 1;
HAVING 分组条件
[HAVING where_condition]
// 示例 count()聚合函数
SELECt sex, age FROM user_list GROUP BY 1 HAVing age > 35;
SELECt sex FROM user_list GROUP BY 1 HAVing count(id) >= 2;
ORDER BY
[ORDER BY {col_name | expr | position} [ASC | DESC], ...]
// 示例
SELECT * FROM user_list ORDER BY id DESC;
SELECT * FROM user_list ORDER BY age, id DESC;
LIMIT
限制查询返回结果
[{LIMIT {[offset,] row_count | row_count OFFSET offset}]
// 示例 第一个是索引
SELECT * FROM user_list LIMIT 2;
SELECT * FROM user_list LIMIT 3,2;
// 设置编码类型
SET NAMES gbk;
SET NAMES utf8;
子查询: 指的是出现在其他SQL语句内的SELECT子句
SELECT * FROM t1 WHERE col1 = (SELECT col2 FROM t2);
子查询返回值: 可以是标量、一行、一列或者子查询
=, >, <, >=, <>, !=, <=>
// 语法结构
operand comparison_operator subquery
SELECT AVG(goods_price) FROM tdb_goods; // AVG()求平均值
SELECT ROUND(AVG(goods_price)*2) FROM tdb_goods; // 四舍五入保留二位小数
SELECT good_id, good_name, good_price FROM tdb_goods WHERE good_price >= 8636.36;
// 子查询,合并查询
SELECT good_id, good_name, good_price FROM tdb_goods WHERE good_price >=
(SELECT ROUND(AVG(goods_price)*2) FROM tdb_goods);
SELECT goods_price FROM tdb_goods WHERE goods_cate = '超极本';
//
operand comparison_operator ANY subquery
operand comparison_operator SOME subquery
operand comparison_operator ALL subquery
SLECT * FROM tdb_goods WHERE goods_price >= ALL (SELECT goods_price FROM tdb_goods WHERE goods_cate = '超极本');
=ANY
运算符与 IN 等效
!= ALL
或 <>ALL
运算符 与 NOT IN
等效
operand comparison_operator [NOT] IN (subquery)
如果子查询返回任何行(结果),EXISTS将返回TRUE;否则为FALSE。
//
SELECT goods_cate FROM tdb_goods GROUP BY goods_cate;
INSERT INTO tbl_name [(col_name, ...)] SELECT ...
// 示例
INSERT tdb_goods_cates(cat_name) SELECT goods_cate FROM tdb_goods GROUP BY goods_cate;
UPDATE table_reference
SET col_name1 = {expr1 | DEFAULT }
[,col_name2 = {expr2 | DEFAULT}] ...
[WHERE where_condition]
// 语法结构
table_reference
{[INNER | CROSS] JOIN | {LEFT | RIGHT} [OUTER] JOIN}
table_reference
ON conditional_expr
UPDATE tdb_goods INNER JOIN tdb_goods_cates ON goods_cate = cate_name SET goods_cate = cate_id;
参考文章: