oracle 每日一題 資料庫約束

2021-07-26 23:29:08 字數 1998 閱讀 1636

以往舊題索引:

... eid&typeid=1808

原始出處:

執行環境:sqlplus, serveroutput已開啟

你已開始處理新應用程式。它在這些表中儲存訂單明細:

create table plch_customers (

customer_id   int not null 

primary key,

customer_name varchar2(255) not null 

unique

);create table plch_orders (

order_id       int not null 

primary key,

order_datetime date not null,

customer_id    int not null

references plch_customers ( customer_id )

);create table plch_order_items (

order_id    int not null

references plch_orders ( order_id ),

item_number int not null

check ( item_number between 1 and 5 ),

product_id  int not null,

unit_cost   number(10, 2) not null,

quantity    int not null,

primary key ( order_id, item_number ),

check ( ( unit_cost * quantity ) > 0 )

);給定這個模式,下列哪個說法是正確的?

(a) 

所有的客戶(plch_customers)都必須有不同的名字。

(b) 

每個訂單(plch_orders)有且僅有一位客戶。

(c) 

在給定的日期和時間,一名客戶僅能下乙個訂單。

(d) 

在乙個訂單內,最多只能有五種不同產品。

(e) 

在訂單中,單價(unit_cost) 或者 數量(quantity) 都不可能為負數

number ( precision, scale)
scale表示數字小數點右邊的位數,scale預設設定為0.  如果把scale設成負數,oracle將把該數字取捨到小數點左邊的指定位數。
check ( item_number between 1 and 5 ),
check ( ( unit_cost * quantity ) > 0 )
references plch_customers ( customer_id )
a: customer_name上有乙個唯一鍵。所以這會禁止你在兩行資料儲存相同的客戶名字。

b: 從plch_orders到plch_customers有乙個非空的外來鍵。所以你必須儲存乙個在plch_customers上存在的customer_id。在plch_orders上只有乙個客戶列,所以你不能在乙個訂單上有多個客戶。

c: 不對!在( customer_id, order_datetime )上並沒有乙個唯一鍵或者主鍵。所以你可以在兩行上為這些列儲存相同的值。

d: 是的。plch_order_items.item_number是乙個整數,有乙個校驗約束來確保這個數值在1和5之間。所以你在這個列能夠儲存的值只有1, 2, 3, 4 或者 5。 和order_id一起,這個列構成了表的主鍵。所以給定乙個order_id, 你只能夠儲存一次這些值。因此對乙個訂單來說,物品的數量最多為五。每個訂單物品對應一種產品。所以你在乙個訂單上最多只能有五種產品!

e:不對!單價(unit_cost) 和數量(quantity)的乘積必須大於零。但是如果你把兩個負數相乘,結果是正數。所以你可以儲存乙個訂單物品,其單價和數量都為負數!

資料庫每日一題(1)

書號 varchar 50 銷售總額 int as begin insert into tb book profit select 書號,sum 單價 銷售數量 as 銷售總額 from 銷售表 x join 圖書表 t on x.書號 t.書號 where x.銷售時間 year group by...

oracle 每日一題 LONG資料型別

原始出處 執行環境 sqlplus,serveroutput已開啟 我執行了下列語句 create table plch data id integer,l long begin insert into plch data values 1,abc commit end 哪些選項在執行之後會導致 f...

每日一題 1

題目詳情 peter喜歡玩數字遊戲,但數獨這樣的遊戲對他來說太簡單了,於是他準備玩乙個難的遊戲。遊戲規則是在乙個n n的 裡填數,規則 對於每個輸入的n,從左上角開始,總是以對角線為起點,先橫著填,再豎著填。這裡給了一些樣例,請在樣例中找到規律並把這個n n的 列印出來吧。輸入描述 多組測試資料 資...