[TOC]
1.登錄mysql -u root -p ;
2.顯示所有數據庫show databases ;
3.使用數據庫use “database name” ;
4.顯示數據庫中所有表 show tables;
5.刪除表 drop table ”Customers“;
1.主鍵
CREATE TABLE OrderItems ( order_num int NOT NULL PRIMARY KEY, order_item int NOT NULL , prod_id char(10) NOT NULL , quantity int NOT NULL , item_price decimal(8,2) NOT NULL );
ALTER TABLE Orders ADD PRIMARY KEY (order_num);
3.唯一
4.檢查
1.多條SQL語句必須以分號(;);
2.SQL語句不區分大小寫;
3.select distinct vend_id from products;
返回不同的值
destinct
作用於所有的列
4.限制結果數量,分頁
①Oracle rownum
②MySQL limit
select * from limit m,n
m是指m+1條開始,取n條
1.order by
①select prod_name from products order by prod_name
②select prod_id , prod_price ,prod_name from products order by prod_price,prod_name;
③select prod_id , prod_price ,prod_name from products order by 2,3;
2.order by desc(降序排列)
select prod_id , prod_price ,prod_name from products order by prod_price desc;
desc致應用到直接位於其前面的列名
默認是升序排列
1.bwtween and
2.is null
3.and,or,in,not
1.必須是用 like 操作符 ,通配符只能用於文本字段
2.% (%不匹配 null)
3._
4.[]
1.select concat(vend_name , '(',vend_country,')') from vendors order by vend_name;
拼接字段,查詢的結果沒有列名
2.alias 別名 ?Oracle中沒有as
select concat(vend_name , '(',vend_country,')') as vene_title from vendors order by vend_name;
3.計算
select prod_id , quantity , item_price , quantity* item_price as expanded_price from orderitems where order_num=20008;
1.聚集函數
**count(*) 對表中的行的數目進行計數 不忽略為null的行
Count(column)對特定的列中具有值的行進行計數**
1.group by
2.having
where 過濾行 having過濾分組
where在數據分組前進行過濾,having在數據分組後進行過濾
select from where---->group by--->having--->order by
作為子查詢的select語句只能查詢單個列
select cust_name ,cust_state ,(select count(*) from orders where orders.cust_id=customers.cust_id) as orders from customers order by cust_name;
select vend_name , prod_name ,prod_price from vendors , products where vendors.vend_id=products.vend_id ;
select vend_name , prod_name ,prod_price from vendors INNER join products on vendors.vend_id=products.vend_id ;
笛卡爾積 叉聯結
1.左外聯結
select customers.cust_id ,orders.order_num from customers left outer join orders on customers.cust_id=orders.cust_id;
2.右外聯結
select customers.cust_id ,orders.order_num from customers right outer join orders on orders.cust_id=customers.cust_id;
select cust_name ,cust_contact ,cust_email from customers where cust_state in ('IL','IN','MI') union select cust_name ,cust_contact ,cust_email from customers where cust_name='Fun4A11';
規則:
1.UNION必須由2條以上的select語句組成
2.UNION的每個查詢必須包含相同的列