SQL data definition using Oracle1 SQL Data Definition using Oracle.

Slides:



Advertisements
Similar presentations
9 Creating and Managing Tables. Objectives After completing this lesson, you should be able to do the following: Describe the main database objects Create.
Advertisements

Data Definition Language (DDL)
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
SQL’s Data Definition Language (DDL) n DDL statements define, modify and remove objects from data dictionary tables maintained by the DBMS n Whenever you.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
Introduction to Structured Query Language (SQL)
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
30-Jun-15 SQL A Brief Introduction. SQL SQL is Structured Query Language Some people pronounce SQL as “sequel” Other people insist that only “ess-cue-ell”
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
Database Management System LICT 3011 Eyad H. Elshami.
Oracle Data Definition Language (DDL)
Copyright © Curt Hill SQL The Data Definition Language.
Database Design lecture 3_1 1 Database Design Lecture 3_1 Data definition in SQL.
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
1 The Relational Model Instructor: Mohamed Eltabakh
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.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
1 Creating and Modifying Database Objects. 2 An Oracle database consists of multiple user accounts Each user account owns database objects Tables Views.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
10 Creating and Managing Tables Objectives At the end of this lesson, you will be able to: Describe the main database objects Create tables Describe.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
10-1 Copyright  Oracle Corporation, All rights reserved. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns.
SQL: DDL John Ortiz Cs.utsa.edu.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
Copyright  Oracle Corporation, All rights reserved. 10 Creating and Managing Tables.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
Tables and Constraints Oracle PL/SQL. Datatypes The SQL Data Definition Language Commands (or DDL) enable us to create, modify and remove database data.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
9 Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Schema Objects.
Visual Programing SQL Overview Section 1.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
SQL CREATING AND MANAGING TABLES lecture4 1. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns ViewLogically.
IS 380 Introduction to SQL This lectures covers material from: database textbook chapter 3 Oracle chapter: 3,14,17.
1 Chapter 2: Creating and Modifying Database Objects.
Sql DDL queries CS 260 Database Systems.
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.
1 CS 430 Database Theory Winter 2005 Lecture 10: Introduction to SQL.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Creating and Managing Tables 14. ObjectivesObjectives After completing this lesson, you should be able to do the following: After completing this lesson,
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Basic SQL*Plus edit and execute commands SQL*Plus buffer and built-in editor holds the last SQL statement Statements are created in free-flow style and.
Physical Model Lecture 11. Physical Data Model The last step is the physical design phase, In this phase data is – Store – Organized and – Access.
CDT/1 Creating data tables and Referential Integrity Objective –To learn about the data constraints supported by SQL2 –To be able to relate tables together.
SQL Statements SELECT INSERTUPDATEDELETECREATEALTERDROPRENAMETRUNCATECOMMITROLLBACKSAVEPOINTGRANTREVOKE Data Retrieval Language (DRL) Data Retrieval Language.
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.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Creating and Managing Tables. Database Objects ObjectDescription TableBasic unit of storage; composed of rows and columns ViewLogically represents subsets.
Fundamentals of DBMS Notes-1.
SQL Creating and Managing Tables
SQL Creating and Managing Tables
SQL Creating and Managing Tables
SQL data definition using Oracle
Oracle Data Definition Language (DDL)
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Chapter 2: Creating And Modifying Database Tables
SQL (Structured Query Language)
Presentation transcript:

SQL data definition using Oracle1 SQL Data Definition using Oracle

SQL data definition using Oracle2 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 Oracle3 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 Oracle4 Data types Slightly different from standard SQL Strings Numbers Dates Other –no Boolean data type Documentation – b28318/datatype.htm

SQL data definition using Oracle5 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 Oracle6 Data types, numbers Numbers –number (digits, decimals) fixed number of digits and decimals, like price, salary, etc. –number (digits)integer –numberfloating point (high precision)

SQL data definition using Oracle7 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 when we look at "select …" –advice don't make an attribute called "age", as it changes frequently. make an attribute called birthday.

SQL data definition using Oracle8 Data types, other CLOBcharacter large object –texts up to 4 gigabytes BLOBbinary large object –up to 4 gigabytes, pictures, sounds, etc. NCLOB –like CLOB, but using 2 bytes pr. character BFILEbinary file –reference to a binary file outside the DBMS

SQL data definition using Oracle9 Constraints, types Types of constraints –integrity constraints primary key foreign key –value constraints not null unique

SQL data definition using Oracle10 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 Oracle11 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 Oracle12 Constraints, not null The attribute must no 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 Oracle13 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 Oracle14 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 Oracle15 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 Oracle16 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 Oracle17 Default value + domains No 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 Oracle18 Creating a table Tables are usually created by a DBA (Database administrator) Tables are usually not created from an application. –SQLite (used in Android + iOS) is an exception

SQL data definition using Oracle19 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 Person

SQL data definition using Oracle20 Alter table Changing 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 Oracle21 Chicken and egg problem, creation 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 Oracle22 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 Oracle23 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 Oracle24 Drop tables truncate table tableName –remove all data from the table, but the [not empty] table still exists. –use carefully!! drop table –deletes the table including data –use carefully!! –Referential integrity You are not allowed to drop a table if another table refer to it rename oldTableName to newTableName