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.

Slides:



Advertisements
Similar presentations
Data Definition and Integrity Constraints
Advertisements

Data Definition Language (DDL)
Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
Sanjay Goel, School of Business, University at Albany, SUNY 1 SQL- Data Definition Language ITM 692 Sanjay Goel.
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 Chapter 2: Creating and Modifying Database Tables.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
DT211 Stage 2 Databases Lab 1. Get to know SQL Server SQL server has 2 parts: –A client, running on your machine, in the lab. You access the database.
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
SQL Overview Defining a Schema CPSC 315 – Programming Studio Spring 2008 Project 1, Lecture 3 Slides adapted from those used by Jeffrey Ullman, via Jennifer.
Database Management System LICT 3011 Eyad H. Elshami.
Oracle Data Definition Language (DDL)
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
Database Design lecture 3_1 1 Database Design Lecture 3_1 Data definition in SQL.
SQL data definition using Oracle1 SQL Data Definition using Oracle.
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
CS 3630 Database Design and Implementation. Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial.
1 The Relational Model Instructor: Mohamed Eltabakh
Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio.
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.
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.
SQL: DDL John Ortiz Cs.utsa.edu.
9 Copyright © Oracle Corporation, All rights reserved. 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.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
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.
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.
9 Copyright © 2004, Oracle. All rights reserved. Using DDL Statements to Create and Manage Schema Objects.
Visual Programing SQL Overview Section 1.
SQL Jan 20,2014. DBMS Stores data as records, tables etc. Accepts data and stores that data for later use Uses query languages for searching, sorting,
Lecture5: SQL Overview, Oracle Data Type, DDL and Constraints Ref. Chapter6 Lecture4 1.
9 Copyright © Oracle Corporation, All rights reserved. Creating and Managing Tables.
1 Chapter 2: Creating and Modifying Database Objects.
Sql DDL queries CS 260 Database Systems.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
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.
©Silberschatz, Korth and Sudarshan1 Structured Query Language (SQL) Data Definition Language Domains Integrity Constraints.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
1 CS 430 Database Theory Winter 2005 Lecture 11: SQL DDL.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
IS6146 Databases for Management Information Systems Lecture 3: SQL III – The DDL Rob Gleasure robgleasure.com.
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.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Physical Model Lecture 11. Physical Data Model The last step is the physical design phase, In this phase data is – Store – Organized and – Access.
Understand Data Definition Language (DDL) Database Administration Fundamentals LESSON 1.4.
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.
Managing Tables, Data Integrity, Constraints by Adrienne Watt
ORACLE SQL Developer & SQLPLUS Statements
CS4222 Principles of Database System
Managing Objects with Data Dictionary Views
SQL OVERVIEW DEFINING A SCHEMA
SQL data definition using Oracle
Managing Objects with Data Dictionary Views
Oracle Data Definition Language (DDL)
SQL-1 Week 8-9.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Chapter 2: Creating And Modifying Database Tables
SQL (Structured Query Language)
Presentation transcript:

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 use a DDL statement, the DBMS changes metadata (“data about data”) maintained in the data dictionary n Objects that can be defined include: table, view, sequence, index

Oracle’s Data Dictionary n Data dictionary tables maintain data about the structure of databases n Users cannot directly insert, update or delete information in the data dictionary n DDL statements cause the DBMS to add/update or delete information in the data dictionary tables n Oracle’s data dictionary views such as ALL_OBJECTS, ALL_TABLES, ALL_CONSTRAINTS, ALL_COLUMNS can be queried using SELECT statements and contain information about objects you have access to

SQL’s DDL Statements n Objects are defined using the CREATE statement n Some objects can be modified using the ALTER statement n Objects are removed using the DROP statement n Oracle’s data dictionary views such as USER_OBJECTS, USER_TABLES, USER_CONSTRAINTS, USER_COLUMNS, USER_SEQUENCES, USER_INDEXES can be queried using SELECT statements and contain information about objects you have created

Schema n The DataBase Administrator (DBA) has set up your Oracle account with permission for you to create objects in a schema (schema has same name as your account) n A schema (same as DB2’s collection) is a collection of related tables, views, sequences and indexes, i.e. a database

System Datatypes n System-defined datatypes include: –CHAR(n): Fixed length character string often used for Primary and Foreign Key numeric fields and also for codes such as ProgramCode(CPA/CPD/CNS/…); eg. program_code CHAR(3) –VARCHAR2(n): Variable length character string often used for names; eg. last_name VARCHAR2(30) –NUMBER(n,r): Real numbers with n digits in total and with r digits to the right of the decimal point; eg hrly_pay_rate NUMBER(5,2) can store hourly pay rates from 0.00 to –INTEGER: Integer values up to 2*(10**9) –DATE: dates in the format DD-MON-YYYY or MM/DD/YYYY

Create a Table n You define a table and its columns using the CREATE TABLE statement using the basic format: CREATE TABLE tablename ( column1name datatype(length), column2name datatype(length),… column255name datatype(length)) eg. CREATE TABLE student (id CHAR(9), fname VARCHAR2(30), lname VARCHAR2(30), gpa decimal(2,1) )

Create a Table (ctd) n You can also create a table based on an existing table: CREATE TABLE table1name AS SELECT col1name, …, colnname FROM table2name WHERE … eg. CREATE TABLE honourstudent AS SELECT id, lname FROM student WHERE gpa = 4.0

Change Table Structure n Change structure of a table (i.e. add columns or constraints, remove columns or constraints, change properties of a column) using the ALTER TABLE statement: ALTER TABLE tablename ( ADD col1name datatype(length), DROP col2name, MODIFY col3name datatype(length), MODIFY col4name NOT NULL, … )

ALTER TABLE Examples n ALTER TABLE student ADD (address VARCHAR(40)) n ALTER TABLE student MODIFY (lname VARCHAR2(35) ) n ALTER TABLE student DROP COLUMN gpa

Remove Table from Data Dictionary n Remove table from data dictionary (and also deletes all data in table) DROP TABLE tablename n Deletes all data in table and frees storage but leaves table in data dictionary TRUNCATE TABLE tablename

Constraints n Constraints protect the integrity of data in a database n Constraints are defined on tables and columns in tables n Constraint types: PRIMARY KEY, FOREIGN KEY, NOT NULL, CHECK, UNIQUE n All constraints should be given a name, except for NOT NULLs, so error messages produced will include the name of the constraint violated

Constraints (ctd) n Information about constraints is maintained in the data dictionary and can be queried using the view USER_CONSTRAINTS n Constraints may be defined as part of a CREATE TABLE statement n Constraints may be added, dropped or disabled (but not modified) by using an ALTER TABLE statement

Constraint Example n CREATE TABLE student ( id CHAR(9) PRIMARY KEY, fname VARCHAR(30) NOT NULL, lname VARCHAR2(30) NOT NULL, sem_num SMALLINT, soc_ins CHAR(9) CONSTRAINT soc_ins_uq UNIQUE(soc_ins), prog_code CHAR(3) NOT NULL, CONSTRAINT student_prog_code_fk FOREIGN KEY (prog_code) REFERENCES program(progcode) )

Constraint Examples n ALTER TABLE student ADD CONSTRAINT student_sem_num CHECK (sem_num BETWEEN 1 AND 6) n ALTER TABLE student DROP UNIQUE (soc_ins_uq) n ALTER TABLE student DROP PRIMARY KEY n ALTER TABLE student DROP CONSTRAINT student_sem_num n ALTER TABLE student DISABLE CONSTRAINT student_prog_code_fk