SHOW DATABASES;#显示所有数据库CREATE DATABASE IF NOT EXISTS db_name CHARACTER SETUTF8; # 创建库(选择字符编码)
SHOWCREATE DATABASE db_name CHARACTER SETUTF8;# 查看新建库的方式ALTER DATABASE db_name CHARACTER SETUTF8; # 修改数据库USE db_name; # 使用数据库SELECT DATABASE(); # 查看当前使用的数据库
表操作:
SHOW TABLES;
#创建表CREATE TABLE tab_name (field type [完整性约束条件] CHECK (id>0)) CHARACTER SETUTF8;--CHECK 为约束,插入更新数据时会检查,保证id是否大于0
DESCtab_name; # 查看表结构
RENAMETABLE 旧表名 TO新表名; # 重命名表ALTER TABLE tab_name ADD 列名 type [完整约束条件],ADD 列名 type [完整约束条件];ALTER TABLE tab_name DROP列名; # 删除列ALTER TABLE tab_name MODIFY 列名 type [完整约束条件] DEFAULT;ALTER TABLE tab_name CHANGE 旧列名 新列名 type; --改列名
DROP TABLE tab_name; --删表
#注释/*注释*/
--注释
SELECT语句:SELECT DISTINCT name FROMemp; #检索出不同的值(检索多列时若两列不完全相同,
所有列都会被检索出来)SELECT id FROM emp LIMIT 5 OFFSET 5; #检索从第5行起后5行ORDER BY:SELECTprod_id,prod_price,prod_nameFROMProductsORDER BYprod_price,prod_name;--仅在prod_price出现相同的值时,才会对prod_name进行排序,如果prod_price的值唯一
--则不会对prod_name排序
SELECTprod_id,prod_price,prod_nameFROMProductsORDER BY 2,3;--先对第2个列进行排序,再对第三个列进行排序
SELECTprod_id,prod_price,prod_nameFROMProductsORDER BY prod_price DESC,prod_name;--有DESC的进行升序排列,默认排列为A到Z (ASC)
SELECT prod_name as name FROMProduct;--SELECT指定别名或进行运算都不会改动原表
WHERE:SELECTvend_id,prod_nameFROMProductsWHERE vend_id <> 'DLL01';--检索除DLL01的值
--进行不匹配,匹配检索时,null值不会出现在检索结果之中
SELECTvend_id,prod_nameFROMProductsWHERE NOT vend_id = 'DLL01';SELECTprod_name,prod_priceFROMProductsWHERE prod_price BETWEEN 5 AND 10;--范围检查
SELECTprod_name,prod_priceFROMProductsWHERE prod_price IS NULL;--检索空值
SELECTvend_id,prod_price,prod_nameFROMProductsWHERE vend_id = 'DLL01' AND prod_price <=4;--ORDER BY 子句应在WHERE之后
SELECTprod_name,prod_priceFROMProductsWHERE (vend_id = 'DLL01' OR vend_id = 'BRS01') AND prod_price >= 10;--AND的优先级比OR高,一般要用圆括号分组
--过滤:
SELECTprod_name,prod_priceFROMProductsWHERE vend_id IN ('DLL01','BRS01')ORDER BYprod_name;SELECTvend_id,prod_nameFROMProductsWHERE prod_name LIKE 'Fish%';--检索prod_name中以Fish开头的值(区分大小写),但不会检索出NULL的行
--还可以这样用:'%fish%'
SELECTvend_id,prod_nameFROMProductsWHERE prod_name LIKE '_inch teddy bear';--下划线也是通配符,匹配一个字符
--函数:
SELECT Concat(vend_name,'(',RTRIM(vend_country),')') --合并
AS vend_title --去掉右边的空格
FROM Vendors ORDER BY vend_name; --LTRIM()去掉左边的空格,TRIM()去两边
SELECT quantity*item_price ASexpanded_priceFROMOrderItemsWHERE order_num = 20008;SELECT cust_name,cust_contant FROMCustomersWHERE SOUNDEX(cust_contact)=SOUNDEX('Michael Green'); --拟声
SELECTorder_numFROMOrdersWHERE YEAR(order_date) = 2012;GROUP BY:SELECT vend_id ,COUNT(*) ASnum_prodsFROMProductsGROUP BY vend_id; --要在WHERE子句之后,ORDER BY子句之前
--GROUP BY 后记得 ORDER BY
HAVING --过滤组:
SELECT cust_id,COUNT(*) ASordersFROMOrdersGROUP BYcust_idHAVING orders >=2;SELECT vend_id,COUNT(*) ASnum_prodsFROMProductsWHERE prod_price >=4
GROUP BYvend_idHAVING num_prods >=2;--子查询:
--策略:看最后一个WHERE 和各个SELECT前的检索值
SELECTcust_name,cust_contactFROMCustomersWHERE cust_id IN (SELECTcust_idFROMOrdersWHERE order_num IN (SELECTorder_numFROMOrderItemsWHERE prod_id = 'RGAN01'));--还可通过联结进行查询
SELECTcust_name,
cust_state,
(SELECT COUNT(*)FROMOrdersWHERE Orders.cust_id =Customers.cust_id)ASordersFROMCustomersORDER BYcust_name;--联结表:
--内联结:
--内联结就是将两个表并在一起,将两表中的信息同时检索出来
SELECTvend_name,prod_name,prod_priceFROM vendors INNERJION ProductsON Vendors.vend_id = Products.vend_id; --'标准写法'