一、 SQL语言
DDL语句:数据定义语言,主要是进行定义/改变表的结构、数据类型、表之间的链接等操作。常用的语句关键字有 CREATE、DROP、ALTER 等。 DML语句:数据操纵语言,主要是对数据进行增加、删除、修改操作。常用的语句关键字有 INSERT、UPDATE、DELETE 等。 DQL语句:数据查询语言,主要是对数据进行查询操作。常用关键字有 SELECT、FROM、WHERE 等。 DCL语句:数据控制语言,主要是用来设置/更改数据库用户权限。常用关键字有 GRANT(授权)、REVOKE(取消权限) 等。
二、操作数据库
创建:create databases 数据库名 删除:drop database 数据库名 选择:use 数据库名
三、操作数据表
CREATE TABLE table_name ( column_name column_type) ;
CREATE TABLE IF NOT EXISTS ` nowcoder_tbl` (
` nowcoder_id` INT UNSIGNED AUTO_INCREMENT ,
` nowcoder_title` VARCHAR ( 100 ) NOT NULL ,
` nowcoder_author` VARCHAR ( 40 ) NOT NULL ,
` submission_date` DATE ,
PRIMARY KEY ( ` nowcoder_id` )
) ENGINE = InnoDB DEFAULT CHARSET = utf8;
删除数据表
drop table 数据表 DROP TABLE IF EXISTS 数据表 修改数据表类型
mysql> ALTER TABLE testalter_tbl ENGINE = MYISAM;
mysql> SHOW TABLE STATUS LIKE 'testalter_tbl' \G
mysql> ALTER TABLE testalter_tbl RENAME TO alter_tbl;
四、操作数据
INSERT INTO table_name ( field1, field2, . . . fieldN ) VALUES
( value1, value2, . . . valueN ) ;
INSERT INTO nowcoder_tbl
- > ( nowcoder_title, nowcoder_author, submission_date)
- > VALUES
- > ( "学习 PHP" , "牛客教程" , NOW ( ) ) ;
INSERT INTO nowcoder_tbl
- > ( nowcoder_title, nowcoder_author, submission_date)
- > VALUES
- > ( "JAVA 教程" , "NOWCODER.COM" , "2019-10-06" ) ;
UPDATE table_name SET field1= new- value1, field2= new- value2 [ WHERE Clause]
你可以同时更新一个或多个字段。
你可以在 WHERE 子句中指定任何条件。
你可以在一个单独表中同时更新数据。
当你需要更新数据表中指定行的数据时 WHERE 子句是非常有用的。
update nowcoder_tbl set nowcoder_title= "学习 C++" where nowcoder_id= 1 ;
删除数据
truncate和delete的区别
truncate 和 delete 都可以清除数据表的所用数据,但 truncate 比 delete 的速度快,且使用的 系统和事务日志资源少。 truncate没有 where 的条件筛选,只能单独使用,delete 不仅可以单独而且还可以和 where 搭配, 从而实现删除单条或多条数据。 删除的实现原理不同。truncate 是通过释放存储表数据所使用的数据页来进行数据的删除,并且只在 事务日志中记录页的释放。delete 语句每删除一行就在事务日志中为所删除的每行记录一项。
DELETE FROM table_name [ WHERE Clause]
delete from nowcoder_tbl where nowcoder_id = 3 ;
truncate table user_table;
五、查询数据
SELECT column_name, column_name FROM table_name [ WHERE Clause] [ LIMIT N] [ OFFSET M]
查询语句中你可以使用一个或者多个表,表之间使用逗号( , ) 分割,并使用 WHERE 语句来设定查询条件。
SELECT 命令可以读取一条或者多条记录。
你可以使用星号(* )来代替其他字段, SELECT 语句会返回表的所有字段数据
你可以使用 WHERE 语句来包含任何条件。
你可以使用 LIMIT 属性来设定返回的记录数。
你可以通过 OFFSET 指定 SELECT 语句开始查询的数据偏移量。默认情况下偏移量为0 。
select * from nowcoder_tbl;
where 语句
SELECT field1, field2, . . . fieldN FROM table_name
WHERE field1 LIKE condition1 [ AND [ OR ] ] filed2 = "somevalue"
你可以在 WHERE 子句中指定任何条件。
你可以在 WHERE 子句中使用LIKE 子句。
你可以使用LIKE 子句代替等号 = 。
LIKE 通常与 % 一同使用,类似于一个元字符的搜索。
你可以使用 AND 或者 OR 指定一个或多个条件。
你可以在 DELETE 或 UPDATE 命令中使用 WHERE . . . LIKE 子句来指定条件。
select * from nowcoder_tbl where nowcoder_author like "%COM" ;
UNION操作符
MySQL UNION 操作符用于连接两个以上的 SELECT 语句的结果组合到一个结果集合中。多个 SELECT 语句会删除重复的数据。
SELECT expression1, expression2, . . . expression_n FROM tables [ WHERE conditions]
UNION [ ALL | DISTINCT ]
SELECT expression1, expression2, . . . expression_n FROM tables [ WHERE conditions] ;
expression1, expression2, . . . expression_n: 要检索的列。
tables : 要检索的数据表。
WHERE conditions: 可选, 检索条件。
DISTINCT : 可选,删除结果集中重复的数据。默认情况下 UNION 操作符已经删除了重复数据,所以 DISTINCT 修饰符对结果没啥影响。
ALL : 可选,返回所有结果集,包含重复数据。
SELECT country FROM Websites
UNION ALL
SELECT country FROM apps
ORDER BY country;
SELECT country, name FROM Websites
WHERE country= "CN"
UNION ALL
SELECT country, app_name FROM apps
WHERE country= "CN"
ORDER BY country;
SELECT field1, field2, . . . fieldN FROM table_name1, table_name2. . .
ORDER BY field1 [ ASC [ DESC ] [ 默认 ASC ] ] , [ field2. . . ] [ ASC [ DESC ] [ 默认 ASC ] ]
你可以使用任何字段来作为排序的条件,从而返回排序后的查询结果。
你可以设定多个字段来排序。
你可以使用 ASC 或 DESC 关键字来设置查询结果是按升序或降序排列。 默认情况下,它是按升序排列。
你可以添加 WHERE . . . LIKE 子句来设置条件。
select * from nowcoder_tbl order by submission_date asc ;
GROUP BY
GROUP BY 语句根据一个或多个列对结果集进行分组。 在分组的列上我们可以使用 COUNT, SUM, AVG,等函数。 使用with rollup:实现在分组统计数据基础上再进行相同的统计(SUM,AVG,COUNT…)。
SELECT column_name, function ( column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;
mysql> SELECT name, COUNT ( * ) FROM employee_tbl GROUP BY name;
+
| name | COUNT ( * ) |
+
| 小明 | 3 |
| 小王 | 2 |
| 小丽 | 1 |
+
mysql> SELECT name, SUM ( singin) as singin_count FROM employee_tbl GROUP BY name WITH ROLLUP ;
+
| name | singin_count |
+
| 小丽 | 2 |
| 小明 | 7 |
| 小王 | 7 |
| NULL | 16 |
+
select coalesce ( a, b, c) ;
mysql> SELECT coalesce ( name, "总数" ) , SUM ( singin) as singin_count FROM employee_tbl GROUP BY name WITH ROLLUP ;
+
| coalesce ( name, "总数" ) | singin_count |
+
| 小丽 | 2 |
| 小明 | 7 |
| 小王 | 7 |
| 总数 | 16 |
+
连表查询
INNER JOIN(内连接,或等值连接)
获取两个表中字段匹配关系的记录。(可省略inner 直接使用 join )
mysql> SELECT a. nowcoder_id, a. nowcoder_author, b. nowcoder_count FROM nowcoder_tbl a INNER JOIN tcount_tbl b ON a. nowcoder_author = b. nowcoder_author;
mysql> SELECT a. nowcoder_id, a. nowcoder_author, b. nowcoder_count FROM nowcoder_tbl a, tcount_tbl b where a. nowcoder_author= b. nowcoder_a
+
| nowcoder_id | nowcoder_author | nowcoder_count |
+
| 1 | 牛客教程 | 10 |
| 2 | 牛客教程 | 10 |
| 3 | NOWCODER. COM | 20 |
| 4 | NOWCODER. COM | 20 |
+
LEFT JOIN(左连接):
获取左表所有记录,即使右表没有对应匹配的记录。( LEFT JOIN 会读取左边数据表的全部数据,即便右边表无对应数据。)
mysql> SELECT a. nowcoder_id, a. nowcoder_author, b. nowcoder_count FROM nowcoder_tbl a LEFT JOIN tcount_tbl b ON a. nowcoder_author = b. nowcoder_author;
+
| nowcoder_id | nowcoder_author | nowcoder_count |
+
| 1 | 牛客教程 | 10 |
| 2 | 牛客教程 | 10 |
| 3 | NOWCODER. COM | 20 |
| 4 | NOWCODER. COM | 20 |
| 5 | FK | NULL |
+
RIGHT JOIN(右连接):
获取右表所有记录,即使左表没有对应匹配的记录。(RIGHT JOIN 会读取右边数据表的全部数据,即便左边边表无对应数据。)
mysql> SELECT a. nowcoder_id, a. nowcoder_author, b. nowcoder_count FROM nowcoder_tbl a RIGHT JOIN tcount_tbl b ON a. nowcoder_author = b. nowcoder_author;
+
| nowcoder_id | nowcoder_author | nowcoder_count |
+
| 1 | 牛客教程 | 10 |
| 2 | 牛客教程 | 10 |
| 3 | NOWCODER. COM | 20 |
| 4 | NOWCODER. COM | 20 |
| NULL | NULL | 22 |
+
insert into nowcoder_test_tbl( nowcoder_author, nowcoder_count) values ( "牛客教程" , null ) ;
IS NULL : 当列的值是 NULL , 此运算符返回 true 。
IS NOT NULL : 当列的值不为 NULL , 运算符返回 true 。
<=> : 比较操作符(不同于= 运算符),当比较的的两个值为 NULL 时返回 true 。
select * from nowcoder_test_tbl where nowcoder_count is not null ;