Murali Mani SQL: Updates (DML) and Views (DDL). Murali Mani SQL DML (Updating the Data) Insert Delete Update.

Slides:



Advertisements
Similar presentations
Basic SQL Introduction Presented by: Madhuri Bhogadi.
Advertisements

Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Relational Databases Chapter 4.
Murali Mani Normalization. Murali Mani What and Why Normalization? To remove potential redundancy in design Redundancy causes several anomalies: insert,
VIEWS Pertemuan 7 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Dec 4, 2003Murali Mani SQL B term 2004: lecture 14.
Murali Mani The Relational Model. Murali Mani Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
Chapter 3. 2 Chapter 3 - Objectives Terminology of relational model. Terminology of relational model. How tables are used to represent data. How tables.
Nov 24, 2003Murali Mani SQL B term 2004: lecture 12.
CS3431 SQL : Query Language. CS3431 SELECT-FROM-WHERE SELECT * FROM Student WHERE sName=“Greg” AND address=“320 FL”  (sName=“Greg” AND address=“320 FL”)
Murali Mani SQL. Murali Mani SELECT-FROM-WHERE SELECT * FROM Student WHERE sName=“Greg” AND address=“320 FL”  (sName=“Greg” AND address=“320 FL”) (Student)
Cs3431 SQL: Updates (DML) and Views (DDL). cs3431 SQL DML (Updating the Data) Insert Delete Update.
Cs3431 Normalization. cs3431 Why Normalization? To remove potential redundancy in design Redundancy causes several anomalies: insert, delete and update.
Dec 15, 2003Murali Mani Transactions and Security B term 2004: lecture 17.
View and Materialized view. What is a view? Logically represents subset of data from one or more table. In sql, a view is a virtual relation based on.
Chapter 4 Relational Databases Copyright © 2012 Pearson Education, Inc. publishing as Prentice Hall 4-1.
Chapter 4 Relational Databases Copyright © 2012 Pearson Education 4-1.
SQL's Data Definition Language (DDL) – View, Sequence, Index.
Lecture 2 The Relational Model. Objectives Terminology of relational model. How tables are used to represent data. Connection between mathematical relations.
Chapter 4 The Relational Model Pearson Education © 2014.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
Database Technical Session By: Prof. Adarsh Patel.
Views: Limiting Access to Data A view is a named select statement that is stored in a database as an object. It allows you to view a subset of rows or.
Other database objects (Sequence). What Is a Sequence? A sequence: Automatically generates sequential numbers Is a sharable object Is typically used to.
Chapter 6 Additional Database Objects Oracle 10g: SQL.
Chapter 10 Views. Topics in this Chapter What are Views For? View Retrievals View Updates Snapshots SQL Facilities.
FEN  Data Definition: CREATE TABLE, ALTER TABLE  Data Manipulation: INSERT, UPDATE, DELETE  Queries: SELECT SQL: Structured Query Language.
1 The Relational Model. 2 Why Study the Relational Model? v Most widely used model. – Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. v “Legacy.
Quick review of SQL And conversion to Oracle SQL.
Views Lesson 7.
M1G Introduction to Database Development 5. Doing more with queries.
Constraints, Triggers and Views COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
Chapter 2 Views. Objectives ◦ Create simple and complex views ◦ Creating a view with a check constraint ◦ Retrieve data from views ◦ Data manipulation.
SQL Unit – 2 Base Knowledge Presented By Mr. R.Aravindhan.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
CIS 375—Web App Dev II SQL. 2 Introduction SQL (Structured _______ Language) is an ANSI standard language for accessing databases.ANSI SQL can execute.
CMPT 258 Database Systems The Relationship Model PartII (Chapter 3)
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
The Relational Model. 2 Relational Model Terminology u A relation is a table with columns and rows. –Only applies to logical structure of the database,
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Chapter 3: Relational Databases
The Relational Model © Pearson Education Limited 1995, 2005 Bayu Adhi Tama, M.T.I.
1 CS 430 Database Theory Winter 2005 Lecture 13: SQL DML - Modifying Data.
SQL: Structured Query Language Instructor: Mohamed Eltabakh 1 Part II.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
1 Database Fundamentals Introduction to SQL. 2 SQL Overview Structured Query Language The standard for relational database management systems (RDBMS)
Murali Mani Constraints. Murali Mani Keys: Primary keys and unique CREATE TABLE Student ( sNum int, sName varchar (20), dept char (2), CONSTRAINT key.
Chapter 3 The Relational Model. Why Study the Relational Model? Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. “Legacy.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
SQL: Structured Query Language Instructor: Mohamed Eltabakh 1.
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.
SQL : Query Language Part II CS3431.
SQL Views CS542.
SQL : Query Language CS3431.
Normalization Murali Mani.
SQL Views and Updates cs3431.
Advanced SQL: Views & Triggers
Chapter 2 Views.
Instructor: Mohamed Eltabakh
Chapter 2 Views.
Normalization cs3431.
SQL: Structured Query Language
SQL: Structured Query Language
IST 318 Database Administration
Presentation transcript:

Murali Mani SQL: Updates (DML) and Views (DDL)

Murali Mani SQL DML (Updating the Data) Insert Delete Update

Murali Mani Inserting tuples INSERT INTO Student VALUES (6, ‘Emily’, ‘324 FL’, NULL); INSERT INTO Student (sNumber, sName) VALUES (6, ‘Emily’); INSERT INTO Professor (pNumber) SELECT professor FROM Student;

Murali Mani Delete and Update Deleting tuples DELETE FROM Student WHERE sNumber=‘6’; Updating tuples UPDATE Student SET professor=‘ER’ WHERE sNumer=‘6’

Murali Mani Views NOTE: You can present logical subsets or combinations of the data by creating views of tables. A view is a virtual table based on a table or another view. A view contains no data of its own but is like a window through which data from tables can be viewed or changed. The tables on which a view is based are called base tables. The view is stored as a SELECT statement in the data dictionary.

Murali Mani Views View is a virtual relation Convenience: Queries on base relations might be “complex” Logical Data Independence: “base tables” may change, but still queries using views need not change. Provide different views of the same data. Security: Expose only necessary data to users Views can be queried like any “base” relation.

Murali Mani Views CREATE VIEW as DROP VIEW CREATE VIEW studentProfessor (student, professor) AS SELECT sName, pName FROM Student, Professor WHERE Student.professor = Professor.pNumber; DROP VIEW studentProfessor

Murali Mani Views - Example sNumbersNameaddressprofessor 1Dave320FL1 2Greg320FL1 3Matt320FL2 Student pNumberpNameaddress 1MM235FL 2ER241FL Professor CREATE VIEW studentProfessor (student, professor) AS SELECT sName, pName FROM Student, Professor WHERE Student.professor = Professor.pNumber; SELECT * from studentProfessor studentprofessor DaveMM GregMM MattER

Murali Mani Updating Views Consider views defined with only one relation in the FROM clause such as: CREATE VIEW MyStudent (num, name) AS SELECT sNumber, sName FROM Student; These views are updatable. Updating these views are done by updating the underlying Student tables.

Murali Mani Updating Single relation Views For instance, the following updates are valid: DELETE FROM MyStudent WHERE name=`Dave'; -- This will delete the corresponding row from the Student table INSERT INTO MyStudent VALUES (4, `Mary’); -- This will be translated to INSERT INTO Student(sNumber, sName) VALUES (4, `Mary’);

Murali Mani Inserting into single relation views Consider the view CREATE VIEW MyStudent1(name) AS SELECT sName FROM Student; -- INSERT INTO MyStudent1 VALUES (‘Mary’) will be translated to INSERT INTO Student(sName) VALUES (‘Mary’). This will return an error as sNumber must not be null

Murali Mani Updating Single relation views If the SELECT clause specifies DISTINCT, then the view is not updatable. For instance, the following view is not updatable. CREATE VIEW MyStudent2(num) AS SELECT DISTINCT sNumber FROM Student;

Murali Mani Updating Single Relation Views Note that the WHERE clause may specify subqueries. Let us consider an extreme example. CREATE VIEW MyStudent3 (num, name) AS SELECT sNumber, sName FROM Student WHERE sNumber NOT IN (SELECT sNumber FROM Student); -- this view will always have 0 tuples. Insert into this view will still insert into student table, though that tuple does not appear in the view.

Murali Mani Multiple relation views: Delete Consider a multi-relation view such as CREATE VIEW studentProf(student, professor) AS SELECT sName, pName FROM Student, Professor WHERE professor=pNumber; -- Note that the pNumber is key for professor. We see that sNumber is a key for Student and also for the view (though sNumber does not appear in the result). So deleting from the views are possible by deleting appropriate sNumbers from the Student table.

Murali Mani Deleting from multi-relation views Try the following update statements: DELETE FROM Studentprof WHERE professor='MM'; -- This will actually delete the two rows in the student table. -- Therefore deletes can be done against multi- relation views if there is a table such that the view and the table have the same key.

Murali Mani Deleting from multirelation views Suppose we drop the key constraint on the professor table for the same view. Now delete will fail because there is no table whose key is the key of the view.

Murali Mani Inserting into multi-relation views Consider the following slightly modified view definition CREATE VIEW studentProf(student, professor) AS SELECT sNumber, pName FROM Student, Professor WHERE professor=pNumber; INSERT INTO Studentprof VALUES (4, 'ER'); -- THIS ABOVE INSERT WILL FAIL AS IT TRIES TO INSERT INTO Professor TABLE AS WELL. INSERT INTO Studentprof(student) VALUES (4); -- THIS ABOVE INSERT WILL SUCCEED.

Murali Mani Inserting into multi-relation views Insert will succeed only if The insert translates to insert into only one table. The key for the table to be inserted will also be a key for the view.