Fundamental of Database Systems

Slides:



Advertisements
Similar presentations
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5 More SQL: Complex Queries, Triggers, Views, and Schema Modification.
Advertisements

SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Database Basics I101 Summer 2006 Copyright 2004, Matt Hottell.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Basic (Flat) Relational Model.
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
Introduction to Structured Query Language (SQL)
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
Exam 2 Review Dr. Bernard Chen Ph.D. University of Central Arkansas.
Oracle Data Definition Language (DDL)
Database. Basic Definitions Database: A collection of related data. Database Management System (DBMS): A software package/ system to facilitate the creation.
INTEGRITY. Integrity constraint Integrity constraints are specified on a database schema and are expected to hold on every valid database state of the.
SQL Data Definition (CB Chapter 6) CPSC 356 Database Ellen Walker Hiram College (Includes figures from Database Systems by Connolly & Begg, © Addison Wesley.
Ms. Hatoon Al-Sagri CCIS – IS Department SQL-99 :Schema Definition, Constraints, Queries, and Views 1.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Slide Chapter 5 The Relational Data Model and Relational Database Constraints.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 7 (Part a): Introduction to SQL Modern Database Management 9 th Edition Jeffrey A.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
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.
CREATE TABLE CREATE TABLE statement is used for creating relations Each column is described with three parts: column name, data type, and optional constraints.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
CSE314 Database Systems Lecture 3 The Relational Data Model and Relational Database Constraints Doç. Dr. Mehmet Göktürk src: Elmasri & Navanthe 6E Pearson.
Constraints Lesson 8. Skills Matrix Constraints Domain Integrity: A domain refers to a column in a table. Domain integrity includes data types, rules,
DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E) 1.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Lecture 03 Constraints. Example Schema CONSTRAINTS.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
Constraints Advanced Database Systems Dr. AlaaEddin Almabhouh.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 Basic SQL تنبيه : شرائح العرض (Slides) هي وسيلة لتوضيح الدرس واداة.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
Chapter 3 The Relational Data Model and Relational Database Constraints Copyright © 2004 Pearson Education, Inc.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 The Relational Data Model and Relational Database Constraints تنبيه.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
Fundamental of Database Systems
Fundamental of Database Systems
Getting started with Accurately Storing Data
Fundamentals of DBMS Notes-1.
Database Constraints Ashima Wadhwa.
國立臺北科技大學 課程:資料庫系統 Chapter 7 SQL Data Definition.
Managing Tables, Data Integrity, Constraints by Adrienne Watt
SQL: Schema Definition and Constraints Chapter 6 week 6
Insert, Update and the rest…
Constraints and Triggers
Quiz Questions Q.1 An entity set that does not have sufficient attributes to form a primary key is a (A) strong entity set. (B) weak entity set. (C) simple.
Database Systems Instructor Name: Lecture-12.
Module 5: Implementing Data Integrity by Using Constraints
Lecturer: Mukhtar Mohamed Ali “Hakaale”
Accounting System Design
DATABASE SQL= Structure Query Language مبادئ قواعد بيانات
CS122 Using Relational Databases and SQL
Accounting System Design
Oracle Data Definition Language (DDL)
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Data Definition Language
ISC321 Database Systems I Chapter 4: SQL: Data definition, Constraints, and Basic Queries and Updates Fall 2015 Dr. Abdullah Almutairi.
CS122 Using Relational Databases and SQL
CS1222 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
Instructor: Samia arshad
Session - 6 Sequence - 1 SQL: The Structured Query Language:
CS122 Using Relational Databases and SQL
-Transactions in SQL -Constraints and Triggers
SQL (Structured Query Language)
Presentation transcript:

Fundamental of Database Systems 1401312-3 By Dr Abdullah Alzahrani د. عبدالله الزهراني aahzahrani@uqu.edu.sa SQL Data Definition

Reference الكتاب المرجع Fundamentals of Database Systems, 5th ed., by Elmasri and Navathe, Pearson International Edition, 2007. http://dev.mysql.com/doc/ Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016

SQL DD DD (Data Definition) CREATE TABLE SQL Constraints ALTER TABLE NOT NULL Constraint DEFAULT Constraint PRIMARY KEY Constraint FOREIGN KEY Constraint UNIQUE Constraint CHECK Constraint ALTER TABLE DROP TABLE Some of the materials and examples are taken from: http://www.w3schools.com/ and the reference book Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016

SQL DD The CREATE TABLE Command CREATE TABLE Command is used to specify a new relation by giving it a name and specifying its attributes and initial constraints. The attributes are specified first. Each attribute is given a name, a data type to specify its domain of values. The key, entity integrity, and referential integrity constraints can be specified within the CREATE TABLE statement after the attributes are declared, or they can be added later using the ALTER TABLE Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016

SQL DD Important terms Domain Constraints: Domain constraints specify that within each tuple, the value of each attribute must be of correct data type Key constraint: defines a superkey of a relation so any two distinct tuples in any state of the relation cannot have identical values. This is to specify a uniqueness constraint. Key constraints and entity integrity constraints are specified on individual relations Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016

