登陆数据库:
mysql -u用户名 -p密码 -P端口 -h数据库地址
修改mysql提示符:(仅本次连接有效)
方法一:执行mysql -uroot -proot -prompt \h
结果为:localhost
方法二:进入mysql后,执行PROMPT \u@\h \d>
结果为:root@localhost(数据库名)>
参数说明:
\D
完整日期
\d
当前数据库
\h
服务器名称(域名/IP)
\u
系统当前用户
库级操作:
SELECT USER();//显示当前用户
SELECT DATABASE();//显示打开的数据库
//创建数据库
//CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name [DEFAULT] CHARACTER SET [=] charset_name;
CREATE DATABASE hehe;
//查看已有的数据库信息(比如编码等)
SHOW CREATE DATABASE hehe;
//显示数据库
//SHOW {DATABASES | SCHEMA} [LIKE 'Ppattern' | WHERE expr]
SHOW DATABASES;
//修改数据库(比如编码等)
//ALTER {DATABASE | SCHEMA} [IF NOT EXISTS] db_name [DEFAULT] CHARACTER SET [=] charset_name;
//删除数据库
//DROP {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
DROP DATABASE hehe;
表级操作:
//创建表
//列名 数据类型 [数值型是否有符号] [是否可NULL] [默认值] [唯一约束] [自动递增,1开始] [主键,一张表只能有一个]
//列名 数据类型 [UNSGINED] [NULL | NOT NULL] [DEFAULT value] [UNIQUE KEY] [AUTO_INCREMENT] [PREMARY KEY]
CREATE TABLE table_name(
username varchar(20),
number int(11) unsgined,
sex ENUM(1,2,3) DEFAULT 3,
);
//查看表
SELECT TABLES [FROM 数据库名];
//查看表结构
SHOW COLUMNS FROM table_name;
//修改表名
方法一:ALTER TABLE table_name RENAME new_name
方法二:RENAME TABLE table_name TO new_name [, table_name2 TO new_name2,...]
//添加列
ALTER TABLE table_name ADD password varchar(20) NOT NULL AFTER {username | FIRST | LAST};
//删除列,并增加
ALTER TABLE table_name DROP password, DROP username, ADD id;
//修改列定义
ALTER TABLE table_name MODIFY username varchar(50) NOT NULL;
//修改列名和定义
ALTER TBALE table_name CHANGE old_col_name new_col_name [定义] [FIRST | AFTER col_name]
//列操作总结:ALTER TABLE table_name [操作],[操作]...
增、删、改、查:
=========================增=========================
//INSERT [INTO] table_name [(列名1,列名2,...)] VALUE(第一条),(第二条),...;
INSERT table_name (username, password, age) VALUE ("zhangsan", "123456", 12),("lisi", "123456", 18);
//INSERT [INTO] table_name SET username="wangwu",password="123456";只能插入单条
//INSERT [INTO] table_name [(列名1,列名2,...)] SELECT...
=========================删=========================
//DELETE FROM table_name [WHERE ...]
=========================改=========================
//UPDATE table_name SET username="zhangsan",password="654321",...[WHERE ...];
=========================查=========================
SELECT 列名 FROM 表名, where条件, group by分组条件, HAVING二级条件, order by排序, LIMIT条件
SELECT col_name[,col_name1,col_name2,...] [AS new_col_name]
[
FROM table_name
[WHERE where_condition]
[GROUP BY {col_name | position} [ASE | DESC], ...]
[HAVING where_condition]
[ORDER BY {col_name | expr | position} [ASC | DESC], ...]
[LIMIT {[offset,] row_count | row_count OFFSET offset}]
]
SET NAMES gbk;//select结果用gbk编码显示,不会改变数据库编码方式
SELECT * FROM hehe\G;//以表格的形式显示结果
子查询:
SELECT * FROM hehe WHERE id = (SELECT id FROM xixi WHERE name = "zhangsan");
假如子查询中的(...)返回结果查过一行,则需要用关键字ANY、SOME、ALL三者之一来修饰。
SELECT * FROM hehe WHERE price = ANY(SELECT price FROM xixi WHERE name = "goods");
ANY
SOME
ALL
>、>=
>min
>min
>max
=
任意值
任意值
!=
任意值
其中=和!=可以用IN和NOT IN代替:
SELECT * FROM hehe WHERE priceIN(SELECT price FROM xixi WHERE name = "goods");
使用子查询结果
//INSERT table_name (col_name1,col_name2,...) SELECT ...
连接:
语法:... [LEFT | RIGHT] JOIN 表名 ON 连接条件 ...
内连接【JOIN】
外连接:左外连接【LEFT JOIN】、右外连接【RIGHT JOIN】、全连接【无】
自身连接【内连接自身table即可】
连接的条件可以使用ON或者WHERE关键字,但是一般使用ON来定义连接条件,WHERE来定义筛选条件。
//双表连接,显式
SELECT col_name [AS resultId] FROM table_name1 AS t1 JOIN table_name2 AS t2 ON t1.id = t2.id WHERE...
UPDATE table_name1 AS a JOIN table_name2 AS b ON a.id = b.number SET a.name = b.name;
DELETE t1 FROM table_name1 AS t1 LEFT JOIN (SELECT语句...也可嵌套连接和子查询和WHERE筛选)
//双表连接,隐式,内连接可以省略JOIN,左外连接和右外连接不可省略。
SELECT col_name [AS resultId] FROM table_name1 AS t1, table_name2 AS t2 ON t1.id = t2.id WHERE...
//双表外连接,把内连接的JOIN改成LEFT JOIN或者RIGHT JOIN即可。
//多表连接
SELECT col_name FROM table_name1 AS t1
JOIN table_name2 AS t2 ON t1.id = t2.number
JOIN table_name3 AS t2 ON t1.pid = t3.number
[...]
mysql有4种数据类型(整形、浮点型、字符型、日期时间型)。