Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

SQL CREATE TABLE statements

Creating tables and integrity constraints


horse
Introduction: creating tables. horse_no name sire dam location dob
45754 MATRICULE 34232 23233 Newmarket 02-OCT-12
Classification of constraints. 34232 DARBY CREEK LANCE Newmarket 19-MAR-13
Specifying constraints. 23233 TOPSIDER Galway 27-OCT-12
Constraints in action.
Conclusion.
create table horse (
horse_no varchar2(10),
Reference: name varchar2(20),
Connolly & Begg, Database Systems, sire varchar2(10),
dam varchar2(10),
Fifth edition pp 181-185 location varchar2(10),
dob date);
Fourth Edition pp 164-168.

DATES IN ORACLE Integrity Constraints


Integrity: the accuracy or correctness of data
Dates have a default form of DD-MON-YY
EG: ‘09-MAR-17’ The apostrophes are required.
UPDATE ORDER User integrity constraints – eg the value of an order has to be
SET O_DATE = ‘09-MAR-17’ greater than zero
WHERE ORDER_NUMBER = 12211;
Entity integrity constraints – eg the order date can not be null
Functions are available for converting characters to dates before loading into Referential integrity constraint
the database and for converting dates to characters on retrieval ie:
UPDATE ORDER Deletion of this
SET O_DATE = TO_DATE(‘09 03 17’,‘DD MM YY’) prevented
WHERE ORDER_NUMBER = 12211; while these are still
SELECT AMOUNT,TO_CHAR(TIMESTAMP,‘DD-MM-YY HH MI SS’) in the db
FROM TRANSACTION;

AMOUNT TIMESTAMP
9000 03-03-17 12 34 26
213 04-03-17 20 12 45
CREATE TABLE CUSTOMER
Integrity Constraints (CUST_NUM CHAR(10) CONSTRAINT PK_CUST PRIMARY KEY,
CUST_NAME CHAR(30) CONSTRAINT CHECK_N CHECK (
Integrity: the accuracy or correctness of data LENGTH (REPLACE (TRANSLATE (UPPER(CUST_NAME),
'ABCDEFGHIJKLMNOPQRSTUVWXYZ ‘,
User integrity constraints – eg the value of an order has to be 'XXXXXXXXXXXXXXXXXXXXXXXXXXX'),
'X','') )= 0),
greater than zero ADDRESS CHAR(30))
Entity integrity constraints – eg the order date can not be null
Referential integrity constraint CREATE TABLE ORDER
(ORDER_NO CHAR(10) CONSTRAINT PK_NUM PRIMARY KEY ,
Deletion of this CUST_NUM CHAR(10) CONSTRAINT CUSTREF REFERENCES
prevented CUSTOMER(CUST_NUM),
ORDER_DATE DATE,
while these are still VALUE NUMBER (9,2) CONSTRAINT NO_NL_VAL NOT NULL
in the db CONSTRAINT CHECK_VL CHECK (VALUE>0) )
CREATE TABLE ITEM
( ORDER_NO CHAR(10),
ITEM_NUM CHAR(10),
DESCRIPT CHAR(20) CONSTRAINT NO_NL_DESC NOT NULL,
PRICE NUMBER (9,2) CONSTRAINT NO_NL_PRICE NOT NULL,
CONSTRAINT ORDREF FOREIGN KEY (ORDER_NO) REFERENCES
ORDER(ORDER_NO) ON DELETE CASCADE,
CONSTRAINT PK_O_I PRIMARY KEY(ORDER_NO,ITEM_NUM))

TRIGGERS (SQL3 & ORACLE V7.0)


Question: what is the effect of statement xi ? CREATE TRIGGER ORDER_VAL_LIMITS
BEFORE INSERT OR UPDATE OF VALUE
i) insert into customer values ( 'c1', 'Fred Pink','42 James Street'); ON ORDER
FOR EACH ROW
ii) insert into customer values ( 'c1', 'Bill Fish','34 Codd Row'); DECLARE
iii) insert into customer values ( 'c2', 'Sam 5pade','5 Fifth Street'); LIMIT NUMBER;
CREDITLIMIT NUMBER;
iv) insert into order values ( 'C2314' , 'c4', '20-JUL-10',20); BEGIN
SELECT LIMIT
v) insert into order values ( 'X2316' , 'c1', '20-JUL-10',0); INTO CREDITLIMIT
vi) insert into order values ( 'C2314' , 'c1', '01-DEC-10',15); FROM CUSTOMER
WHERE CUSTOMER.CUST_NUM = :new.CUST_NUM;
vii) insert into item values ( 'C2314','I1','',99); IF (:new.VALUE > CREDITLIMIT )
viii) insert into item values ( 'C2314','I1','nut',99); THEN raise_application_error (-20601,:new.VALUE ||' is over limit');
END IF;
ix) insert into item values ( 'C2314','I1','bolt',99); END;
insert into customer values('c2','F.Harrison','3, Cheesmarket',200);
x) delete from order where order_num = 'C2314'; 1 row created
xi) select * from item; insert into order values('o5','c1','29-Jan-10',50);
1 row created
insert into order values('o6','c2','29-Jan-10',250);
Error at line 1
ORA-20601 250 is over credit limit
Write an SQL CREATE TABLE statement to create the following: Write an SQL CREATE TABLE statement to create the following:

horse horse
horse_no name sire dam location dob horse_no name sire dam location dob
45754 MATRICULE 34232 23233 Newmarket 02-OCT-12 45754 MATRICULE 34232 23233 Newmarket 02-OCT-12
34232 DARBY CREEK LANCE Newmarket 19-MAR-13 34232 DARBY CREEK LANCE Newmarket 19-MAR-13
23233 TOPSIDER Galway 27-OCT-12 23233 TOPSIDER Galway 27-OCT-12

create table horse ( create table horse (


horse_no varchar2(10), horse_no varchar2(10) constraint pkhse primary key ,
name varchar2(20), name varchar2(20),
sire varchar2(10), sire varchar2(10) constraint refsire references horse(horse_no),
dam varchar2(10), dam varchar2(10) constraint refdam references horse(horse_no),
location varchar2(10), location varchar2(10),
dob date); dob date);

You might also like