1 Designing Tables for an Oracle Database System Database Course, Fall 2003.

Slides:



Advertisements
Similar presentations
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Advertisements

OUTLINE OF THE LECTURE PART I GOAL: Understand the Data Definition Statements in Fig 4.1 Step1: Columns of the Tables and Data types. Step2: Single column.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL.
1 Translation of ER-diagram into Relational Schema Prof. Sin-Min Lee Department of Computer Science.
Final Exam Revision 5 Prof. Sin-Min Lee Department of Computer Science.
1 The Oracle Database System Building a Database Database Course The Hebrew University of Jerusalem.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
1 Designing Tables for an Oracle Database System Database Course, Fall 2005.
1 Table Alteration. 2 Altering Tables Table definition can be altered after its creation Adding columns Changing columns’ definition Dropping columns.
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
1 Designing Tables for an Oracle Database System Database Course, Fall 2004.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial Password: UWPstudent Password is case sensitive.
Oracle Data Definition Language (DDL)
ER to Relational Mapping. Logical DB Design: ER to Relational Entity sets to tables. CREATE TABLE Employees (ssn CHAR (11), name CHAR (20), lot INTEGER,
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Database Design lecture 3_1 1 Database Design Lecture 3_1 Data definition in SQL.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model  Vendors: IBM, Informix, Microsoft, Oracle, Sybase  Recent.
Database. Basic Definitions Database: A collection of related data. Database Management System (DBMS): A software package/ system to facilitate the creation.
SQL Data Definition (CB Chapter 6) CPSC 356 Database Ellen Walker Hiram College (Includes figures from Database Systems by Connolly & Begg, © Addison Wesley.
SQL data definition using Oracle1 SQL Data Definition using Oracle.
1 Translation of ER-diagram into Relational Schema Prof. Sin-Min Lee Department of Computer Science.
CS 3630 Database Design and Implementation. Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial.
SQL data definition using Oracle1 SQL Data Definition using Oracle.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
10 Copyright © 2009, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
1 Copyright © 2006, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
SQL: DDL John Ortiz Cs.utsa.edu.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
Creating Tables and Inserting Records -- Not easy to edit! -- check constraints! Create table test1 ( C1 char(5) primary key, C2 Varchar2(15) not null.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
CS 3630 Database Design and Implementation. Database Schema Branch (Bno…) Staff (Sno…Bno) Owner (Ono…) PropertyForRent (Pno…Ono) Renter (Rno…) Viewing.
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
1 SQL - II Data Constraints –Applying data constraints Types of data constraints –I/O constraints The PRIMARY KEY constraints The FOREIGN KEY constraints.
Chapter 9 Constraints. Chapter Objectives  Explain the purpose of constraints in a table  Distinguish among PRIMARY KEY, FOREIGN KEY, UNIQUE, CHECK,
Oracle 11g: SQL Chapter 4 Constraints.
10 Copyright © Oracle Corporation, All rights reserved. Including Constraints.
CREATE TABLE CREATE TABLE statement is used for creating relations Each column is described with three parts: column name, data type, and optional constraints.
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
INCLUDING CONSTRAINTS lecture5. Outlines  What are Constraints ?  Constraint Guidelines  Defining Constraint  NOT NULL constraint  Unique constraint.
Visual Programing SQL Overview Section 1.
The Oracle Database System. Connecting to the Database At the command line prompt, write: sqlplus In the beginning your password.
Week 8-9 SQL-1. SQL Components: DDL, DCL, & DML SQL is a very large and powerful language, but every type of SQL statement falls within one of three main.
Including Constraints. What Are Constraints? Constraints enforce rules at the table level. You can use constraints to do the following: – Enforce rules.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
SQL. Internet technologies – Ohad © Database  A database is a collection of data  A database management system (DBMS) is software designed to assist.
1 Designing Tables for a Database System. 2 Where we were, and where we’re going The Entity-Relationship model: Used to model the world The Relational.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
2 Copyright © 2009, Oracle. All rights reserved. Managing Schema Objects.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
Logical DB Design: ER to Relational
CS 3630 Database Design and Implementation
Converting ER-Diagrams to Table Definitions
Designing Tables for a Database System
DB Review.
ORACLE SQL Developer & SQLPLUS Statements
CS4222 Principles of Database System
Designing Tables for a postgreSQL Database System
SQL data definition using Oracle
Translation of ER-diagram into Relational Schema
Oracle Data Definition Language (DDL)
SQL-1 Week 8-9.
Database Design: Relational Model
ISC321 Database Systems I Chapter 4: SQL: Data definition, Constraints, and Basic Queries and Updates Fall 2015 Dr. Abdullah Almutairi.
Turn on spool and save to file a.txt
Presentation transcript:

1 Designing Tables for an Oracle Database System Database Course, Fall 2003

2 Add to Your.cshrc Add the following to your.cshrc file: source ~db/oraenv You will be able to use Oracle after you log out and log in again (or source.cshrc ) You can run Oracle from the gx-es, pita, inferno, etc. Cannot run from xil-es If you are on xil, do rlogin or xon to one of these computers (e.g., rlogin gx-03 )

3 Connecting to the Database At the command line prompt, write: sqlplus In the beginning your password is the same as your login. You can change your password with the command: password To disconnect, type: quit Remember: Every command must end with a semicolon (;)

4 Running Commands from an.sql File Instead of typing commands into the SQLPLUS terminal, you can load commands from a file –Use the from SQLPLUS to load the file file.sql –Invoke the SQLPLUS command with the extra to load the file at connection:

5 Spooling the Output Output can be placed in a file: –spool myFile.out Spooling can be turned off with: –spool off

6 Creating Tables

7 Creating a Table The basic format of the CREATE TABLE command is: CREATE TABLE TableName( Column1 DataType1 ColConstraint, … ColumnN DataTypeN ColConstraint, TableConstraint1, … TableConstraintM );

8 An Example CREATE TABLE Employee( SSN NUMBER NOT NULL, Fname VARCHAR2(20), Lname VARCHAR2(20), Gender CHAR(1), Salary NUMBER(5) NOT NULL, Dept NUMBER );

9 An Example (cont.) Oracle is case insensitive in Column names! If you type describe Employee you get: Name Null? Type SSN NOT NULL NUMBER FNAME VARCHAR2(20) LNAME VARCHAR2(20) GENDER CHAR(1) SALARY NOT NULL NUMBER(5) DEPT NUMBER

10

11 Examples of Data Types CHAR(n)String of length n (n <= 2000) VARCHAR2(n)Variable length string of size <= n (n <= 4000) LONGVariable length string of length (<= 2GB) CLOBCharacter large object (<= 4GB) BLOBBinary large object (<= 4GB) DATEValid dates (up to seconds) TIMESTAMPValid timestamps (up to milliseconds) NUMBERUp to 40 digits NUMBER(n)Whole Number of size n NUMBER(n,m) Number of size n with m digits after decimal place OthersXML, Abstract types, etc.

12 Example What happens if we insert: –'abc' into char(5)? –'abc' into varchar(5)? –'abc' into char(2)? –'abc' into varchar(2)? – into number(3,2)? – into number(5,2)? – into number(4,1)? – into number(3)? – into number? Why not always use number and not number(n,m)? Why not always use varchar2(4000) or long? Where is the boolean datatype?

13 Constraints in Create Table Adding constraints to a table enables the database system to enforce data integrity. However, adding constraints also makes inserting data slower. Different types of constraints: * Not Null* Default Values * Unique * Primary Key * Foreign Key* Check Condition

14 Not Null Constraint CREATE TABLE Employee( SSNNUMBER NOT NULL, Fname VARCHAR2(20), LnameVARCHAR2(20), GenderCHAR(1), SalaryNUMBER(5) NOT NULL, DeptNUMBER );

15 Default Values CREATE TABLE Employee( SSNNUMBER NOT NULL, Fname VARCHAR2(20), LnameVARCHAR2(20), GenderCHAR(1) DEFAULT(‘F’), SalaryNUMBER(5) NOT NULL, DeptNUMBER );

16 Unique Constraint (Syntax 1) CREATE TABLE Employee( SSN NUMBER UNIQUE NOT NULL, Fname VARCHAR2(20), LnameVARCHAR2(20), GenderCHAR(1) DEFAULT(‘F’), SalaryNUMBER(5) NOT NULL, DeptNUMBER );

17 Unique Constraint (Syntax 2) CREATE TABLE Employee( SSNNUMBER NOT NULL, Fname VARCHAR2(20), LnameVARCHAR2(20), GenderCHAR(1) DEFAULT(‘F’), SalaryNUMBER(5) NOT NULL, DeptNUMBER, UNIQUE(SSN) );

18 Unique Constraint (Syntax 3) CREATE TABLE Employee( SSNNUMBER NOT NULL, Fname VARCHAR2(20), LnameVARCHAR2(20), GenderCHAR(1) DEFAULT(‘F’), SalaryNUMBER(5) NOT NULL, DeptNUMBER, constraint SSN_UN_CONS UNIQUE(SSN) );

19 Unique Constraint (Another Example) CREATE TABLE Employee( SSNNUMBER UNIQUE NOT NULL, Fname VARCHAR2(20), LnameVARCHAR2(20), GenderCHAR(1) DEFAULT(‘F’), SalaryNUMBER(5) NOT NULL, DeptNUMBER, UNIQUE(Fname, Lname) ); How else can this be written?

20 Primary Key Constraint CREATE TABLE Employee( SSNNUMBER PRIMARY KEY, Fname VARCHAR2(20), LnameVARCHAR2(20), GenderCHAR(1) DEFAULT(‘F’), SalaryNUMBER(5) NOT NULL, DeptNUMBER, UNIQUE(Fname, Lname) ); Primary Key implies: * NOT NULL * UNIQUE. There can only be one primary key.

21 Primary Key Constraint (Syntax 2) CREATE TABLE Employee( SSNNUMBER, Fname VARCHAR2(20), LnameVARCHAR2(20), GenderCHAR(1) DEFAULT(‘F’), SalaryNUMBER(5) NOT NULL, DeptNUMBER, UNIQUE(Fname, Lname), PRIMARY KEY(ssn) ); What is Syntax 3?

22 Another Table CREATE TABLE Department( Dept NUMBER PRIMARY KEY, Name VARCHAR2(20), ManagerId NUMBER ); Shouldn’t all department numbers in Employee appear in Department ?

23 Foreign Key Constraint CREATE TABLE Employee( SSNNUMBER PRIMARY KEY, Fname VARCHAR2(20), LnameVARCHAR2(20), GenderCHAR(1) DEFAULT(‘F’), SalaryNUMBER(5) NOT NULL, DeptNUMBER, UNIQUE(Fname, Lname), FOREIGN KEY (Dept) REFERENCES Department(Dept) ); NOTE: Dept must be unique (or primary key) in Department

24 Foreign Key Constraint (Syntax 2) CREATE TABLE Employee( SSNNUMBER PRIMARY KEY, Fname VARCHAR2(20), LnameVARCHAR2(20), GenderCHAR(1) DEFAULT(‘F’), SalaryNUMBER(5) NOT NULL, DeptNUMBER, UNIQUE(Fname, Lname), FOREIGN KEY (Dept) REFERENCES Department ); NOTE: Dept must be the name of the field in Department, too

25 Understanding Foreign Keys The constraint on the last table should be read as: “The field Dept in Employee is a foreign key that references the field Dept in Department ” Meaning: Every non-null value in the field Dept of Employee must appear in the field Dept of Department. What happens to Employee s in department 312 when Department 312 is removed from the Department table?

26 Deleting a Referenced Value If nothing additional is specified, then Oracle will not allow Department 312 to be deleted if there are Employees working in this department. If the constraint is written as FOREIGN KEY (Dept) REFERENCES Department ON DELETE CASCADE then Employees working in 312 will be deleted automatically from the Employee table, when 312 is deleted from Departments

27 Cyclic Foreign Keys CREATE TABLE Department( Dept NUMBER PRIMARY KEY, Name VARCHAR2(20), ManagerId NUMBER, FOREIGN KEY (ManagerId) REFERENCES Employee(SSN) ); Do you see a problem in defining these tables and in inserting data now? We should revise the Department table:

28 Solution to Cyclic Constraints Add one of the constraints later on (after insertion): CREATE TABLE Department( Dept NUMBER PRIMARY KEY, Name VARCHAR2(20), ManagerId NUMBER); Insert data here… ALTER TABLE Department ADD(FOREIGN KEY (ManagerId) REFERENCES Employee(SSN));

29 Check Conditions A check condition is a Boolean expression: –“And”s and “Or”s of conditions of the type X > 5… On a column: it can refer only to the column On a table: it can refer only to multiple columns in the table

30 Check Constraints CREATE TABLE Employee( SSNNUMBER PRIMARY KEY, Fname VARCHAR2(20), LnameVARCHAR2(20), GenderCHAR(1) DEFAULT(‘F’) CHECK(Gender = ‘F’ or Gender = ‘M’), SalaryNUMBER(5) NOT NULL, CHECK (Gender = ‘M’ or Salary > 10000) );

31 Deleting a Table To delete the table Employee : DROP TABLE Employee; Be careful about the order of dropping when there are foreign key constraints. Why? Can use: DROP TABLE Employee cascade constraints;

32 Translating ER-Diagrams to Table Definitions

33 Relations vs. Tables We show how to translate ER-Diagrams to table definitions Sometimes, people translate ER-Diagrams to relation definition, which is more abstract than table definitions. –e.g., Employee(SSN, Fname, Lname, Gender, Salary, Dept); –table definitions contain, in addition, constraints and datatypes

34 Translating Entities General Rule: Create a table with the name of the Entity. There is a column for each attribute The key in the diagram is the primary key of the table Actor id name address birthday

35 Translating Entities create table Actor(id varchar2(20) primary key, name varchar2(40), birthday date, address varchar2(100)); Actor id name address birthday Relation: Actor (id, name, birthday, address)

36 Translating Relationships (without constraints) General Rule: Create a table with the name of the relationship The table has columns for all of the relationship's attributes and for the keys of each entity participating in the relationship What is the primary key of the table? What foreign keys are needed? Actor id name address birthday Acted In Film title type year salary

37 Translating relationships (without constraints) What would be the relation for ActedIn? How would you define the table for ActedIn? Actor id name address birthday Acted In Film title type year salary

38 Translating Recursive Relationships (without constraints) Employee id name address Manages manager worker Relation: Actor (worker-id, manager-id) What would be the table definition?

39 Translating relationships (key constraints): Option 1 General Rule for Option 1: Same as without key constraints, except that the primary key is defined differently Director id name Directed Film title salary year

40 Translating relationships (key constraints): Option 1 create table Directed( id varchar2(20), title varchar2(40), salary integer, ) Director id name Directed Film title salary year What primary and foreign keys are missing?

41 Translating relationships (key constraints): Option 2 General Rule for Option 2: Do not create a table for the relationship Add information columns that would have been in the relationship's table to the table of the entity with the key constraint What is the disadvantage of this method? What is the advantage of this method? Director id name Directed Film title salary year

42 Translating relationships (key constraints): Option 2 create table Film( title varchar2(40), year integer, primary key (title), ) Director id name Directed Film title salary year What 3 lines are missing?

43 Translating relationships (key constraints) What are the different options for translating this diagram? A R B C

44 Translating relationships (participation constraints) General Rule: If has both participation and key constraint, use Option 2 from before. Add the not null constraint to ensure that there will always be values for the key of the other entity Director id name Directed Film title salary year

45 Translating relationships (participation constraints) create table Film( title varchar2(40), year integer, id varchar2(20), salary integer, foreign key (id) references Director, primary key (title)) Director id name Directed Film title salary year Where should we add NOT NULL?

46 Translating relationships (participation constraints) How would we translate this? Actor id name Acted In Film title salary year

47 Translating Weak Entity Sets create table award( name varchar2(40), year integer, money number(6,2), o_name varchar2(40), primary key(name, year, o_name), foreign key (o_name) references Organization(name) on delete cascade ) Award Organization Gives year name phone number money

48 Translating ISA: Option 1 create table MoviePerson(... ) create table Actor(id varchar2(20), picture bfile, primary key(id), foreign key (id) references MoviePerson)) create table Director(...) Movie Person ISA id name address picture Director Actor

49 Translating ISA: Option 2 No table for MoviePerson! create table Actor(id varchar2(20), address varchar2(100), name varchar2(20), picture blob, primary key(id)); create table Director(...) Movie Person ISA id name address picture Director Actor

50 Which Option To Choose? What would you choose if: –Actor and Director DO NOT COVER MoviePerson? –Actor OVERLAPS Director?

51 Translating Aggregation Create table for Won using: –key of ActedIn –key of Award (careful, award is a weak entity) Actor picture Film year type title Acted In salary Award Organization Gives year name phone number Won

52 Think About It Recall the ER-Diagram from last week What tables/relations would you define for the diagram?