MySQL关键字

2020-12-19 03:16:40 编辑          阅读量:1081
  1. /*查找:*/
  2. SELECT, FROM, ORDER BY, WHERE, IS NULL, DISTINCT, BETWEEN AND, AND, OR, IN, NOT, LIKE, %, _, -- 不支持[],
  3. , 子查询, AS
  4. /*计算字段:*/
  5. TRIM, AS,
  6. /*分组*/
  7. ORDER BY -- 位置在WHERE后,Having之前,过滤用HAVING(注意与WHERE的区别,WHERE过滤行),eg:SELECT vend_id, COUNT(*) AS num_prods FROM Products WHERE prod_price >= 4 GROUP BY vend_id HAVING num_prods >= 2;
  8. /*函数*/
  9. -- 字符串处理函数
  10. Concat -- 连接表项, eg:SELECT Concat(prod_name, '(', prod_id, ')') FROM Products;
  11. UPPER, LOWER, TRIM, LTRIM, LENGTH,
  12. SOUNDEX, --返回字符串的SOUNDEX值, eg:SELECT * FROM Products WHERE SOUNDEX(prod_name) = SOUNDEX('inch teddy bear');
  13. -- 日期处理函数
  14. YEAR
  15. -- 数值处理函数
  16. ABS, COS, EXP, PI, SIN, SQRT,
  17. -- 汇集函数
  18. AVG, MAX, MIN, SUM -- 参数中使用DISTINCT表示只看不重复的行,eg:SELECT AVG(DISTINCT prod_price) AS avg_price FROM Products;
  19. COUNT -- 参数为 * 时,不忽略NULL,参数为列名时,省略该列的NULL
  20. /*内、外连接*/
  21. natural join, join .. on, left outer join, right outer join, full outer join
  22. /*组合*/
  23. UNION, UNION ALL
  24. /*插入*/
  25. INSERT INTO ... VALUES(...); # 使用时必须使用列名,可省略部分列(有默认值或可为NULL)
  26. /*更新*/
  27. UPDATE ... SET ... WEHRE
  28. # 使用同一个表的列更新另一个列
  29. UPDATE blog_article SET weight=2*likes+views;
  30. /*删除*/
  31. DELETE;
  32. TRUNCATE TABLE ...; # 清空表的内容,但不删除表
  33. DELETE FROM ...;
  34. /*表操作*/
  35. CREATE TABLE ... AS SELECT * FROM ...;
  36. CREATE TABLE ...(item CHAR(...) NULL DEFAULT ..);
  37. # create table customers (id int, name varchar(48), age int, address text, salary float);
  38. ALTER TABLE ... ADD ... # 增加列
  39. ALTER TABLE ... DROP COLUMN ... # 删除列
  40. DROP TABLE ... # 删除表
  41. RENAME
  42. # 查看表结构
  43. show create table table_name;
  44. describe table_name
  45. show full columns from table_name;
  46. /*视图*/
  47. CREATE VIEW ... AS SELECT ...
  48. /*存储过程*/ -- 重要
  49. mysql> create procedure insertInnoDB()
  50. -> begin
  51. -> set @i = 1;
  52. -> while @i <= 1000000
  53. -> do
  54. -> insert into testInnoDB(name) values(concat("wy", @i));
  55. -> set @i = @i + 1;
  56. -> end while;
  57. -> end//
  58. Query OK, 0 rows affected (0.00 sec)
  59. mysql> call insertInnoDB;
  60. /*事务处理*/
  61. # 事务 ACID Atomicity(原子性)、Consistency(稳定性)、Isolation(隔离性)、Durability(可靠性)
  62. # 事务,回退, 提交,保留点
  63. mysql> START TRANSACTION;
  64. Query OK, 0 rows affected (0.00 sec)
  65. mysql> INSERT INTO Customers(cust_id, cust_name) VALUES('1000000010', 'Toys Emporium');
  66. Query OK, 1 row affected (0.00 sec)
  67. mysql> SAVEPOINT delete1
  68. -> ;
  69. Query OK, 0 rows affected (0.00 sec)
  70. mysql> UPDATE Customers
  71. -> SET cust_name='CTG'
  72. -> WHERE cust_id='1000000010';
  73. Query OK, 1 row affected (0.00 sec)
  74. Rows matched: 1 Changed: 1 Warnings: 0
  75. mysql> ROLLBACK TO delete1;
  76. Query OK, 0 rows affected (0.02 sec)
  77. mysql> COMMIT;
  78. Query OK, 0 rows affected (0.05 sec)
  79. /*游标*/ #用的较少
  80. /*约束*/
  81. # 主键、外键
  82. CREATE TABLE Orders(
  83. order_num INTEGER NOT NULL PRIMARY KEY
  84. order_date DATETIME NOT NULL
  85. cust_id CHAR(10) NOT NULL REFERENCES Customers(cust_id)
  86. );
  87. # another method
  88. ALTER TABLE Orders
  89. ADD CONSTRAINT
  90. FOREIGH KEY(cust_id) REFERENCES Customers(cust_id);
  91. #检查约束
  92. CHECK
  93. /*触发器*/
  94. # update, insert, delete触发(可自己定义)
  95. # 效率低于约束
  96. /*索引*/
  97. CREATE INDEX prod_name_ind
  98. ON Products(prod_name);
  • 查找A、B两表中相同和不同的行
    • 查找A表中有的,而B表中无的行
      1. SELECT t1.*, t2.* from A as t1 LEFT JOIN B as t2 ON t1.`name`=t2.`name` OR t1.`name`=t2.new_name WHERE t2.`name` is NULL
    • 查找A表中无的,而B表中有的行
      把上面查询的LEFT改为RIGHT就可以
    • 查找A表和B表都有的行
      1. SELECT t1.*, t2.* from A as t1 JOIN B as t2 ON t1.`name`=t2.`name` OR t1.`name`=t2.new_name
  • 利用B表的数据更新A表的数据
    1. UPDATE A, B set A.name=B.name WHERE A.id=B.id
  • NULL 最好用is NULL来判断,尽量不用=NULL

附件:

20-things-i-learned.pdf
ccrdgr.doc
netease-cloud-music_0.9.0-1_amd64.deb

评论详情

共4条评论


火星上的西装
generic atorvastatin 20mg <a href="https://lipiws.top/">buy atorvastatin 20mg for sale</a> atorvastatin generic

匿名用户
增删改查

匿名用户
I want python

匿名用户
to think

最近访客

172.*.*.238[] 2025-04-04 09:37:16

162.*.*.8[0] 2025-04-03 11:05:01

136.*.*.212[0] 2025-04-03 08:03:03

66.*.*.36[0] 2025-04-02 09:46:59

66.*.*.36[0] 2025-04-02 08:43:10

Copyright © 2006-2021 .All Rights Reserved
粤ICP备16122044号