SQL DD Important terms Entity integrity constraint: states that no primary key value can be NULL. This is because the primary key value is used to identify individual tuples in a relation. Having NULL values for the primary key implies that we cannot identify some tuples. For example, if two or more tuples had NULL for their primary keys, we may not be able to distinguish them if we try to reference them from other relations. Referential integrity: The referential integrity constraint is specified between two relations and is used to maintain the consistency among tuples in the two relations. Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016

SQL DD we can specify that every student record must be related to a department record. This is known as a referential integrity constraint. Another type of constraint specifies uniqueness on data item values, such as every student record must have a unique value for student_id. This is known as a key or uniqueness constraint. Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016

SQL DD In SQL, we have the following constraints: NOT NULL - Indicates that a column cannot store NULL value UNIQUE - Ensures that each row for a column must have a unique value PRIMARY KEY - A combination of a NOT NULL and UNIQUE. Ensures that a column (or combination of two or more columns) have a unique identity which helps to find a particular record in a table more easily and quickly FOREIGN KEY - Ensure the referential integrity of the data in one table to match values in another table CHECK - Ensures that the value in a column meets a specific condition DEFAULT - Specifies a default value for a column Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016

SQL DD The CREATE TABLE Command FORM Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016

SQL DD The CREATE TABLE examples Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016

SQL DD The CREATE TABLE examples Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016

SQL DD The CREATE TABLE examples Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016

SQL DD The CREATE TABLE examples Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016

SQL DD The ALTER TABLE Command The definition of a table can be changed by using the ALTER command. ALTER TABLE actions include: Adding or dropping a column (attribute); Changing a column definition; Adding or dropping table constraints. Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016

SQL DD The ALTER TABLE Command FORMs ALTER TABLE table_name ADD column_name datatype Ex: ALTER TABLE student add COLUMN student_name varchar(100) ALTER TABLE table_name DROP COLUMN column_name Ex: ALTER TABLE student drop COLUMN student_name; ALTER TABLE table_name MODIFY COLUMN column_name datatype Ex: ALTER TABLE student MODIFY COLUMN student_name varchar(100); Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016

SQL DD The ALTER TABLE Command FORMs (cont.) ALTER TABLE table_name ADD constraint … ALTER TABLE table_name DROP constraint … Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016

SQL DD The ALTER TABLE Command FORMs ALTER TABLE and PRIMARY KEY Ex: ALTER TABLE student ADD PRIMARY KEY (student_id); Ex: ALTER TABLE student ADD CONSTRAINT pk_student_ID PRIMARY KEY (student_Id, student_Name) Ex: ALTER TABLE student DROP PRIMARY KEY ALTER TABLE and FOREIGN KEY Ex: ALTER TABLE student ADD FOREIGN KEY (DEP_Id) REFERENCES DEPARTMENT(DEPARTMENT_ID) Ex:ALTER TABLE student ADD CONSTRAINT fk_student FOREIGN KEY (DEP_Id, speciality_id) REFERENCES DEPARTMENT(DEPARTMENT_ID, DEPARTMENT_ID) Ex: ALTER TABLE student DROP FOREIGN KEY fk_student Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016

SQL DD The ALTER TABLE Command FORMs ALTER TABLE and DEFAULT constraint Ex: ALTER TABLE student ALTER COLUMN student_name set default 's1' ALTER TABLE student ALTER student_name DROP DEFAULT ALTER TABLE and CHECK constraint Ex: ALTER TABLE student ADD CONSTRAINT con438 CHECK (student_Id>438000); Ex: ALTER TABLE student DROP con438 Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016

SQL DD The ALTER TABLE Command FORMs ALTER TABLE and UNIQUE constraint Ex: ALTER TABLE student ADD CONSTRAINT studentUNIQ UNIQUE (student_Id, student_Name) ALTER TABLE student DROP CONSTRAINT  studentUNIQ ALTER TABLE and NOT NULL constraint Ex: ALTER TABLE student MODIFY COLUMN Email varchar(100) NOT NULL; ALTER TABLE student MODIFY COLUMN Email varchar(100); Just remove the NOT NULL from the alter to allow NULL values. Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016

SQL DD The DROP TABLE Command The DROP command can be used to drop named schema elements, such as tables, domains, or constraints; The relation and its definition can be deleted by using the DROP TABLE command Notice that the DROP TABLE command not only deletes all the records in the table if successful, but also removes the table definition Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016

SQL DD The DROP TABLE Command There are two drop behaviour options: CASCADE. RESTRICT. Example: DROP TABLE DEPENDENT CASCADE If the RESTRICT option is chosen instead of CASCADE, a table is dropped only if it is not referenced in any constraints. With the CASCADE option, all such constraints, views, and other elements that reference the table being dropped are also dropped automatically from the schema, along with the table itself. Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016

SQL DD The DROP TABLE Command FORM Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa Fundamental of Database Systems 1401312-3 9/20/2016

End SQL Queries Dr Abdullah Alzahrani. aahzahrani@uqu.edu.sa 9/20/2016 Fundamental of Database Systems 1401312-3 9/20/2016