Lecture7: SQL Overview, Oracle Data Type, DDL and Constraints Part #2

Similar documents
Lecture7: SQL Overview, Oracle Data Type, DDL and Constraints Part #2

Lecture11: Transaction Control Language Data Control Language

Lecture3: Data Modeling Using the Entity-Relationship Model.

Lecture4: Guidelines for good relational design Mapping ERD to Relation. Ref. Chapter3

CHAPTER4 CONSTRAINTS

A <column constraint> is a constraint that applies to a single column.

3ISY402 DATABASE SYSTEMS

Data Definition Language (DDL)

SQL: DDL. John Ortiz Cs.utsa.edu

다양한예제로쉽게배우는 오라클 SQL 과 PL/SQL 서진수저

Database Foundations. 6-3 Data Definition Language (DDL) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Database Programming - Section 10. Instructor Guide

Lecture10: Data Manipulation in SQL, Advanced SQL queries

MySQL Introduction. By Prof. B.A.Khivsara

Chapter 1 SQL and Data

Using DDL Statements to Create and Manage Tables. Copyright 2004, Oracle. All rights reserved.

Lecture5 Functional Dependencies and Normalization for Relational Databases

Using DDL Statements to Create and Manage Tables. Copyright 2004, Oracle. All rights reserved.

DATA CONSTRAINT. Prepared By: Dr. Vipul Vekariya

Using DDL Statements to Create and Manage Tables. Copyright 2006, Oracle. All rights reserved.

Lecture 6 Structured Query Language (SQL)

Chapter 17: Table & Integrity Contraints. Informatics Practices Class XII. By- Rajesh Kumar Mishra. KV No.1, AFS, Suratgarh

Data Modelling and Databases. Exercise Session 7: Integrity Constraints

q Ø v v v v v v v v IBM - 2

CS6312 DATABASE MANAGEMENT SYSTEMS LABORATORY L T P C

Lecture 5 Data Definition Language (DDL)

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

Lecture2: Database Environment

Data about data is database Select correct option: True False Partially True None of the Above

To understand the concept of candidate and primary keys and their application in table creation.

Oracle Create Table Foreign Key On Delete No

Unit 1 - Chapter 4,5

Difficult I.Q on Databases, asked to SCTPL level 2 students 2015

Lab # 4. Data Definition Language (DDL)

IT360: Applied Database Systems. SQL: Structured Query Language DDL and DML (w/o SELECT) (Chapter 7 in Kroenke) SQL: Data Definition Language

EXAMINATIONS 2009 END-YEAR. COMP302 / SWEN304 Database Systems / Database System Engineering. Appendix

DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E)

ORACLE VIEWS ORACLE VIEWS. Techgoeasy.com

Downloaded from

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

DATABASE DEVELOPMENT (H4)

COSC344 Database Theory and Applications. Lecture 5 SQL - Data Definition Language. COSC344 Lecture 5 1

The Structured Query Language Get Started

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) WINTER 15 EXAMINATION Model Answer

Oracle User Administration

Lecture 08. Spring 2018 Borough of Manhattan Community College

ORACLE 12C NEW FEATURE. A Resource Guide NOV 1, 2016 TECHGOEASY.COM

Lab # 2. Data Definition Language (DDL) Eng. Alaa O Shama

3.2.4 Enforcing constraints

HOW TO CREATE AND MAINTAIN DATABASES AND TABLES. By S. Sabraz Nawaz Senior Lecturer in MIT FMC, SEUSL

Microsoft MOS- Using Microsoft Office Access Download Full Version :

Indexes (continued) Customer table with record numbers. Source: Concepts of Database Management

Chapter 11 How to create databases, tables, and indexes

Step 0 How to begin and what you need to do before you start?

Babu Madhav Institute of Information Technology 2015

CSC Web Programming. Introduction to SQL

LECTURE 6: GUIDELINES FOR GOOD RELATIONAL DESIGN MAPPING ERD TO RELATIONS

King Fahd University of Petroleum and Minerals

5 Integrity Constraints and Triggers

Oracle Tables TECHGOEASY.COM

SQL language. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

15CSL58: DATABASE MANAGEMENT LABORATORY

