SQL四大基礎語法

SQL四大基礎與法為

  1.  select 查詢:用來查詢資料庫內的資料
  2.  install 新增 :把新資料新增進去資料庫
  3.  update 更新(修改) : 把已存在於資料庫內的資料修改為新資料
  4.  delete 刪除 :刪除已存在於資料庫內的資料

select 

可以用select 來查詢資料庫內資料
select 顯示欄位 from 表名 where 條件 order 排序方式

select的語法順序如下:

  1. from
  2. where
  3. group
  4. having
  5. select
  6. distinct
  7. union
  8. order by

範例: 

  • 全部資料:
    select * from test
  • 有條件搜尋 where
    select * from test where a>0 AND b>0
  • 排序 order by
    select * from test where  a>0 AND b>0 order by a desc,b asc
  • 群組 
    select a,b from test group by a,b

insert

用來對資料表新增一筆新資料
instert  into 表名 VALUES (資料1,資料2…..)
範例

  • 對資料表的所有欄位
    instert INTO test VALUES (‘a’,’b’,’c’)
  • 對特定欄位新增資料 
    instert INTO test (a,b,c) VALUES (‘a’,’b’,’c’)
  • 一個欄位對應一個值的方式
    instert into test a=’a’ , b=’b’ , c=’c’

UPDATE

把已存在於資料庫內的資料修改為新資料
會把符合條件的資料修改為新資料
UPDATE 表名 Set 欄名1=新資料1,  欄名2=新資料2 where 條件

範例:

  • UPDATE test SET a=’a1′ WHERE a=’a’;

DELETE

會把符合條件的資料刪除
DELETE FROM 表明 where 欄名=資料;

範例

  • DELETE  FROM test WHERE a=’a’;