SQL常用语句总结

时间:2021-12-07 01:01:41

创建数据库

语法

CREATE DATABASE databasename;


创建表

:需要先选中数据库:USE databasename;

语法

CREATE TABLE tablename (

column1name description,

column2name description 

…);

举例

CREATE TABLE users (

user_id MEDIUMINT UNSIGNED NOT NULL AUTO_INCREMENT,

first_name VARCHAR(20) NOT NULL, 

last_name VARCHAR(40) NOT NULL, 

email VARCHAR(60) NOT NULL,

pass CHAR(40) NOT NULL,

registration_date DATETIME NOT NULL,

PRIMARY KEY (user_id)

);


显式表

语法

1、SHOW COLUMNS FROM tablename;

2、DESCRIBE tablename


插入记录

语法

1、INSERT INTO tablename ( column1, column2…) VALUES (value1, value2…);

2、INSERT INTO tablename VALUES (value1, NULL, value2, value3, …);

3、INSERT INTO tablename (column1, column2) VALUES (valueA, valueB), (valueC,valueD), (valueE, valueF);//多行插入

:查询中的引号规则:

1、数值不应该 用引号括住;

2、字符串必须用引号括住;

3、日期和时间必须用引号括住;

4、函数不能用引号括住;

5、单词NULL一定不能用引号括住;

如果需要在值中使用引号,可以使用另一种引号括住它;或者在引号前放置一个反斜杠对它进行转义


选择数据

语法

SELECT which_column FROM which_table;

SELECT * FROM tablename;

SELECT column1, column2 FROM tablename;

SQL常用语句总结

SQL常用语句总结

举例

SELECT NOW();

SELECT name FROM people WHERE birth_date = '2010-01-25’;

SELECT * FROM item WHERE (price BETWEEN 10.00  AND 20.00) AND (quantity > 0);

SELECT * FROM cities WHERE (zip_code = 900120) OR (zip_code = 923100);

SELECT * FROM users WHERE email IS NULL;  //NULL等于没有值,空字符串“”表示有值

SELECT user_id, first_name, last_name FROM users WHERE pass = SHA1('mypass’);

SELECT first_name, last_name FROM users WHERE (user_id < 10) OR (user_id > 20);

SELECT first_name, last_name FROM users WHERE user_id NOT BETWEEN 10 and 20;


使用LIKE 和 NOT LIKE

:下划线(_)用于匹配单个字符;百分号(%)用于匹配多个字符;

举例

SELECT * FROM users WHERE last_name LIKE ‘_Smith%’;

SELECT first_name, last_name FROM users WHERE email NOT LIKE '%@authors.com’;


查询排序结果

:ORDER BY子句的默认顺序是升序ASC;也可以加参数DESC改为降序

语法

SELECT * FROM tablename ORDER BY column;

SELECT * FROM tablename ORDER BY column DESC;

SELECT * FROM tablename ORDER BY column1, column2;  //多列排序

SELECT * FROM tablename WHERE conditions ORDER BY column;  //结合条件

举例

SELECT first_name, last_name FROM users ORDER BY last_name ASC, first_name ASC;


限制查询结果(LIMIT)

:可以与WHERE和ORDER BY一起使用LIMIT,总是将LIMIT放在查询的末尾

语法

SELECT * FROM tablename LIMIT x;  //只返回前x条子句

SELECT * FROM tablename LIMIT x, y;  //返回从x开始到x+y条,数据库记录从0开始计数

SELECT which_columns FROM tablename WHERE conditions ORDER BY column LIMIT x;

举例

SELECT first_name, last_name FROM users ORDER BY registration_date DESC LIMIT 5;


更新数据

语法

UPDATE tablename SET column=value;

UPDATE tablename SET column1=valueA, column5=valueB…;  //更新多条记录

UPDATE tablename SET column2=value WHERE column5=value;

举例

UPDATE users SET email='mike@authors.com' WHERE user_id = 18 LIMIT 1;//防止意外过多更新行


删除数据

语法

DELETE FROM tablename;

举例

DELETE FROM users WHERE user_id = 8 LIMIT 1;

补充

清空表: TRUNCATE TABLE tablename;

删除表中所有数据和表本身: DROP TABLE tablename

删除整个数据库: DROP DATABASE databasename


别名(alias)

是查询中对表或列进行符号性重命名。别名常用于表、列或函数调用。使用AS名词来创建别名

举例

SELECT first_name AS name FROM users WHERE name=‘sam’


使用函数

:函数名本身不区分大小写

语法

SELECT FUNCTION(column) FROM tablename;  //对列值应用某个函数

SELECT column1, FUNCTION(column2), column3 FROM tablename;  //对列值应用某个函数


文本函数

SQL常用语句总结

举例

SELECT CONCAT(first_name, ' ', last_name) AS Name FROM users;

SELECT CONCAT(last_name, ', ', first_name) AS Name FROM users ORDER BY Name;

SELECT LENGTH(last_name) AS L, last_name FROM users ORDER BY L DESC LIMIT 1;

:CONCAT_WS是CONCAT的派生函数,可以忽略具有NULL值的列


数字函数

SQL常用语句总结

举例

SELECT RAND();

SELECT * FROM tablename ORDER BY RAND();

SELECT CONCAT('$', FORMAT(5639.6, 2)) AS cost;

SELECT email FROM users ORDER BY RAND() LIMIT 1;

SELECT MOD(9,2);


日期和时间函数

:MySQL支持两种存储日期和时间的数据类型(DATETIME和TIMESTAMP)、一种只存储日期的类型(DATE)、一种只存储时间的类型(TIME)、一种只存储年份的类型(YEAR)

SQL常用语句总结

举例

SELECT DATE(registration_date) AS Date FROM users ORDER BY registration_date DESC LIMIT 1;

SELECT DAYNAME(registration_date) AS Weekday FROM users ORDER BY registration_date ASC LIMIT 1;

SELECT CURDATE(), CURTIME();

SELECT LAST_DAY(CURDATE()), MONTHNAME(CURDATE());

补充

ADDDATE()、