SQL基本操作

  • mysql.exe :the mysql command-line tool
  • mysqld.exe : the mysql server

connect to SQLServer

> mysql -h 127.0.0.1 -P3306 -u root  -p54321
> mysql -u root -p
> 54321

comman show

  • show
    • databases
    • tables
    • column from ‘table’ –describe ‘table’
    • create database | create table
    • grants
    • errors | warnings

Date-Manipulation Language

SELECT

comman select

select distinct *
from table
order by prod-name, prod-price DESC
limit 5
  • AS , Function
  • where, Group by, Having(like where), Order by, limit
select vend_id, Count(*) AS num_prods
FROM products
WHERE prod-price >= 10
GROUP BY vend_id
HAVING COUNT(*) >= 2

select vend_id, Count(*) AS num_prods
from products
Group by vend_id
Having Cunt(*) >= 2;

select order_num, SUM(quantity*item_price) AS ordertotal
from orderotems
Group by order_num
Having SUM(quantity*item_price) >= 50
Order by ordertotal;

comman where

  • operator : + … ,between and
  • and, or, in, not(in,between,exists)
  • like ‘%’, ’ _ ‘
  • regexp binary ‘[123]’, ‘.’, ‘^’, ‘$’

  • UNION : 針對多個where條件,當需要保留where條件中的重複(所用條件同時滿足),必須用UNION

function

  • 文本處理函數:RTrim(), LTrim(), Concat()
  • 日期和時間處理函數:
  • 數值處理函數: Abs(), Cos(), Sin(), Tan(), Exp(), Sqrt(), Mod(), Pi(), Rand()
  • aggregate function: [distinct]
    • AVG()–ignore null–,
    • COUNT()–COUNT(* )
    • MAX(), MIN(), 對數值和日期,或者相應排序的文本
    • SUM()

鑲嵌子查詢、聯結表(natual join, outer join)

  • 鑲嵌子查詢、聯結表
#
SELECT cust_name, cust_contact
FROM customers
WHERE cust_id IN (SELECT cust_id
                  FROM orders
                  WHERE order_num IN ( SELECT order_num
                                       FROM orderitems
                                       WHERE prod_id = 'TNT2'))

SELECT cust_name, cust_contact
FROM customers, orders, orderitems
WHERE customers.cust_id = orders.cust_id
  AND orderotems.order_num = orders.order_num
  AND prod_id = 'TNT2';
  • 內外聯結
#列出所有產品以及訂購數量
SELECT customers.cust_id, orders.order_num
FROM customers INNER JOIN orders
  ON customer.cust_id = orders.cust_id;

#列出所有產品以及訂購數量,包括那些至今尚未下訂單的客戶
SELECT customers.cust_id, orders.order_num
FROM customers LEFT OUTER JOIN orders
  ON customers.cust_id = orders.cust_id;
  • 使用聚集函數
#對客戶下了多少訂單進行統計,包括沒有人訂購的產品
SELECT customers.cust_name, customers.cust_id, COUNT(orders.order_num) AS num_ord
FROM customers LEFT OUTER JOIN orders
  ON customers.cust_id = orders.cust_id
GROUP BY customers.cust_id ;

全文本收索–索引

SELECT note_text
FROM productnotes
WHERE Match(note_text) Against('rabbit');

SELECT note_text,
      Match(note_text) Against('rabbit') AS rank
FROM productnotes;
  • 布爾文本搜索

INSERT

INSERT INTO customers
VALUES(NULL, ' ', )

UPDATE, DELETE

UPDATE customers
SET cust_email = NULL
WHERE cust_in = 10000;

DELETE FROM customers
WHERE cust_id = 10006;

TRUNCATE TABLE customers

Date-Definition Language

創建和操縱表

  • 創建

  • 更新

ALTER TABLE vendors
ADD vend_phone CHAR(20);

ALTER TABLE vendors
DROP COLUMN vend_phone;
  • 刪除
DROP TABLE customer2;
  • 重命名
RENAME TABLE customer2 TO customer;
發佈了34 篇原創文章 · 獲贊 25 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章