常見的SQL命令(2)

    

    創建學生情況表:student

        create table student(st_class CHAR(8),st_no CHAR(10) NOT NULL,st_name CHAR(8) NOT NULL,st_sex CHAR(2),st_age SMALLINT,PRIMARY KEY(st_no))


    創建課程設置表:subject

        create table subject(su_no CHAR(4) NOT NULL,su_subject CHAR(20) NOT NULL,su_credit INTEGER,su_period INTEGER,su_preno CHAR(4),PRIMARY KEY(su_no))


    創建學生選課表:score

        create table score (st_no CHAR(10),st_nom CHAR(4),st_score INTEGERR NULL,FOREIGN KEY(st_no) REFERENCE student)


    查看錶結構:DESC student

    刪除表:    DROP TABLE student,score

    添加字段:  alter table student add (st_born date not null)

    刪除字段:  alter table student drop (st_age)

    修改字段類型和長度:

                alter table student alter column name varchar(23) null

 視圖 

   從student表中導出只包括女學生情況的視圖:

        create view womanview as select st_class,st_no,st_name,st_age from student where 

        st_sex="女"

    

    從student和score表中創建分數在60分以上的女學生視圖:

        create view woman_score as select student.st_class student.st_no,student.st_name,

        student.st_age,score.sc_score from student score where student.st_sex="女" and 

        score.sc_score >= 60 and student.st_no=score.st_no


    查看 woman_score 視圖

        select * from woman_score


 索引

  對基本表student 中 st_no 和 st_age 創建索引,分別爲升序和降序排列

       create unique index stindex on student (st_no ASC,st_age DESC)

    刪除索引:

        drop index stindex


 SELECT查詢

  

    JOIN ON 語句   聯接

    WHERE 語句     指定查詢條件

    ORDER BY 語句  指定排序字段和排序方式

    GROUP BY 和 HAVING 語句  分組

    DISTINCT 語句和 UNIQUE 語句 指定是否要重複記錄

    TOP語句        列在最前

    

    WHERE子句中的操作符和運算函數

        算術比較運算符: <  <=  > >= = <>

        邏輯運算符:    AND,OR,NOT

        集合運算符:    UNION(並),INTERSECT(交),EXCEPT(差)

        集合成員資格運算符:    IN,NOT IN

        謂詞:          EXISTS(存在) , ALL ,SOME, UNIQUE

        聚合函數:      AVG,MIN,MAX,SUM,COUNT(計數)

    

    條件查詢:

         select unique student.st_class,student.st_no,student.st_name,student.sex,

         student.st.age,score.su_no,score.score 

    排序查詢:

        查不及格的課程,並將課程號按從大到小排列

        select unique su_no from score where score<60 order by su_no DESC

    嵌套查詢:

        查詢課程編號爲C03,且課程成績在80分以上的學生的學號和姓名

        select st_no,st_name from student where st_no in (select st_no from score where         su_no = 'c03' and score>80)

    計算查詢:

        查詢男學生總人數和平均年齡:

        select count(*),AVG(st_age) from student where st_sex='男'

        統計選修課程人數:

        select count(DISTINCT st_no) from score

    

  數據更新

     INSERT語句:insert into score(st_no,su_no,score) value ('10002','c02',95)

        DELETE語句:delete from student where st_no = '10002'

        UPDATE語句:update subject set su_subject = '英語' where su_no = 'c02'

 

   數據控制

     安全性控制、完整性控制、事務控制和併發控制

        授權語句:grant connect to sse identified by password

                  grant resource to sse

                  grant dba to sse

        權力回收:

                  revoke connect from sse

                  revoke resource from sse

        將表student 查詢權授予所有用戶:

                grant select on student to public

        將表 subject 的插入及修改權力授予用戶SSE並使得他具有將這種權力授予其他人的權力

                grant insert,update(su_subject) on subject to sse with grant option

        

    

        

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