Relational Data Structure and Concepts. Structured Query Language (Part 1) The Entity Integrity Rules. Relational Data Structure and Concepts

Similar documents
Introduction The SELECT statement: basics Nested queries Set operators Update commands Table management

Database Management System 9

Database Systems Relational Model. A.R. Hurson 323 CS Building

Relation Databases. By- Neha Tyagi PGT CS KV 5 Jaipur II Shift Jaipur Region. Based on CBSE Curriculum Class -11. Neha Tyagi, PGT CS II Shift Jaipur

Chapter # 7 Introduction to Structured Query Language (SQL) Part I

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

UFCEKG 20 2 : Data, Schemas and Applications

Solved MCQ on fundamental of DBMS. Set-1

Sankalchand Patel College of Engineering, Visnagar B.E. Semester III (CE/IT) Database Management System Question Bank / Assignment

UNIT-IV (Relational Database Language, PL/SQL)

Unit 4 SQL language: other definitions

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

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

Supplier-Parts-DB SNUM SNAME STATUS CITY S1 Smith 20 London S2 Jones 10 Paris S3 Blake 30 Paris S4 Clark 20 London S5 Adams 30 Athens

G64DBS Database Systems. Lecture 7 SQL SELECT. The Data Dictionary. Data Dictionaries. Different Sections of SQL (DDL) Different Sections of SQL (DCL)

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

CS6312 DATABASE MANAGEMENT SYSTEMS LABORATORY L T P C

Review -Chapter 4. Review -Chapter 5

MySQL Introduction. Practical Course Integrative Bioinformatics. March 7, 2012

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

Multiple-Choice. 3. When you want to see all of the awards, even those not yet granted to a student, replace JOIN in the following

Information Systems. Relational Databases. Nikolaj Popov

Business Analytics. SQL PL SQL [Oracle 10 g] P r i n c e S e t h i w w w. x l m a c r o. w e b s. c o m

A Sample Solution to the Midterm Test

CPS510 Database System Design Primitive SYSTEM STRUCTURE

12. MS Access Tables, Relationships, and Queries

SQL functions fit into two broad categories: Data definition language Data manipulation language

Deccansoft softwareservices-microsoft Silver Learing Partner. SQL Server Syllabus

SQL - Subqueries and. Schema. Chapter 3.4 V4.0. Napier University

Database Programming with SQL

Overview. Data Integrity. Three basic types of data integrity. Integrity implementation and enforcement. Database constraints Transaction Trigger

Database Applications (15-415)

Assignment Session : July-March

SQL Interview Questions

Oracle. SQL(Structured Query Language) Introduction of DBMS. Build In Function. Introduction of RDBMS. Grouping the Result of a Query

The Structured Query Language Get Started

Basant Group of Institution

Mahathma Gandhi University

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

1. Using Bitvise SSH Secure Shell to login to CS Systems Note that if you do not have Bitvise ssh secure shell on your PC, you can download it from

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

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

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

2. Software Oracle 12c is installed on departmental server machines.

Introduction to Databases

DB Creation with SQL DDL

Unit 1 - Advanced SQL

Chapter 1 SQL and Data

Chapter 4. The Relational Model

Sql Server Syllabus. Overview

Techno India Batanagar Computer Science and Engineering. Model Questions. Subject Name: Database Management System Subject Code: CS 601

RDBMS-Day3. SQL Basic DDL statements DML statements Aggregate functions

A7-R3: INTRODUCTION TO DATABASE MANAGEMENT SYSTEMS

Oracle User Administration

Example 1 - Create Horizontal View. Example 2 - Create Vertical View. Views. Views

Data Manipulation (DML) and Data Definition (DDL)

EDUVITZ TECHNOLOGIES

Lab # 4. Data Definition Language (DDL)

Introduction to Relational Databases. Introduction to Relational Databases cont: Introduction to Relational Databases cont: Relational Data structure

II B.Sc(IT) [ BATCH] IV SEMESTER CORE: RELATIONAL DATABASE MANAGEMENT SYSTEM - 412A Multiple Choice Questions.

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

Lecture 07. Spring 2018 Borough of Manhattan Community College

Unit 3 The Relational Model

