美少女Java實訓筆記03

 








              sql基礎      

---------------------------------------------------------

【1】、說明:創建新表

create table tabname(col1 type1 [not null] [primary key],col2 type2 [not null],..)

【2】、根據已有的表創建新表: 

Acreate table tab_new like tab_old (使用舊錶創建新表)

Bcreate table tab_new as select col1,col2… from tab_old definition only

【3】、說明:刪除

DELETE FROM EMPLOYEES

 

   WHERE BRANCH_OFFICE = 'Los Angeles';

【4】、說明:插入一個列
INSERT  INTO 表名 VALUES (字段對應值1,字段對應值2,....);

列增加後將不能刪除。DB2中列加上後數據類型也不能改變,唯一能改變的是增加varchar類型的長度。
[5]、說明:添加主鍵: 

              CREATE TABLE tb
              (
              id INT IDENTITY(1,1) PRIMARY KEY,
              name VARCHAR(20)
              )
 
[6]、說明:創建索引
create [unique] index idxname on tabname(col….) 
刪除索引drop index idxname
注:索引是不可更改的,想更改必須刪除重新建。

7、更新語句
語法:
update  表名 set 字段名=更新的值 where 條件;
例如:我們將上面user表中第一條記錄中的密碼改成222222,語句如下:
update user set passWord='222222'where id=1;
8.1:給指定條件查詢:
語法:
select *from 表名 where 條件;
例如我們要查詢user表中id<4的字段值,語句如下:
select *from user where id<4;
10、distinct:

              select distinct *from user

              查詢user表中的所有字段的值都一樣只顯示一條記錄

              select distinct password from user


              查詢user表中的password字段值相同的只顯示一條記錄

              select distinct username,passwordfrom user;

              查詢user表中username和password字段值都相同的只顯示一條記錄。

10、條件

       10.1: is null 和is not null

       null是一種狀態,不是裏面的字符串爲空。

通配符:

10.2,% :表示任意0個或多個字符。可匹配任意類型和長度的字符,有些情況下若是中文,請使用兩個百分號(%%)示。

比如 SELECT * FROM [user] WHERE u_name LIKE '%三%'

10.7:排序

ORDER BY

asc desc 當對多個字段排序時,只作用前面的一個字段。

order by :排序字段可以查詢字段的別名,先查後排序,where條件中不能用查詢字段的別名。

SELECT *FROM emp ORDER BY deptno ASC,sal DESC;

對emp 表中兩個字段排序,先對deptno的值進行升序排列,在先對deptno字段的值排序好的基礎上再對sal字段的值進行降序排列

11、模糊查詢:

關鍵字LIKE

例如查詢user表中userName字段值最後一個字符是‘東’的,語句如下:

select *from user from userName LIKE ‘%東’;

例如查詢user表中userName字段值中包含'東'的,語句如下:

select *from user from userName LIKE ‘%東%’

;注:"%"表示零個或者多個字符.我這裏只介紹了模糊查詢中的一種通配符的使用,具體的看上面的通配符使用。



[12]、說明:幾個簡單的基本的sql語句
選擇:select * from table1 where 範圍
插入:insert into table1(field1,field2) values(value1,value2)
刪除:delete from table1 where 範圍
更新update table1 set field1=value1 where 範圍
查找select * from table1 where field1 like ’%value1%’ ---like的語法很精妙,查資料!
排序select * from table1 order by field1,field2 [desc]
總數select count as totalcount from table1
求和select sum(field1) as sumvalue from table1
平均select avg(field1) as avgvalue from table1
最大select max(field1) as maxvalue from table1
最小select min(field1) as minvalue from table1




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章