sql 小知识点

sql 几个要点

0.DBMS
数据库管理系统
1.distinct
针对全部,不能针对部分
2. order by

order by 放在最后
order by A, B 先按A,在A相同的时候按照B
order by 可以按照位置排序

SELECT prod_id, prod_price, prod_name FROM Products ORDER BY 2, 3;

order by 降序用 desc, 只针对前面的列。如果想在多个列上进行降序排序,必须对每一列指定 DESC 关键字。

SELECT prod_id, prod_price, prod_name FROM Products ORDER BY prod_price DESC, prod_name DESC;

3.between and

包括两端
Not between 不包括两端

4.通配符

% 代表0,1,or more,不包括NULL
_ 代表一个

5.聚集函数

AVG() 返回某列的平均值

  • 只能作用于单列
  • 默认忽略NULL
    COUNT() 返回某列的行数
  • 如果指定列名,则 COUNT()函数会忽略指定列的值为空的行,但如果

COUNT()函数中用的是星号(*),则不忽略。
MAX() 返回某列的最大值
MIN() 返回某列的最小值
SUM() 返回某列值之和
6.union
连接2条或2条以上的sql, union all 返回重复行
7.表间复制
CREATE TABLE CustCopy AS SELECT * FROM Customers;
INSERT INTO Customers(cust_id, cust_contact, cust_email, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country) SELECT cust_id, cust_contact, cust_email, cust_name, cust_address, cust_city, cust_state, cust_zip, cust_country FROM CustNew;

8.update
update table set 列名=** where ....;

9.存储过程
存储过程就是为以后使用而保存的一条 或多条 SQL 语句。可将其视为批文件,虽然它们的作用不仅限于批处理。使用存储过程有三个主要的好处,即简单、安全、高性能

猜你喜欢

转载自blog.csdn.net/qq_43019193/article/details/106147626