SQL(Structured Query Language,结构化查询语言)是一种专门用来与数据库通信的语言,其并非是编程语言。 SQL 的优点:
SQL 并不指定某一个 DBMS,在大多数 DBMS 中 SQL 都是通用的(但是不同的 DBMS 可能有不同的实现) SQL 语法简单 SQL 能够进行复杂的数据库操作
MySQL 是一个 RDBMS,即关系数据库管理系统,广泛应用于各个领域,它的主要特点有:
这里给出常用的参数:
-u // 指定用户名
-p // 指定密码
-P // 指定端口
-h // 指定主机名
使用help命令查看所有命令:
mysql> help
For information about MySQL products and services, visit:
http://www.mysql.com/
For developer information, including the MySQL Reference Manual, visit:
http://dev.mysql.com/
To buy MySQL Enterprise support, training, or other products, visit:
https://shop.mysql.com/
List of all MySQL commands:
Note that all text commands must be first on line and end with ';'
? (\?) Synonym for `help'.
clear (\c) Clear the current input statement.
connect (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
ego (\G) Send command to mysql server, display result vertically.
exit (\q) Exit mysql. Same as quit.
go (\g) Send command to mysql server.
help (\h) Display this help.
notee (\t) Don't write into outfile.
print (\p) Print current command.
prompt (\R) Change your mysql prompt.
quit (\q) Quit mysql.
rehash (\#) Rebuild completion hash.
source (\.) Execute an SQL script file. Takes a file name as an argument.
status (\s) Get status information from the server.
system (\!) Execute a system shell command.
tee (\T) Set outfile [to_outfile]. Append everything into given outfile.
use (\u) Use another database. Takes database name as argument.
charset (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings (\W) Show warnings after every statement.
nowarning (\w) Don't show warnings after every statement.
resetconnection(\x) Clean session context.
For server side help, type 'help contents'
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
4 rows in set (0.01 sec)
mysql> use [db_name];
Database changed
mysql> show tables;
mysql> show tables from [db_name]; // 查询指定数据库中的所有可用表
+-------------------+
| Tables_in_acgfate |
+-------------------+
| accounts |
| users |
+-------------------+
2 rows in set (0.01 sec)
mysql> show columns from [table];
+------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+------------------+------+-----+---------+----------------+
| id | bigint unsigned | NO | PRI | NULL | auto_increment |
| created_at | datetime(3) | YES | | NULL | |
| updated_at | datetime(3) | YES | | NULL | |
| deleted_at | datetime(3) | YES | MUL | NULL | |
| username | longtext | YES | | NULL | |
| password | longtext | YES | | NULL | |
| nickname | longtext | YES | | NULL | |
| mail | longtext | YES | | NULL | |
| avatar | longtext | YES | | NULL | |
| gender | longtext | YES | | NULL | |
| level | tinyint unsigned | YES | | NULL | |
| join_time | datetime(3) | YES | | NULL | |
| silence | tinyint(1) | YES | | NULL | |
+------------+------------------+------+-----+---------+----------------+
13 rows in set (0.01 sec)
查询 简单查询 按列查询
SELECT [column] FROM [table];
SELECT [column1], [column2] FROM [table];
SELECT * FROM [table];
表中某些列的数据可能是存在重复的,使用DISTINCT关键字可以查询不重复的单列记录:
SELECT DISTINCT [column] FROM [table]
city | provience |
---|---|
A1 | LA |
A2 | LA |
A1 | LB |
使用LIMIT子句可以限定查询的范围,而不是查询整个表的记录:
SELECT [column] FROM [table] LIMIT 5; // 查询前5行
SELECT [column] FROM [table] LIMIT 5, 5 // 查询从第6行开始的5条记录
SELECT [table].[column] FROM [table]
按指定列排序
SELECT [column1] FROM [table] ORDER BY [column2]
SELECT [column1] FROM [table] ORDER BY [column2] DESC // 按降序排序
SELECT [column1] FROM [table] ORDER BY [column2], [column3]
SELECT [column1] FROM [table] ORDER BY [column2] DESC, [column3]