COMP 430 Intro. to Database Systems

Slides:



Advertisements
Similar presentations
Data Definition and Integrity Constraints
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.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
The Relational Model Class 2 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) (From ER to Relational)
Triggers The different types of integrity constraints discussed so far provide a declarative mechanism to associate “simple” conditions with a table such.
The Relational Model Lecture 3 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) From ER to Relational.
Creating Tables, Defining Constraints Rose-Hulman Institute of Technology Curt Clifton.
Data Integrity Constraints
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
SQL Query Extras MIS 433. Rerunning the last Query n Type the forward slash “/” to rerun the last query that was entered.
Databases in Visual Studio. Database in VisualStudio An MS SQL database are built in Visual studio The Name can be something like ”(localdb)\Projects”
Relational Database Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.
© 2007 by Prentice Hall (Hoffer, Prescott & McFadden) 1 Introduction to SQL.
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 UNIT 6: Chapter 7: Introduction to SQL Modern Database Management 9 th Edition Jeffrey A.
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.
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
Constraints cis 407 Types of Constraints & Naming Key Constraints Unique Constraints Check Constraints Default Constraints Misc Rules and Defaults Triggers.
Managing Constraints. 2 home back first prev next last What Will I Learn? Four different functions that the ALTER statement can perform on constraints.
DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E) 1.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
IS6146 Databases for Management Information Systems Lecture 3: SQL III – The DDL Rob Gleasure robgleasure.com.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
Murali Mani Constraints. Murali Mani Keys: Primary keys and unique CREATE TABLE Student ( sNum int, sName varchar (20), dept char (2), CONSTRAINT key.
Understand Data Definition Language (DDL) Database Administration Fundamentals LESSON 1.4.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
Views / Session 3/ 1 of 40 Session 3 Module 5: Implementing Views Module 6: Managing Views.
COMP 430 Intro. to Database Systems MongoDB. What is MongoDB? “Humongous” DB NoSQL, no schemas DB Lots of similarities with SQL RDBMs, but with more flexibility.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Fundamentals of DBMS Notes-1.
Trigger used in PosgreSQL
Rob Gleasure robgleasure.com
Fundamental of Database Systems
COMP 430 Intro. to Database Systems
CS SQL.
Rob Gleasure robgleasure.com
CS320 Web and Internet Programming SQL and MySQL
COP Introduction to Database Structures
SQL: Schema Definition and Constraints Chapter 6 week 6
Creating Database Triggers
SQL Creating and Managing Tables
Database Keys and Constraints
Module 5: Implementing Data Integrity by Using Constraints
Referential Integrity
SQL Creating and Managing Tables
STRUCTURED QUERY LANGUAGE
Index Structure.
The Relational Model Relational Data Model
SQL Creating and Managing Tables
The Basics of Data Manipulation
Constraints.
SQL data definition using Oracle
Referential Integrity
CMPT 354: Database System I
Unit I-2.
SQL-1 Week 8-9.
CS3220 Web and Internet Programming SQL and MySQL
Data Definition Language
A Very Brief Introduction to Relational Databases
MIS2502: Data Analytics SQL 4– Putting Information Into a Database
Chapter 11 Managing Databases with SQL Server 2000
SQL – Constraints & Triggers
CS3220 Web and Internet Programming SQL and MySQL
Instructor: Samia arshad
Integrity 5/5/2019 See scm-intranet.
កម្មវិធីបង្រៀន SQL Programming ជាភាសាខ្មែរ Online SQL Training Course
SQL NOT NULL Constraint
Presentation transcript:

COMP 430 Intro. to Database Systems Modifying data & schemas

Add, modify, delete data

Data not static Databases are built to be used. Forgetting this is a common student mistake.

Adding data – review INSERT INTO Student VALUES (“S123456789”, “John”, “Doe”, “Houston”, “TX”); INSERT INTO Student VALUES (“S111111111”, “Mary”, “Jones”, “Houston”, “TX”), (“S222222222”, “Joe”, “Wallis”, “Kansas City”, “KS”); INSERT INTO Student (id, first_name, last_name) VALUES (“S987654321”, “Jane”, “Smith”);

Saving query results into a table INSERT INTO Student SELECT …; INSERT INTO Student (id, first_name, last_name) Existing table SELECT … INTO new_table_name …; New table

Modifying & deleting data UPDATE Student SET city = “Austin”, state = “TX” WHERE id = “S123456789”; DELETE FROM Student; DELETE FROM Student WHERE …;

What’s the issue? Student (id, first_name, last_name, city, state) Enrollment (student_id, crn) Course (crn, dept_code, number, title) DELETE FROM Student WHERE id = “S123456789”;

Modifying & deleting data affects other tables Student (id, first_name, last_name, city, state) Enrollment (student_id, crn) Course (crn, dept_code, number, title) Need to maintain referential integrity! By default, UPDATE & DELETE fail when they would break referential integrity. DELETE FROM Student WHERE id = “S123456789”;

Modifying & deleting data affects other tables Student (id, first_name, last_name, city, state) Enrollment (student_id, crn) Course (crn, dept_code, number, title) CREATE TABLE Enrollment ( … FOREIGN KEY student_id REFERENCES Student (id) ON UPDATE …, FOREIGN KEY crn REFERENCES Course (crn) ON DELETE … ); RESTRICT NO ACTION SET NULL SET DEFAULT CASCADE

Natural vs. synthetic keys Natural You can get a new SSN in cases of stolen identity. Account numbers can change when banks merge. Fits with ON UPDATE CASCADE. Synthetic Should never change. Fits with ON UPDATE RESTRICT / NO ACTION.

Hard vs. soft deletes Hard I want to delete the data, so dang it, delete it! Fits with ON DELETE CASCADE. Soft I might want this data later, after all. Delete = Flag this data as “inactive”. E.g., closed customer account or discontinued product. Fits with ON DELETE RESTRICT / NO ACTION.

Add, modify, delete from schema

Database schemas not static Needs change over time. Think about long-term plans & maintenance.

Add, modify, delete attribute ALTER TABLE Student ADD COLUMN zip INT; ALTER TABLE Student MODIFY COLUMN zip VARCHAR(9); ALTER TABLE Student ALTER COLUMN zip VARCHAR(9); ALTER TABLE Student DROP COLUMN state;

DANGER! Can change query semantics! Dangerous – avoid! Specify columns explicitly. SELECT * FROM … …;

Add, modify, delete constraints See previous set of notes.

Add, modify, delete views CREATE VIEW … AS SELECT …; ALTER VIEW … AS …; CREATE OR REPLACE VIEW … AS SELECT …; DROP VIEW …;

What’s the difference? CREATE VIEW StudentName AS SELECT first_name, last_name FROM Student; SELECT first_name, last_name INTO StudentName FROM Student;

Process for changing schemas

Traditional – Regular shutdowns Make & test changes on a test server. Have regularly scheduled system shutdown. Typically late at night to minimize user disruption. Disable user access to live server. Make & test changes on the live server. Takes time. Lots of data, possibly replicated & distributed.

Reducing downtime Instead of a full shutdown, just block users from DB temporarily. Shutdown only as long as needed. “Lock” the DB. Only need to lock tables that are changing. See COMP 322 & 421 for more about locks. For UX, assumes lock is only needed briefly.

Updating without any user disruption E.g.: Create “shadow table” incorporating changes Create triggers in original table that forwards data updates to shadow table Copy original table’s data to shadow table Rename shadow table to replace original table Can choose to only let some users see the changes. Some DBMS provide support for such “online” schema changes.