SQL constrains and keys. SORTED RESULTS Sort the results by a specified criterion SELECT columns FROM tables WHERE predicates ORDER BY column ASC/DESC;

Slides:



Advertisements
Similar presentations
Complex Integrity Constraints in SQL. Constraints over a Single Table Table Constraint: Create TABLE Sailors (sid INTEGER, sname CHAR(10), rating INTEGER,
Advertisements

The Relational Model Much of the material presented in these slides was developed by Dr. Ramon Lawrence at the University of Iowa.
Relational Database Design UNIT II 1. 2 Advantages of Using Database Systems Centralized control of a firm’s data Redundancy can be reduced (avoid keeping.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
The Relational Model. Introduction Introduced by Ted Codd at IBM Research in 1970 The relational model represents data in the form of table. Main concept.
OUTLINE OF THE LECTURE PART I GOAL: Understand the Data Definition Statements in Fig 4.1 Step1: Columns of the Tables and Data types. Step2: Single column.
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)
Relations The Relational Data Model John Sieg, UMass Lowell.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
The Relational Model Lecture 3 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) From ER to Relational.
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.
Relational Database Management System A type of database in which records are stored in relational form is called relational database management system.
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
SQL: Constraints and Triggers Chapter 6 Ullman and Widom Certain properties we’d like our database to hold Modification of the database may break these.
Database Architecture The Relational Database Model.
Database Systems Lecture 5 Natasha Alechina
Database Lecture # 1 By Ubaid Ullah.
Relational Model & Relational Algebra. 2 Relational Model u Terminology of relational model. u How tables are used to represent data. u Connection between.
© Pearson Education Limited, Chapter 2 The Relational Model Transparencies.
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.
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
RAJIKA TANDON DATABASES CSE 781 – Database Management Systems Instructor: Dr. A. Goel.
1 Translation of ER-diagram into Relational Schema Prof. Sin-Min Lee Department of Computer Science.
Relational Database Management Systems. A set of programs to manage one or more databases Provides means for: Accessing the data Inserting, updating and.
FEN  Concepts and terminology  Operations (relational algebra)  Integrity constraints The relational model.
1 The Relational Model Instructor: Mohamed Eltabakh
CG084&085 / / 1 The Relational Data Model Properties of Relations Keys and Constraints.
1 CSE 480: Database Systems Lecture 5: Relational Data Model.
DAY 12: DATABASE CONCEPT Tazin Afrin September 26,
Slide 1 Chapter 7 – Part 1 Data Definition Language & Data Manipulation Language.
ATADESAB. BATLE CORDER DLEIF Lesson objectives In this lesson you will learn some basic database terms and learn how a database is created.
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.
Relational Data Model Ch. 7.1 – 7.3 John Ortiz Lecture 3Relational Data Model2 Why Study Relational Model?  Most widely used model.  Vendors: IBM,
M1G Introduction to Database Development 2. Creating a Database.
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.
FEN Introduction to the database field:  The Relational Model Seminar: Introduction to relational databases.
CS146 References: ORACLE 9i PROGRAMMING A Primer Rajshekhar Sunderraman
Constraints, Triggers and Views COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
The University of Akron Dept of Business Technology Computer Information Systems The Relational Model: Concepts 2440: 180 Database Concepts Instructor:
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
7 1 Constraints & Triggers Chapter Constraints and triggers? Constraints: Certain properties that the DBMS is required to enforce –E.g. primary.
Visual Programing SQL Overview Section 1.
Understand Primary, Foreign, and Composite Keys Database Administration Fundamentals LESSON 4.2.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Oracle Developer. Create Table SQL> create table catalog ( 2 cno varchar2(7), 3 ctitle varchar2(50), 4 primary key (cno)); Table created. SQL> describe.
VOCAB REVIEW. A field that can be computed from other fields Calculated field Click for the answer Next Question.
Chapter 3 The Relational Model. Why Study the Relational Model? Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. “Legacy.
1 Designing Tables for a Database System. 2 Where we were, and where we’re going The Entity-Relationship model: Used to model the world The Relational.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
SQL CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
IT 5433 LM3 Relational Data Model. Learning Objectives: List the 5 properties of relations List the properties of a candidate key, primary key and foreign.
1 Constraints and Triggers in SQL. 2 Constraints are conditions that must hold on all valid relation instances SQL2 provides a variety of techniques for.
Standards and Conventions
Database Constraints Ashima Wadhwa.
Chapter 2: Relational Model
Integrity Constraints
Lecturer: Mukhtar Mohamed Ali “Hakaale”
Rob Gleasure robgleasure.com
Database Design: Relational Model
Instructor: Mohamed Eltabakh
Instructor: Samia arshad
SQL NOT NULL Constraint
SQL AUTO INCREMENT Field
Presentation transcript:

SQL constrains and keys

SORTED RESULTS Sort the results by a specified criterion SELECT columns FROM tables WHERE predicates ORDER BY column ASC/DESC; select fname, sid from students order by fname ASC;

Constraints not null validate conditions for fields Unique create table students ( sid char(5) not null unique, fname varchar(20), lname varchar(20) not null, minit char, age int(3) check (age>0), fatherage int(3) check (fatherage>0), check (fatherage>age)) ;

Exercise Try to insert at least two different records that would violate at least one constraint.

Primary keys a primary key is a value that can be used to identify a particular row in a table. A super key for a relation scheme R is any subset k of R that satisfies the property that in any valid relation under the scheme R it is not possible to have two different tuples with the same values under k. A candidate key for R is any super key for R such as none of its proper subsets is also a super key A primary key is one of the candidate keys chosen by the designer of the database

Primary key examples What would happen if more than one bank account would have the same number ? Solution : unique ids ? How to enforce: primary keys Why not only “unique” : –primary keys are automatically indexed by the dbms –primary keys are not null –if a column is unique than more than one null row with data can exist create table students ( sid varchar(5), fname varchar(20), lname varchar(20) not null, minit char, primary key (sid));

The primary key may be a tuple create table enrolls ( sid varchar(5), term varchar(10), lineno int(4), primary key (sid,term,lineno));

Foreign keys A foreign key is a field or group of fields in a database record that point to a key field or group of fields forming a key of another database record in some (usually different) table. Usually a foreign key in one table refers to the primary key of another table create table enrolls ( sid varchar(5) not null, term varchar(10), lineno int(4), primary key (sid,term,lineno), foreign key (sid) references students);