Lecture 07. Spring 2018 Borough of Manhattan Community College

Lab # 3 Hands-On. DML Basic SQL Statements Institute of Computer Science, University of Tartu, Estonia

D B M G. SQL language: basics. Managing tables. Creating a table Modifying table structure Deleting a table The data dictionary Data integrity

SQL Server. Lecture3 Cascading referential integrity constraint

LECTURE10: DATA MANIPULATION IN SQL, ADVANCED SQL QUERIES

COSC344 Database Theory and Applications. Lecture 6 SQL Data Manipulation Language (1)

Sample Question Paper

Visit for more.

LECTURE11: TRANSACTION CONTROL LANGUAGE DATA CONTROL LANGUAGE

Lab # 4 Hands-On. DDL and DML Advance SQL Statements Institute of Computer Science, University of Tartu, Estonia

4 Schema Definition with SQL / DDL (II)

SQL Fundamentals. Chapter 3. Class 03: SQL Fundamentals 1

Structured Query Language. ALTERing and SELECTing

The Relational Model. Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC)

In This Lecture. SQL Data Definition SQL SQL. Non-Procedural Programming. Notes. Database Systems Lecture 5 Natasha Alechina

G64DBS Database Systems. G64DBS Module. Recommended Textbook. Assessment. Recommended Textbook. Recommended Textbook.

DATABASE MANAGEMENT SYSTEMS

Appendix A Practices and Solutions

Objectives of logical design... Transforming the ERD diagram into relations. Relational database components. Mapping a composite attribute

Database design process

Teach Yourself InterBase

Database Logical Design

MTAT Introduction to Databases

CIS Slightly-different version of Week 10 Lab, also intro to SQL UPDATE and DELETE, and more

An Effective Methodology for an Upper-level Fundamentals of Database Systems Course

COSC344 Database Theory and Applications. Lecture 11 Triggers

SQL is an English like language consisting of commands to store, retrieve, maintain & regulate access to your database.

Islamic University of Gaza Faculty of Engineering Department of Computer Engineering Fall 2011 ECOM 4113: Database System Lab Eng.

Chapter-14 SQL COMMANDS

UNIT 4 DATABASE SYSTEM CATALOGUE

PROCEDURAL DATABASE PROGRAMMING ( PL/SQL AND T-SQL)

ITCertMaster. Safe, simple and fast. 100% Pass guarantee! IT Certification Guaranteed, The Easy Way!

Full file at

Question Bank IT(402) Unit Database 1. What is database? Ans: A database is a collection of information that is organized so that it can be easily

SQL Simple Queries. Chapter 3.1 V3.01. Napier University

Outer Join, More on SQL Constraints

8) A top-to-bottom relationship among the items in a database is established by a

LECTURE 3: ENTITY-RELATIONSHIP MODELING

Transcription:

IS220 : Database Fundamentals College of Computer and Information Sciences - Information Systems Dept. Lecture7: SQL Overview, Oracle Data Type, DDL and Constraints Part #2 Ref. Chapter6 Prepared by L. Nouf Almujally & Aisha AlArfaj& L.Fatima Alhayan 1

The Process of Database Design Real World Domain Conceptual model (ERD) Relational Data Model Create schema (DDL) Load Data (DML) 2

ALTER TABLE The ALTER TABLE statement is used to: Add columns Delete columns Modify columns Add a new constraint Remove an existing constraint 3Lecture7

ALTER TABLE - Add Column 1. To add a column in a table, use the following syntax: ALTER TABLE table_name ADD column_name datatype ; EXAMPLE: ALTER TABLE Student ADD Degree VARCHAR2(50) ; Table altered. 4Lecture7

ALTER TABLE - Delete Column 2. To delete a column in a table, use the following syntax: ALTER TABLE table_name DROP COLUMN column_name [CASCADE CONSTRAINTS] ; - Use [CASCADE CONSTRAINTS] to drop PRIMARY KEY column with dependent integrity constraints. EXAMPLE: ALTER TABLE Student Table altered. DROP COLUMN Degree; 5Lecture7

SQL> create table parent Table created. (id number(2) primary key); SQL> create table child (id number(2), Constraint id_fk foreign key (id) references parent(id)); Table created. SQL> alter table parent drop column id; alter table parent drop column id * ERROR at line 1: ORA-12992: cannot drop parent key column 6Lecture7

