數據庫學習(更新中)

1、SQL SELECT 語句

SELECT 語句用於從表中選取數據,結果被存儲在一個結果表中(稱爲結果集)。

語法:select 列名稱  from  表名稱(選擇某一列)

           select  *  from  表名稱(選擇所有列)

例如:select  Name  from  Student

2、SQL SELECT DISTINCT 語句

用於返回唯一不同的值。

語法:select  distinct  列名稱  from  表名稱

例如:select  distinct  ID  from  Student

3、SQL WHERE 子句

有條件的從表中選擇數據。

語法:select  列名稱

           from  表名稱

           where  列  運算符  值

運算符:

例如:(1)select  Name

                    from  Student

                    where  Age = 15

           (2)select  Name

                    from  Student

                    where  Class = 'Sanban'

文本值加單引號,數值不加。

4、SQL  AND  &  OR

AND 和 OR 運算符用於基於一個以上的條件對記錄進行過濾。

如果第一個條件和第二個條件都成立,則 AND 運算符顯示一條記錄。

如果第一個條件和第二個條件中只要有一個成立,則 OR 運算符顯示一條記錄。

例如:(1)select  *

                    from  Student

                    where  FirstName = 'Tom'  and  LastName = 'Jerry'

           (2)select  *

                    from  Student

                    where  FirstName = 'Tom'  or  LastName = 'Jerry'

5、SQL ORDER BY 子句

ORDER BY 語句用於根據指定的列對結果集進行排序,默認升序(ASC),如果想要降序,使用DESC關鍵字。

例如:(1)select  Company,  Order

                    from  Person

                    order  by  Company

           (2)select  Company,  Order

                    from  Person

                    order  by  Company  DESC,  Order  ASC

6、SQL INSERT INTO 語句

insert  into  語句用於向表格中插入新的行。

語法:insert  into  表名稱  values(值1,值2,...)

          也可以指定要插入數據的列:

          insert  into  表名稱(列1,列2,...)values(值1,值2,...)

例如:insert  into  Student  values('Tom',12,'Sanban')

           insert  into  Student(Name,Age)  values('Tom',12)

7、SQL UPDATE 語句

update語句用於修改表中的數據。

語法:update  表名稱

           set  列名稱  =  新值

           where  列名稱  =  某值

例如:(1)update  Student

                    set  Name = 'Jack'

                    where  Age = 12

           (2)update  Student

                    set  Name = 'Jack',  Age = 12

                    where  Class = 'Sanban'

8、SQL DELETE 語句

delete 語句用於刪除表中的行。

語法:delete  from  表名稱

           where  列名稱 = 值

例如:delete  from  Student

           where  Name  =  'Wilso(Wilson那一行被刪除)

刪除所有行:

可以在不刪除表的情況下刪除所有的行。這意味着表的結構、屬性和索引都是完整的:

delete  from  表名稱

或者:

delete  *  from  表名稱

9、SQL TOP 子句

top 子句用於規定要返回的記錄的數目。

註釋:並非所有的數據庫系統都支持 TOP 子句。

SQL Server語法:

select  top  number | percent  列名稱

from  表名稱

MySQL語法:

select  列名稱

from  表名稱

limit  number

例1:從"Student"表中選取前2行

         select  top  2  *  from  Student

例2:從"Student"表中選取前50%

         select  top  50  percent  *  from  Student

10、SQL 通配符 和 LIKE 操作符

like操作符用於在 where子句中搜索列中的指定模式。

SQL 通配符必須與 like 運算符一起使用。

(1)使用%通配符

例1:從"Student"表中選取居住在以"Ne"開頭的城市的人。

         select  *

         from  Student

         where  City  like  'Ne%'

例2:從"Student"表中選取居住在包含"Ne"的城市的人。

         select  *

         from  Student

         where  City  like  '%Ne%'

例3:從"Student"表中選取居住在以"Ne"結尾的城市的人。

         select  *

         from  Student

         where  City  like  '%Ne'

(2)使用_通配符

例子:從"Student"表中選取名字的第一個字符之後是"om"的人。

         select  *

         from  Student

         where  Name  like  '_om'

(3)使用[charlist]通配符

例1:從"Student"表中選取居住在以"A"或''L"或"N"開頭的城市的人。

         select  *

         from  Student

         where  City  like  '[ALN]%'

例2:從"Student"表中選取居住在不以"A"或''L"或"N"開頭的城市的人。

         select  *

         from  Student

         where  City  like  '[!ALN]%'

11、SQL IN 操作符

in 操作符允許我們在 where子句中規定多個值。

語法:select  列名稱

           from  表名稱

           where  列名稱  in(value1, value2,...)

例子:從"Student"表中選取年齡爲18和20的人。

           select  *

           from  Student

           where  Age  in  (18,20)

12、SQL BETWEEN 操作符

between 操作符在 where子句中使用,作用是選取介於兩個值之間的數據範圍。

語法:①select  列名稱

              from  表名稱

              where  列名稱  between   value1  and   value2(value1和value2會不會包括,要看具體的數據庫系統)

           ②select  列名稱

               from  表名稱

               where  列名稱  not  between  value1  and  value2

例如:(1)select  *

                    from Student

                    where  Age  between  15  and  20

           (2)select  *

                    from Student

                    where  Age  not  between  15  and  20

13、SQL Alias(別名)

通過使用SQL,可以爲列名稱和表名稱指定別名。

表的 SQL Alias 語法:

select  列名稱

from  表名稱  as  別名

列的 SQL Alias 語法:

select  列名稱  as  別名

from  表名稱

表別名例子:

假設我們有兩個表,分別是"Person"和"Product",分別指定他們的別名爲"pe"和"pr",現在需要列出"John Adams"的所有信息。

(1)使用別名:

select  pr.ID,pe.LastName,pe.FirstName

from  Person  as  pe,Product  as  pr

where  pe.LastName = 'Adams'  and  pe.FirstName = 'John'

(2)不使用別名:

select  Product.ID,Person.LastName,Person.FirstName

from  Person,Product

where  Person.LastName = 'Adams'  and  Person.FirstName = 'John'

列別名例子:

select  LastName  as  Family,FirstName  as  Name

from  Person

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