數據庫(SQL)語法

     數據庫語法


               
   創建學生數據庫,數據表名爲student:  student(sno,sname,sex,age,class)。
   其中,各列描述如下:
                   sno:學號,字符串
                   name:姓名,字符串
                   sex:性別,字符串
                   age:年齡,整數
                  class:班級編號,整數

插入語句:

insert into <數據表名> [列名]  values<值列表>
INSERT INTO student VALUES('121531010','張三','男',18,10);
INSERT INTO student (sno,name,sex,age,class) VALUES('121530808','張豐','男',20,10);

修改語句:

update 數據表名  set 列名=更新值  [列名2=更新值] [where<更新條件>]
UPDATE student set name='章三',age=17 WHERE sno='121531010';
UPDATE student set age=17 WHERE sno='121531001';

刪除語句:

delete  from   數據表名   [where<更新條件>]
DELETE FROM student WHERE sno='121530806';

查詢語句:

select 列名 from 數據表名 [where<更新條件>] [order by <排列的列名>(ASC或DESC)]
SELECT * from student;
SELECT sno,name,age,class from student WHERE class=10 ORDER BY sno;
SELECT * from student ORDER BY age;//按照年齡升序排列
SELECT name,age from student;
SELECT name,sex from student ORDER BY sno;
SELECT Sno,Sname,Sex  form  Student WHERE ClassId = 1 ORDER BY Sno

去掉重複的行:

SELECT DISTINCT 列名 from student;
SELECT DISTINCT name FROM student;

條件查詢:

AND
select name from student where sex='男' AND class=1;
OR
select name,sex from student where class=1 OR class=2;

條件查詢--確定範圍

select name from student where age between 16 and 18;
select name from student where age not between 16 and 18;

條件查詢--確定集合

select name from student where class in(1,2);
select name from student where class not in(1,2);


like % 模糊查詢(匹配多個字符)

select name from student where name like '張%';

like _ 匹配一個字符

select name from student where name like '張__';

count 統計查詢:

select count(*) from student;
select count(*) from student where class=1 or class=2;
select avg(age) from  student;


AVG  按列計算平均值
SUM  按列計算值的總和
COUNT 按列值統計個個數
MAX 求一列中的最大值
MIN 求一列中的最小值
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章