SQL – More Table Constraints

Slides:



Advertisements
Similar presentations
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Advertisements

Chapter Eight: Database Redesign Database Processing: Fundamentals, Design, and Implementation.
SQL Data Definition II Stanislava Armstrong 1SQL Data Definition II.
My CD Database THE BEST Shoemaker, Ray, Gleisberg.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
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.
1 IT420: Database Management and Organization SQL: Structured Query Language 25 January 2006 Adina Crăiniceanu
The Relational Model These slides are based on the slides of your text book.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
1 The Relational Model. 2 Why Study the Relational Model? v Most widely used model. – Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. v “Legacy.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
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.
Recap of SQL Lab no 8 Advance Database Management System.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
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.
Introduction to Database System Adisak Intana Lecturer Chapter 7 : Data Integrity.
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.
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.
Constraints Lesson 8. Skills Matrix Constraints Domain Integrity: A domain refers to a column in a table. Domain integrity includes data types, rules,
Including Constraints. What Are Constraints? Constraints enforce rules at the table level. You can use constraints to do the following: – Enforce rules.
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,
CpSc 3220 The Language of SQL Chapter 17 Modifying Data.
Constraints Advanced Database Systems Dr. AlaaEddin Almabhouh.
MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
Database Constraints ICT 011. Database Constraints Database constraints are restrictions on the contents of the database or on database operations Database.
Getting started with Accurately Storing Data
Fundamental of Database Systems
Database Access with SQL
SQL – Python and Databases
Tables and Triggers.
The Basics of Data Manipulation
Insert, Update and the rest…
SQL – CRUD.
© 2016, Mike Murach & Associates, Inc.
and Defining Table Relationships
Applied CyberInfrastructure Concepts Fall 2017
Do it now – PAGE 13 You will find your do it now task in your workbook – look for the start button! Thursday, 20 September 2018.
SQL – Column constraints
Module 5: Implementing Data Integrity by Using Constraints
Referential Integrity
Lecturer: Mukhtar Mohamed Ali “Hakaale”
The Relational Model Relational Data Model
Constraints & Triggers
The Basics of Data Manipulation
What is a Database? A collection of data organized in a manner that allows access, retrieval, and use of that data.
Referential Integrity
Structured Query Language
DATABASE SQL= Structure Query Language مبادئ قواعد بيانات
SQL DATA CONSTRAINTS.
CS122 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
Database Processing: David M. Kroenke’s Chapter Seven:
Database Management System
Data Definition Language
CS122 Using Relational Databases and SQL
CS1222 Using Relational Databases and SQL
CS122 Using Relational Databases and SQL
SQL – Constraints & Triggers
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:

SQL – More Table Constraints

tracks.artist_id is a foreign key referencing artists.id name artist_id 1 Let it be 2 Penny Lane 3 Yellow Submarine 4 Hedwig's Theme 5 Jingle Bell Rock NULL 6 The Devil Is A Patient Man artists id name 1 The Beatles 2 John Williams 3 CRUD

Foreign Keys We discussed primary keys, which are columns that uniquely identify each row. However, often our tables will have columns that are meant to match up with columns in a different table. We want to add a constraint on those columns that there must be an associated row in a foreign (other) table. NULLs are okay

CREATE TABLE artists (id INTEGER, name TEXT); CREATE TABLE tracks ( id INTEGER, name TEXT, artist_id INTEGER, FOREIGN KEY (artist_id) REFERENCES artists(id) );

Foreign Key efficiency With a foreign key, it is an error to change the database in a way which makes a row not match with a foreign row. This means insert, update, and delete statements must all be checked. This adds a heavy cost to changing the database. SQLite by default doesn't check foreign keys for correctness. You need to turn on this functionality with: PRAGMA foreign_keys = ON;

PRAGMA foreign_keys = ON; INSERT INTO artists (id, name) VALUES (1, 'Beatles'); INSERT INTO tracks (id, name, artist_id) VALUES (1, 'Jingle Bell Rock', NULL), -- OKAY (2, 'Let it be', 1), -- Okay (3, 'Jurassic Park', 2); -- ERROR: no matching key

CHECK The last constraint in CREATE TABLE is the most versatile, "CHECK". It specifies what legal values can be inserted/updated into a table. It can be added to a column or after the columns, makes no difference.

CREATE TABLE students ( name TEXT, age INTEGER CHECK (age > 18), CHECK (name != '' AND age < 1000) ); INSERT INTO students VALUES ('Josh', 27), -- OKAY ('', 30), -- ERROR ('Tyler', 4); -- ERROR UPDATE students SET age = 6001; -- ERROR

Foreign keys and checks CREATE TABLE tracks ( ... artist_id INTEGER, FOREIGN KEY (artist_id) REFERENCES artists(id) -- is the same as CHECK ( artist_id IS NULL OR EXISTS ( SELECT 1 FROM artists WHERE tracks.artist_id = artists.id ) ) );

When should you use a foreign key constraint? When you need to enforce matching values When you refer to values in a different table Never When you are given the key to the city