Download presentation
Presentation is loading. Please wait.
1
SQL data definition using Oracle
2
SQL data definition using Oracle
DDL SQL = DDL + DML Oracle has its own dialect of SQL DDL (Data definition language) Part of SQL Some DDL statements create table … alter table … drop table … SQL data definition using Oracle
3
SQL data definition using Oracle
Naming conventions Naming conventions for table names and attributes names Illegal Spaces “ “ Hyphens ‘-’ Legal letters [a-z A-Z, not æøå] + digits _, #, $ first character must be a letter no reserved words in SQL [no attribute called 'by‘ or ‘table’] SQL data definition using Oracle
4
SQL data definition using Oracle
Data types Slightly different from standard SQL Strings Numbers Dates Other no Boolean data type SQL data definition using Oracle
5
SQL data definition using Oracle
Data types, strings varchar2 (max_length) varying length, like names, etc. Varchar2(10) Max length 10 bytes Varchar2(10 char) Max length 10 chars Normally 1 char ~ 1 byte, but it depends on the character table (like UNICODE) char (fixed_length) fixed length, like cpr, phone, etc Length in bytes or chars, like varchar2 SQL data definition using Oracle
6
SQL data definition using Oracle
Data types, numbers Numbers number (digits, decimals) fixed number of digits and decimals, like price, salary, etc. (with number(4,2) - the max number is ) number (digits) integer number floating point (high precision) SQL data definition using Oracle
7
SQL data definition using Oracle
Data types, dates date store date and time! no special time data type Oracle SQL has special functions to do arithmetic on dates more on that later. advice don't make an attribute called "age", as it changes frequently. make an attribute called birthday. SQL data definition using Oracle
8
SQL data definition using Oracle
Data types, other CLOB character large object texts up to 4 gigabytes BLOB binary large object up to 4 gigabytes, pictures, sounds, etc. NCLOB like CLOB, but using 2 bytes pr. character BFILE binary file reference to a binary file outside the DBMS SQL data definition using Oracle
9
SQL data definition using Oracle
Constraints, types Types of constraints integrity constraints primary key foreign key value constraints not null unique SQL data definition using Oracle
10
SQL data definition using Oracle
Constraints, naming A constraints must have a name to be able to alter the constraints delete the constraint naming convention tableName_attributeName_constraintType student_cpr_pk If you don’t name your constraints Oracle will name them. Find the names select * from user_constraints where table_name = 'PERSON'; SQL data definition using Oracle
11
Constraints, defining + primary key
2 ways to define a constraint column level, applied to one attribute create table student ( cpr char (9) constraint student_cpr_pk primary key, … ) table level, can be applied to more attributes create table student_course ( cpr char(9) not null, course_number number(3) not null, constraint student_cpr_pk primary key (cpr, course_number) SQL data definition using Oracle
12
Constraints, foreign key
Attributes in one table refers to attributes in [another] table. DBMS checks if the referred attributes exist. referential integrity Example create table student_course ( cpr char(9) not null, course_number number(3) not null constraint student_course_number_fk references course (course_number), constraint student_cpr_pk primary key (cpr, course_number), constraint student_cpr_fk foreign key (cpr) references student (cpr) ); SQL data definition using Oracle
13
Referential triggered actions
foreign key … on delete cascade if the referred row is deleted the referring row should be deleted as well create table employee ( cpr char(9) primary key, … boss_cpr char(9), foreign key (boss_cpr) references employee (cpr) on delete cascade) ) If we delete the top boss the whole organization vanishes!! think carefully before writing … on delete cascade useful in weak entity sets no … on update cascade in Oracle!! not necessary if we use surrogate keys (they never change) SQL data definition using Oracle
14
SQL data definition using Oracle
Constraints, not null The attribute must not be NULL. syntaxes name varchar2(30) not null no name constraint name varchar2(30) constraint student_name_nn not null can not be specified at table level [only at attribute level] primary key attributes must be NOT NULL NULL means does not exist is not known at the moment Don't make your own NULL's using 0 or ‘ ‘ Don’t write NOT NULL to often. Think before you write! SQL data definition using Oracle
15
SQL data definition using Oracle
Constraints, unique The value of an attribute must be unique used when a table has more candidate keys one of the candidates is appointed primary key the rest of the candidates are [only] unique syntaxes attribute level departmentName varchar(12) unique table level constraint student_name_uk unique (firstname, lastname) unlikely constraint!! SQL data definition using Oracle
16
SQL data definition using Oracle
Constraint, check Checking business rules General constraint that every row must satisfy attribute level salary number(5) check ( salary >= 0 ) gender char(1) check (gender in ('M', 'F')) table level check (gender ='M' or salary < '1000') SQL data definition using Oracle
17
Default value + domains
Not a constraint, but has similar syntax. Used when no value is supplied country char(2) default 'DK' salary number(5) default 0 Oracle has no domain concept No create domain … But there is a create type … SQL data definition using Oracle
18
SQL data definition using Oracle
Creating a table General syntax in Oracle CREATE TABLE [schema.] tablename (column1 datatype [CONSTRAINT constraint_name] constraint_type…, column2 datatype [CONSTRAINT constraint_name] constraint_type, [CONSTRAINT constraint_name] constraint_type (column, …), …); SQL data definition using Oracle
19
Creating a table (CONT’D)
Examples CREATE TABLE location (RoomId NUMBER(2), Building VARCHAR2(7) CONSTRAINT location_building_nn NOT NULL, RoomNo CHAR(3) CONSTRAINT location_roomno_nn NOT NULL, Capacity NUMBER(2) CONSTRAINT location_capacity_ck CHECK (Capacity>0), RoomType CHAR; CONSTRAINT location_roomid_pk PRIMARY KEY(RoomId), CONSTRAINT location_roomno_uk UNIQUE(RoomNo)); SQL data definition using Oracle
20
Displaying table information
Forgot the name of your tables? Select table_name from user_tables User_tables has information in all your tables Forgot the name + type of attributes? Describe COURSE SQL data definition using Oracle
21
SQL data definition using Oracle
Alter table Altering an existing table add / delete attributes add / delete constraints most alternations are restricted [hard to do] so design your tables carefully before creating them!! syntax alter table tableName theAlternation alter table student add mobilPhone char(8) SQL data definition using Oracle
22
Chicken and egg problem
From Problem If table A refers to table B and vice versa which table should be created first? Solution create table A without foreign key to B create table B with foreign key to A alter table A adding a foreign key to B SQL data definition using Oracle
23
Chicken and egg, example
create table chicken ( cID number(3) primary key, eID number(3) ) create table egg ( eID number (3) primary key, cID number (3), foreign key (cID) refers to chicken (cID) initially deferred deferrable alter table chicken add constraint chicken_eID_fk foreign key (eID) refers to egg (eID) initially deferred deferrable SQL data definition using Oracle
24
Chicken and egg, insertion
How to insert rows into the tables? Problem an egg row refers to a chicken row, and vice versa Solution (Oracle) create the foreign keys with "initially deferred deferrable" defers constraint checking until transaction commit insert into chicken (1,2) insert into egg (2,1) commit SQL data definition using Oracle
25
SQL data definition using Oracle
Drop tables, etc truncate table tableName remove all data from the table, but the table still exists. use carefully!! drop table deletes the table including data Referential integrity You are not allowed to drop a table if another table refer to it solution: ALTER TABLE egg DROP CONSTRAINT chicken_eID_fk rename oldTableName to newTableName SQL data definition using Oracle
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.