SQL練習 課本的 例3 1 例3 12

2021-10-03 15:32:22 字數 2554 閱讀 9011

一、定義模式create create create ?‍♀️

create schema test authorization wang

create table tab1 (col1 smallint,

col2 int,

col3 char(20),

col4 numeric(10,3),

col5 decimal(5,2),

);或者

create table test.tab2 (col1 smallint,…);

如果不指認是誰的模式則預設為dbo的,如果省略第一行**,那麼新建的表屬於dbo模式(dbo.tab1);加上後則是test.tab1。

二、刪除模式drop drop drop?‍?

drop schema zhang cascade

刪除模式語句如下:

drop schema 《模式名》<cascade|restrict>;其中cascade(級聯)和restrict(限制)兩者必選其一。

三、定義基本表

建表的同時通常還可以定義與該錶有關的完整性約束條件。

create table student

( sno char(9)primary key,

/*列級完整性約束條件,sno是主碼 */

sname char(20)unique,

/ *sname取唯一值 */

s*** char(2),

sage smallint,

sdept char(20)

);primary key意思是主碼;unique意思是取唯一值,此時不能取一樣的名字,否則就會違反列級的約束,輸入失敗。

create table course

(cno char(4)primary key,

/* 列級完整性約束條件,cno是主碼*/

cname char(40)not null,

/* 列級完整性約束條件,cname不能取空值 */

cpno char(4),

/ * cpno的含義是先修課 */

ccredit smallint,

foreign key(cpno)referencescourse(cno)

/ *表級完整性約束條件,cpno是外碼,被參照表是course,被參照列是cno */

);說明參照表和被參照表可以是同乙個表。

create table sc

(sno char(9),

cno char(4),

grade smallint,

primary key(sno,cno),

/ *主碼由兩個屬性構成,必須作為表級完整性進行定義 */

foreign key(sno) references student(sno),

/ *表級完整性約束條件,sno是外碼,被參照表是student */

foreign key(cno) references course(cno)

/ *表級完整性約束條件,cno是外碼,被參照表是course */

);四、修改基本表alter alter alter ?‍?

alter table student add s_entrance date;

不管基本表中原來是否已有資料,新增加的列一律為空值。

alter table student alter column sage int;

不管原來是啥型別,新的只會把舊的覆蓋掉:

alter table course add unique(cname);

五、刪除基本表

drop table student cascade;

基本表定義被刪除,資料被刪除

表上建立的索引、檢視、觸發器等一般也將被刪除

先建立基於student的檢視is_student:

create view is_student

asselect sno,sname,sage

from student

where sdept=『is』;

然後如圖:

**絕對沒有複製貼上嗷!

單例設計模式的練習

單例設計模式 有關的知識點 class a object def init self print 這是 init 方法 def new cls print 這是 new 方法 return object.new cls 返回的是乙個例項了的物件 就是self new 至少要有乙個引數cls,代表要例...

高效分頁的SQL技巧 以Oracle為例

一般的分頁寫法大概就是下面的樣子 所謂的 三層巢狀 寫法 select from select a.rownum rn from select from table name a where rownum 40 where rn 21 在遇到顯示資料表中1百萬行以後的記錄時,可能會出現效能問題。有人...

自定義異常練習(以銀行的存款取款為例)

package com.zidingyi public class newexception extends exception package com.zidingyi public class bank public void setyue double yue 成員方法 存錢 public d...