SQL語言複習

2021-08-20 22:24:31 字數 2104 閱讀 8737

以下僅為個人複習參考

ddlsql> create table student(

2  sno char(10) not null primary key,

3  sname varchar(20) not null,

4  sage int,

5  *** char(2) constraint def_*** default 'm',

6  sdept varchar(20)

7  );--其中包含主鍵約束和預設約束

sql> create table course(

2  cno char(10) primary key,

3  cname varchar(20),

4  credit int,

5  snumber int

6  );

sql> create table sc(

2  sno char(10) not null,

3  cno char(20) not null,

4  grade int,

5  primary key(sno,cno)

6  );

sql> alter table sc add constraint ref_stu_sno foreign key(sno) references student(sno);

新增約束就相當於修改表結構。檢查約束用觸發器會更簡單一點

create table t as select * from student--複製表

create table t1as select * from student where 1=2--只複製表結構

--以下r表示關係,t表示表,p表示條件

drop table r/t;--刪除表,這種刪除是不能恢復的

alter table t add a d--增加表中屬性

alter table t modify a d--修改表中某列屬性

alter table t drop column a --刪除某列

dmlinsert into t values()

insert into student(sno,sname,***) values('','','');

delete from t/r where p--只刪除關係或表中對應條件的值,不修改表結構

update t set a=new where p

select 語句就不用說了

order by 字句是指定結果集中元組的排列次序,就是按照order by 後的列排序,在巢狀查詢中子查詢中不使用order by字句,order by只能對最終查詢結果進行排序。oracle中的查詢順序是先查詢後排序,所以對rownum的使用會有一些影響,rownum是oracle**中記錄邏輯順序的列,在條件中只能小於某數,不能大於某數,否則將找不到任何記錄,所以在某些需要將rownum大於某數的查詢中,可以先將rownum查詢出來並給與乙個名字,在條件中就可以根據這個名字來找出大於某值的

rownum是**中的邏輯標識,也就是**的記錄順序變了,rownum也要變,rowid是標識物理位置的,所以針對每乙個記錄,rowid不變

--查詢成績的前十到前十五的學生學號

select * from (select * from sc order by grade) where rownum<=15 and rownum>=10;--沒有用

select * from (select rownum rn,a.* from (select * from sc order by grade desc) a) where rn<=15 and rn>=10;

create view v as 《查詢語句》 [with check option] with check option選項表示對檢視更新時必須滿足查詢語句中的條件

檢視名可以出現在任何關係名可以出現的地方

comment語句可以為表,列等新增注釋資訊 

comment on table student is '這是學生表' comment on column student.sno is '這是學號'

可以通過user_tab_comments系統檢視來檢視**的注釋資訊,user_col_comments或describe來檢視表中列的注釋

SQL複習總結

1.索引,觸發器,事務,儲存過程各表示什麼意思?它們有什麼不同?2.distinct用法 語法 select distinct 列名稱 from 表名稱 3.模糊查詢 4.關於笛卡爾積 5.exists用法 6.sqlserver清除快取的實現方法 7.mysql資料庫自動備份四種方法 8.sql ...

複習 sql約束

對錶中的資料進行限定,保證資料的正確性 有效性和完整性。注意 1.含義 非空且唯一 2.一張表只能有乙個字段為主鍵 3.主鍵就是表中記錄的唯一標識 使用create table stu id int primary key,給id新增主鍵約束 name varchar 20 alter table ...

C語言複習(專公升本複習)

示例 inculdevoid mian 上面是乙個簡單的c語言程式下面就來介紹其中的各個關鍵點 c語言程式是由乙個或者多個函式構成,乙個c語言有若干條c語言語句構成 2.注 乙個c語言程式或是乙個資料夾中只能有乙個主函式 inculdevoid mian 1.inculde 標頭檔案 inculde...