Use Cascade Constraint oracle Drops ID in Parent table and removes all reference constraints to this key. So the foreign key in Child table is removed. SQL> alter table parent drop column id CASCADE CONSTRAINTS; Table altered. 7Lecture7

ALTER TABLE Modify Column 3. To modify a column data type or constraint in a table, use the following syntax: ALTER TABLE table_name MODIFY (Column_name column_specification) ; EXAMPLE: ALTER TABLE orders Table altered. MODIFY (quantity number(5)); 8Lecture7

ALTER TABLE - Add Constraint 4. To add a new constraint in a table, use the following syntax: ALTER TABLE table_name ADD CONSTRAINT Constraint_name Constraint_type (column_name) ; EXAMPLE: ALTER TABLE Module ADD CONSTRAINT ck_unique UNIQUE (title); Table altered. 9Lecture7

ALTER TABLE - Remove Constraint 5. To remove constraint in a table, use the following syntax: ALTER TABLE table_name DROP CONSTRAINT Constraint_name [CASCADE] ; - Use [CASCADE] to delete PRIMARY KEY constraint with dependent integrity constraints. EXAMPLE1: ALTER TABLE Module DROP CONSTRAINT ck_unique; Table altered. 10

SQL> create table parent Table created. (id number(2) primary key); SQL> create table child (id number(2), Constraint id_fk foreign key (id) references parent(id)); Table created. SQL> ALTER TABLE Parent DROP PRIMARY KEY; ALTER TABLE Parent * ERROR at line 1:ORA-02273: this unique/primary key is referenced by some foreign keys 11

ALTER TABLE - Disable Constraint 6. disable constraint is used to deactivate (turn off) an integrity constraint temporarily, use the following syntax: ALTER TABLE table_name DISABLE CONSTRAINT constraint_name [CASCADE] ; Use [CASCADE] to disable PRIMARY KEY with dependent integrity constraints. Example : ALTER TABLE Parent DISABLE CONSTRAINT emp_empno_pk CASCADE; Table altered. ** disables a PRIMARY KEY constraint and any FOREIGN KEY constraints that depend on it (refer to it) 12

ALTER TABLE - Enable Constraint 7. Activate an integrity constraint currently disabled in the table definition by using the ENABLE clause. Use the following syntax: ALTER TABLE table_name ENABLE CONSTRAINT constraint_name ;; 13

DROP TABLES Syntax: Drop table table_name; OR Drop table table_name [Cascade Constraints]; Pay attention to referential integrity constraints when dropping tables. If Cascade Constraints is used, the constraints will be dropped first. 14

Lecture6 Example FK PK DeptNo DeptName EmpId EmpName DeptNo 101 AAA 102 BBB 103 CCC Department 1 Ali 101 2 Sally 101 3 John 103 Employee SQL> drop table Department; drop table Department * ERROR at line 1:ORA02449: unique/primary keys in table referenced by foreign keys SQL> drop table Department Cascade Constraints ; Table dropped. 15

Viewing Table Structures To see the structure of a table, type or DESCRIBE table_name; DESC table_name ; SQL> describe airlines; 16

Constraints Name Code Describes all constraint definitions on tables owned by the current user. SQL> select constraint_name, constraint_type from user_constraints where table_name = table name; Example: SQL> select constraint_name, constraint_type from user_constraints where table_name = PARENT ; CONSTRAINT_NAME C ------------------------------------------- SYS_C00657541 P system generated constraint name. 17

Alter Table Summary ALTER TABLE table_name ADD column_name data type; DROP COLUMN column_name [CASCADE CONSTRAINTS] ; MODIFY (Column_name column_specification) ; ADD CONSTRAINT constraint_name cons_type (col_name); DROP CONSTRAINT constraint_name [CASCADE] ; DISABLE CONSTRAINT constraint_name [CASCADE] ; ENABLE CONSTRAINT constraint_name; 18

19

References Database Systems: A Practical Approach to Design, Implementation and Management. Thomas Connolly, Carolyn Begg. 5 th Edition, Addison-Wesley, 2009. 20