Index. Bitmap Heap Scan, 156 Bitmap Index Scan, 156. Rahul Batra 2018 R. Batra, SQL Primer,

CBSE Revision Notes Class-11 Computer Science (New Syllabus) Unit 3: Data Management (DM-1) Database

Further GroupBy & Extend Operations

Database Systems Laboratory 5 Grouping, DCL & TCL

UNIT 4 DATABASE SYSTEM CATALOGUE

Relational Model. Rab Nawaz Jadoon DCS. Assistant Professor. Department of Computer Science. COMSATS IIT, Abbottabad Pakistan

D B M G 2. SQL language: basics. Update commands. Introduction The INSERT command The DELETE command The UPDATE command

Oracle Syllabus Course code-r10605 SQL

Sample Question Paper

MySQL Introduction. By Prof. B.A.Khivsara

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

RELATIONAL DATA MODEL

Birkbeck. (University of London) BSc/FD EXAMINATION. Department of Computer Science and Information Systems. Database Management (COIY028H6)

JSPM s Bhivarabai Sawant Institute of Technology & Research, Wagholi, Pune Department of Information Technology

Oracle SQL & PL SQL Course

ASSIGNMENT NO 2. Objectives: To understand and demonstrate DDL statements on various SQL objects

King Fahd University of Petroleum and Minerals

Chapter 8 INTEGRITY 1

Course Outline and Objectives: Database Programming with SQL

U1. Data Base Management System (DBMS) Unit -1. MCA 203, Data Base Management System

LABSHEET 1: creating a table, primary keys and data types

Introduction to SQL Part 1 By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

The Relational Model

Structured Query Language. ALTERing and SELECTing

Oracle Database 10g: Introduction to SQL

Today Learning outcomes LO2

CPS 510 Data Base I. There are 3 forms of database descriptions the ANSI/SPARK, 1975 and so on

CMPT 354: Database System I. Lecture 2. Relational Model

B.H.GARDI COLLEGE OF MASTER OF COMPUTER APPLICATION. Ch. 1 :- Introduction Database Management System - 1

CSC Web Programming. Introduction to SQL

Database Modelling. Lecture 4 (b): Database Integrity, Keys & Constraints. Akhtar Ali 10/14/2014 1

1 Writing Basic SQL SELECT Statements 2 Restricting and Sorting Data

Elena Baralis and Tania Cerquitelli 2013 Politecnico di Torino 1

- Database: Shared collection of logically related data and a description of it, designed to meet the information needs of an organization.

More SQL: Complex Queries, Triggers, Views, and Schema Modification

DS Introduction to SQL Part 1 Single-Table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

A Unit of SequelGate Innovative Technologies Pvt. Ltd. All Training Sessions are Completely Practical & Real-time

Transcription:

Relational Data Structure and Concepts Structured Query Language (Part 1) Two-dimensional tables whose attributes values are atomic. At every row-and-column position within the table, there always exists precisely one value, never a list of values. (Relations do not contain repeating groups after INF) There are no duplicate rows (There is always a primary key in a relation). Rows (tuples) are unordered. DB212 Peter Lo 2004 1 DB212 Peter Lo 2004 2 Relational Data Structure and Concepts Degree of Relation is the number of attributes in relation. Cardinality of Relation is the number of tuples in relation. Domain is a pool of values, from which one of more attributes draw their actual values. For example, a domain of supplier numbers is the set of all possible supplier numbers and the domain. The Entity Integrity Rules No component of the primary key of a relation is allowed to accept nulls. Example Suppose in the supplier relation, there is a tuple for which the supplier number is null. This would be like saying that there was a supplier in the real world that had no identity. DB212 Peter Lo 2004 3 DB212 Peter Lo 2004 4

The Referential Integrity Rule The database must not contain any unmatched foreign key values. The referential integrity rule says that if B references A, then A must exist. Consideration in Foreign Key (1) Can that foreign key accept nulls? Example Does it make sense for a shipment to exist for which the supplier is unknown? DB212 Peter Lo 2004 5 DB212 Peter Lo 2004 6 Consideration in Foreign Key (2) Consideration in Foreign Key (3) What should happen on an attempt to delete the target of a foreign key references? Example An attempt to delete a supplier for which there exists at least at one matching shipment? What should happen on an attempt to update the primary key of a foreign key reference? Example An attempt to update the supplier number for a supplier for which there exists at least one matching shipment? DB212 Peter Lo 2004 7 DB212 Peter Lo 2004 8

