Slide 1 Chapter 7 – Part 2 Chapter 7 – Part 2 Constraints JOIN statement View.

Slides:



Advertisements
Similar presentations
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 8-1 David M. Kroenke’s Chapter Eight: Database Redesign Database Processing:
Advertisements

Fundamentals, Design, and Implementation, 9/e Chapter 8 Database Redesign.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Introduction to Structured Query Language (SQL)
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 COS 346 Day 11.
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 COS 346 Day 12.
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Fundamentals, Design, and Implementation, 9/e Chapter 7 Using SQL in Applications.
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.
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
DAVID M. KROENKE’S DATABASE PROCESSING, 10th Edition © 2006 Pearson Prentice Hall 7-1 David M. Kroenke’s Chapter Seven: SQL for Database Construction and.
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 10: Data Definition Language.
Transforming Data Models into Database Designs
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
Database Systems Lecture 5 Natasha Alechina
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
SQL Basics. SQL SQL (Structured Query Language) is a special-purpose programming language designed from managing data in relational database management.
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
SQL data definition using Oracle1 SQL Data Definition using Oracle.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
SQL Server 7.0 Maintaining Referential Integrity.
SQL data definition using Oracle1 SQL Data Definition using Oracle.
Natural vs. Generated Keys. Definitions Natural key—a key that occurs in the data, that uniquely identifies rows. AKA candidate key. Generated key—a key.
SQL Data Definition Language (DDL) Using Microsoft SQL Server 1SDL Data Definition Language (DDL)
Slide 1 Chapter 7 – Part 1 Data Definition Language & Data Manipulation Language.
SQL Basics. 5/27/2016Chapter 32 of 19 Naming SQL commands are NOT case sensitive SQL commands are NOT case sensitive But user identifier names ARE case.
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.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
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.
1 SQL - II Data Constraints –Applying data constraints Types of data constraints –I/O constraints The PRIMARY KEY constraints The FOREIGN KEY constraints.
SQL for Database Construction and Application Processing
CREATE TABLE CREATE TABLE statement is used for creating relations Each column is described with three parts: column name, data type, and optional constraints.
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
SQL introduction 2013.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Indexes and Views Unit 7.
1 DBS201: More on SQL Lecture 3. 2 Agenda How to use SQL to update table definitions How to update data in a table How to join tables together.
Managing Database With Oracle Replacement for Ch10 COP 4708.
Slide 1 Chapter 7 – Part 3 Stored Procedure, Function &Trigger.
CREATE TABLE ARTIST ( ArtistID int NOT NULL IDENTITY (1,1), Namechar(25) NOT NULL, TEXT ERROR Nationality char (30) NULL, Birthdate numeric (4,0) NULL,
SQL constrains and keys. SORTED RESULTS Sort the results by a specified criterion SELECT columns FROM tables WHERE predicates ORDER BY column ASC/DESC;
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
Introduction to Databases & SQL Ahmet Sacan. What you’ll need Firefox, SQLite plugin Mirdb and Targetscan databases.
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.
Fundamentals of DBMS Notes-1.
SQL: Schema Definition and Constraints Chapter 6 week 6
© 2016, Mike Murach & Associates, Inc.
Module 5: Implementing Data Integrity by Using Constraints
Structured Query Language (Data definition Language)
Lecturer: Mukhtar Mohamed Ali “Hakaale”
DATABASE SQL= Structure Query Language مبادئ قواعد بيانات
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
Rob Gleasure robgleasure.com
Database Processing: David M. Kroenke’s Chapter Seven:
CS1222 Using Relational Databases and SQL
Data Definition Language
CS122 Using Relational Databases and SQL
CS1222 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
Instructor: Samia arshad
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
កម្មវិធីបង្រៀន SQL Programming ជាភាសាខ្មែរ Online SQL Training Course
SQL NOT NULL Constraint
Presentation transcript:

Slide 1 Chapter 7 – Part 2 Chapter 7 – Part 2 Constraints JOIN statement View

Slide 2 Contents A.Art Museum Problem B.Solution C.Other Requirements

Slide 3 A. Art Museum Problem 1.Problem Description 2.Examples

Slide 4 1. Problem Description DAWN museum where a lot of valuable works have been exhibited needs to manage and classify its works. Therefore, a database is one of the best solutions. The owner also requests all of works to be arranged by the artist who created these works. To identify the artist, the following information will be used: artist Id, artist name, nationality, birthday, deceased day. In addition, each artist may have many works or not. The work of each artist will be identified by work id, title, copy and description.

Slide 5  Besides the general information, there are some special requirements such as:  Artist’s nationality must be within these following countries: Canadian, English, French, German, Mexican, Russian, Spanish and US.  Deceased day (if any) must be greater than artist’s birthday.  Artist name must be unique  Both Work title and work copy must be unique  As a database developer, you are requested to design the above database.  After that, write SQL statement to display artist name and the number of works (count of works) of each artist.  Finally, write SQL statement to display artist name and the number of works (count of works) of each artist and these work must be first copy.

Slide 6 2. Examples  Artist name: LEIGHTON, SIR FREDERICK  Nationality: English  Birthday: 1830  Deceased day: 1896  Some Chief Works:  First work: Title: Hercules Wrestling with Death Copy: First copy Description: Painting  Second work; Title: Daphnephoria Copy: Original Description: Painting

Slide 7 B. Solution 1.Create Logical Diagram 2.Create Physical Diagram 3.Write SQL Statement to Create Tables 4.Create Constraints 5.Insert data into tables 6.Write SQL Statement to Display information

Slide 8 1. Create Logical Diagram

Slide 9 2. Create Physical Diagram

Slide Write SQL Statement to Create Tables Create Table ARTISTS ( ART_ID int not null Primary Key Identity, ARTName varchar(50) not null Unique, ARTNationality varchar(50) not null, ARTBirthDay datetime not null, ARTDeceasedDaydatetime );

Slide 11 Create Table WORKS ( WOR_ID bigint not null Primary Key Identity, ART_ID int not null, WORTitle varchar(100)not null, WORCopy varchar(20) not null, WORDescriptionvarchar(200), Constraint AK_Title_Copy Unique (WORTitle, WORCopy), Constraint FK_Works Foreign Key (ART_ID) References ARTISTS (ART_ID) on update cascade );

Slide Create Constraints 4.1. Set Constraint For Nationality in Artist 4.2. Set Constraint Between Birth Date and Deceased Date

Slide Set Constraint For Nationality in Artist  Artist’s nationality must be within these following countries: Canadian, English, French, German, Mexican, Russian, Spanish, US. Alter Table ARTISTS Add Constraint Chk_Nationality Check (ARTNationality in ('Canadian','English','French','German','Mexican', 'Russian‘,'Spanish','US'));

Slide Set Constraint Between Birth Date and Deceased Date  Deceased day (if any) must be greater than artist’s birthday Alter Table ARTISTS Add Constraint ChkDeceasedDay Check (ARTDeceasedDay > ARTBirthDay);

Slide Insert data into tables  Exercise: Using DML, write SQL statements to insert the following data into tables (Artists and Works):  Artists table:  Works table: No.NameNationalityBirthdayDeceased day 1Artist 1US10/2/192530/6/1975 2Artist 2US5/4/1955 3Artist 3French6/7/193012/8/1990 4Artist 4Russian8/9/1950 No.TitleCopyDescriptionArtist ID 1Title 1OriginalPainting1 2Title 2First copyPhotograph1 3Title 3OriginalPainting3 4Title 4First copyPainting3 5Title 5Second copyPhotograph3

Slide Write SQL Statement to Display information 6.1. Count Number of Artist’s Work 6.2. Count Number of Artist’s Work which is First Copy

Slide Count Number of Artist’s Work  Write SQL statement to display all artist name and the number of works (count of works) of each artist.  Using Joins: Select a.ArtName, count(w.Wor_ID) as numWork From Artists a, Works w Where a.Art_ID = w.Art_ID Group by a.ArtName;  Result: (in Artist table, there are 4 artists) NamenumWork Artist 12 Artist 33

Slide 18  Using Left Join: Select a.ArtName, count(w.Wor_ID) as numWork From Artists a LEFT JOIN Works w ON a.Art_ID = w.Art_ID Group by a.ArtName;  Result NamenumWork Artist 12 Artist 20 Artist 33 Artist 40

Slide Count Number of Artist’s Work which is First Copy  Write SQL statement to display all artist name and the number of works (count of works) of each artist and these work must be first copy. Select a.ArtName, count(w.Wor_ID) as numWork From Artists a LEFT JOIN Works w ON a.Art_ID = w.Art_ID Where w.WorCopy =‘First copy’ or w.WorCopy is NULL Group by a.ArtName;  Result: NamenumWork Artist 11 Artist 20 Artist 31 Artist 40

Slide 20 C. Other Requirements 1.Creating View Problem 2.Creating View Solution

Slide Creating View Problem  Code a SQL statement to create a view that shows NAME and NATIONALITY for all artist.  Code a SQL statement to create a view that show artist NAME and computes the number of works for each artist.

Slide Creating View Solution 2.1. What is view for? 2.2. Create view

Slide What is view for?  A SQL view is a virtual table that us constructed from other tables or views. A view has no data of its own, but obtain data from tables or other views. View are constructed from SQL SELECT statements.  Using views to hide columns and rows.  Using views to display results of computed columns.  Using views to hide complicated SQL syntax.

Slide Create view Create View to Show Name and Nationality Create View to Show Artist Name and the number of Artist Work

Slide Create View to Show Name and Nationality Create View ArtistInfo As Select ArtName, ArtNationality From Artists;

Slide Create View to Show Artist Name and the number of Artist Work Create View ArtistsWorks As Select a.ArtName, Count(w.Wor_ID) as numWork From Artists a Left Join Works w on a.Art_ID = w.Art_ID Group by a.ArtName;

Slide 27