SQL and Relational Model Examples of DDL & DML SQL (Structured Query Language) is a relational language, which provides an interface between the user and the RDBMS. SQL s capabilities are divided between the Data Description Language (DDL) and the Data Manipulation Language (DML). DDL Create tables Modify tables (ALTER) Create indexes Drop tables Drop indexes Authorisation (GRANT) Deauthorisation (REVOKE) DML Query table data Modify table data Insert Delete Update Aggregate functions DB212 Peter Lo 2004 9 DB212 Peter Lo 2004 10 Relational Data Definition in SQL Data Description Language (DDL) The purpose of the DDL is to create entries in the catalogue (sometimes called the data dictionary or, in some large systems, the repository). The catalogue is managed by the RDBMS and it is a requirement of the Relational Model that the catalogue itself takes the form of normalized tables. The update of these tables is quite transparent to the user. DB212 Peter Lo 2004 11 DB212 Peter Lo 2004 12

Common DDL Command Creating Tables CREATE TABLE tablename (colname1 type(width), colname2 type(width) : colnamen type(width)); CREATE TABLE SUPPLIER ( SNO CHAR(2), SNAME CHAR(5), STATUS NUMBER(5) ); SUPPLIER DB212 Peter Lo 2004 13 DB212 Peter Lo 2004 SNO SNAME STATUS 14 Altering Tables Add New Column Altering Tables Modify Column ALTER TABLE tablename ADD (colname1 type(width),... colnamen ALTER TABLE SUPPLIER ADD (CITY CHAR(4)); type(width); SUPPLIER Modify data type, size of an existing column ALTER TABLE tablename MODIFY (colname1 type(width),... colnamen type(width); ALTER TABLE SUPPLIER MODIFY (CITY CHAR(6)); DB212 Peter Lo 2004 SNO SNAME STATUS CITY15 DB212 Peter Lo 2004 16

Deleting Tables Indexing Tables Once a table is dropped, all the data will be deleted permanently. Syntax DROP TABLE tablename; DROP TABLE SUPPLIER; Indexing tables helps the DBMS query large tables more quickly and efficiently. Syntax CREATE INDEX indexname ON tablename (colname1,... colnamen); Data are normally inserted first before creating an index. The reverse must be done if your index is unique (each value in a column is unique). CREATE UNIQUE INDEX SUPP_NO ON SUPPLIER (SNO); DB212 Peter Lo 2004 17 DB212 Peter Lo 2004 18 Deleting Indexes Syntax DROP INDEX indexname; DROP INDEX SUPP_NO; Views A View is also called a Virtual Table because it does not actually contain stored data, combines data that is selected from one or more base tables of the other views. If the data in the base tables changes, the information displayed using the view also changes. Provide simplicity to see exactly what you need; at the same time providing the security to prevent unauthorized users from seeing certain information. We can use a view to let someone access just the rows and columns that the person needs from base tables. We can omit columns containing irrelevant or sensitive information. DB212 Peter Lo 2004 19 DB212 Peter Lo 2004 20

Creating Views CREATE VIEW viewname AS SELECT colname1, colnamen FROM tablename1, tablenamen WHERE (condition expression); Example CREATE VIEW VENDOR AS SELECT SNO, SNAME, CITY FROM SUPPLIER; Deleting Views When we delete a view, the table on which the view is deleted will not be deleted. DROP VIEW viewname; DROP VIEW VENDOR DB212 Peter Lo 2004 21 DB212 Peter Lo 2004 22 Inserting Records Updating Data INSERT INTO tablename (colname1,... colnamen) VALUES (... ); INSERT INTO SUPPLIER VALUES (S2, JONES, 10); Value of column would be NULL if it is not included in the INSERT clause. SUPPLIER SNO SNAME STATUS CITY S1 SMITH 20 LONDON DB212 Peter Lo 2004 S2 JONES 10 23 Syntax UPDATE tablename SET colname1 = value,... colnamen = value WHERE (condition expression); UPDATE SUPPLIER SET CITY= ROME, WHERE (SNO = S2 ); SUPPLIER SNO SNAME STATUS CITY DB212 Peter Lo 2004 S1 SMITH 20 LONDON 24 S2 JONES 10 ROME

Update Data (Delete Attribute) Deletion of selective attributes (or data) is done by updating the column to NULL. UPDATE SET WHERE SUPPLIER CITY = NULL, (SNAME = BLAKE ); Delete Record Removing a tuple. DELETE FROM tablename WHERE (condition expression); DELETE SUPPLIER WHERE (SNAME = BLAKE ); DB212 Peter Lo 2004 25 DB212 Peter Lo 2004 26 Controlling Changes Use COMMIT command to make changes permanent. Example of explicit COMMIT: UPDATE SUPPLIER SET CITY = PARIS, WHERE (CITY = ROME ); COMMIT; Without evoking COMMIT, your record(s) may not be updated. Controlling Changes Some SQL commands cause implicit COMMIT effect. SQL commands that cause implicit COMMIT: ALTER AUDIT COMMENT CONNECT CREATE DISCONNECT DROP EXIT NOAUDIT REVOKE QUIT RENAME You can perform automatic COMMIT by: SET AUTOCOMMIT ON Note that this cause changes to take immediate effect after INSERT, UPDATE or DELETE is evoked. DB212 Peter Lo 2004 27 DB212 Peter Lo 2004 28

Canceling Changes Data Security Using ROLLBACK command cancels all pending changes The database is restored to its state at the time of the last COMMIT. ROLLBACK This would not have any effect if you have performed a COMMIT before the ROLLBACK. DB212 Peter Lo 2004 29 Database administrator has all privileges. Privileges: resource logon, create tables connect logon, query only Sytnax GRANT<privileges> TO username IDENTIFIED BY password; GRANT connect, resource TO SCOTT IDENTIFIED BY TIGER; DB212 Peter Lo 2004 30 Data Security You can permit a user to query and make changes to your tables. GRANT SELECT, INSERT ON SUPPLIER TO SCOTT; Table owner can grant them privileges: SELECT INSERT UPDATE DELETE ALTER INDEX To grant a user all privileges: GRANT ALL ON SUPPLIER TO SCOTT; DB212 Peter Lo 2004 31 Data Security You can give other user to grant privileges for one of your tables. GRANT ALL ON SUPPLIER TO SCOTT WITH GRANT OPTION; Revoking for system privileges can only be done by the database administrator. Table privileges can be revoked any time by the respective users. REVOKE INSERT ON SUPPLIER FROM SCOTT; DB212 Peter Lo 2004 32

Query Database Query Database - Projection SELECT colname1,, colnamen FROM tablename1,, tablenamen WHERE (condition expression) GROUP BY (condition expression) HAVING (condition expression) ORDER BY colname1,, colnamen; Selecting all columns: SELECT * FROM SUPPLIER; Selecting specific columns: SELECT SNO, SNAME, CITY FROM SUPPLIER; This is also known as projection. DB212 Peter Lo 2004 33 DB212 Peter Lo 2004 34 Query Database - Restriction Query Database - IN Selecting specific records or tuples (Restriction) SELECT * FROM SUPPLIER WHERE CITY = LONDON ; Selecting specific atomic data: SELECT SNO, SNAME, CITY FROM SUPPLIER WHERE CITY = LONDON ; Selecting data BETWEEN a range: SELECT SNO, PNO, JNO FROM SPJ WHERE QTY BETWEEN 400 AND 700; Equal to any member IN the list: SELECT * FROM PART WHERE PNAME IN ( NUT, SCREW ); Another example: SELECT PNO, PNAME, WEIGHT, CITY FROM SUPPLIER WHERE CITY NOT IN ( ROME, PARIS ; DB212 Peter Lo 2004 35 DB212 Peter Lo 2004 36

Query Database LIKE Matching a pattern LIKE %; SELECT * FROM SUPPLIER WHERE SNAME LIKE SMITH% ; Supplier names like SMITH & SMITH, and SMITHEN would all be acceptable. SELECT * FROM SUPPLIER WHERE SNAME LIKE %SMITH%, Supplier names like ADAM SMITH, GOLDSMITH, and SMITH & ALICE would all be acceptable. DB212 Peter Lo 2004 37