SQL-Data Definition 4/21/2019.

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.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
Introduction to Structured Query Language (SQL)
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Murali Mani SQL DDL and Oracle utilities. Murali Mani Datatypes in SQL INT (or) INTEGER FLOAT (or) REAL DECIMAL (n, m) CHAR (n) VARCHAR (n) DATE, TIME.
Introduction to Structured Query Language (SQL)
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial Password: UWPstudent Password is case sensitive.
Oracle Data Definition Language (DDL)
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: Schema Definition, Constraints, and Queries and Views.
SQL Data Definition (CB Chapter 6) CPSC 356 Database Ellen Walker Hiram College (Includes figures from Database Systems by Connolly & Begg, © Addison Wesley.
CS 3630 Database Design and Implementation. Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial.
Chapter 8 Part 1 SQL-99 Schema Definition, Constraints, Queries, and Views.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
M1G Introduction to Database Development 2. Creating a Database.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Fundamentals  SQL: Structured Query Language is a simple and powerful language used to create, access, and manipulate data and structure in the database.
1 SQL - II Data Constraints –Applying data constraints Types of data constraints –I/O constraints The PRIMARY KEY constraints The FOREIGN KEY constraints.
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
GLOBEX INFOTEK Copyright © 2013 Dr. Emelda Ntinglet-DavisSYSTEMS ANALYSIS AND DESIGN METHODSINTRODUCTORY SESSION EFFECTIVE DATABASE DESIGN for BEGINNERS.
Constraints Lesson 8. Skills Matrix Constraints Domain Integrity: A domain refers to a column in a table. Domain integrity includes data types, rules,
Session 1 Module 1: Introduction to Data Integrity
©Silberschatz, Korth and Sudarshan1 Structured Query Language (SQL) Data Definition Language Domains Integrity Constraints.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
Basic SQL*Plus edit and execute commands SQL*Plus buffer and built-in editor holds the last SQL statement Statements are created in free-flow style and.
8 Copyright © 2005, Oracle. All rights reserved. Managing Schema Objects.
Constraints Advanced Database Systems Dr. AlaaEddin Almabhouh.
CDT/1 Creating data tables and Referential Integrity Objective –To learn about the data constraints supported by SQL2 –To be able to relate tables together.
In this session, you will learn to: Manage databases Manage tables Objectives.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
More SQL: Complex Queries, Triggers, Views, and Schema Modification
Fundamentals of DBMS Notes-1.
Chapter 10 SQL DDL.
Fundamental of Database Systems
Database Definition and FMS Tool Management Case Study
COP Introduction to Database Structures
TABLES AND INDEXES Ashima Wadhwa.
Managing Tables, Data Integrity, Constraints by Adrienne Watt
SQL: Schema Definition and Constraints Chapter 6 week 6
Insert, Update and the rest…
CS 3630 Database Design and Implementation
Designing Tables for a Database System
ORACLE SQL Developer & SQLPLUS Statements
STRUCTURED QUERY LANGUAGE
CS4222 Principles of Database System
SQL OVERVIEW DEFINING A SCHEMA
Databases and Information Management
Oracle Data Definition Language (DDL)
SQL-1 Week 8-9.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Contents Preface I Introduction Lesson Objectives I-2
Relational Database Design
CS3220 Web and Internet Programming SQL and MySQL
Database Design: Relational Model
Chapter # 7 Introduction to Structured Query Language (SQL) Part I.
Domains 4/17/2019 See scm-intranet.
IST 318 Database Administration
CS3220 Web and Internet Programming SQL and MySQL
Integrity 5/5/2019 See scm-intranet.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
CMSC-461 Database Management Systems
Triggers 7/11/2019 See scm-intranet.
Lecture 05: SQL Wednesday, October 9, 2002.
Including Constraints
SQL (Structured Query Language)
Presentation transcript:

SQL-Data Definition 4/21/2019

Objectives To understand the idea of  SQL schemas and  data independence. To develop a means of expressing schemas. 4/21/2019

Topics of Discussion SQL can be used to express database schemas in a standard machine readable format. It is primarily intended for relational database systems This session looks at how Logical schemas are constructed using SQL 4/21/2019

Creating SQL Schemas In a relational database entities are created as tables with specific properties (relations). Attributes are columns of the tables. Data definition in SQL is standardised. These standards are not implemented exactly in each database product. 4/21/2019

Scripts Typically these schemas are stored as scripts either in .SQL files (text) or in the DBMS environment (data dictionary). The script is executed and the persistent tables come into existence. 4/21/2019

Example SQL-92 Create table magazine (machine_id varchar(5), max_power float4 not null, controller varchar(10), Magazine_id varchar(5), constraint  magazinekey primary key(magazine_id)); create table mag_tool (magazine_no varchar(5), tool_id varchar(5) , constraint mag_toolkey primary key(magazine_no, tool_id), Constraint holds foreign key (magazine_no) references magazine(magazine_id));   There is no requirement that primary key-foreign key columns have the same name-just the same domain. Magazine Machine_id Max_power controller Magazine_id Magazine_no Tool_id Mag_tool 1..1 holds 0..* 4/21/2019

a primary key constraint Constraints e.g. a primary key constraint create table employee (name char(20), department varchar(20), NI_number integer, constraint emp_key primary key (NI_number)); 4/21/2019

A Foreign Key Constraint Create table pays (hours_worked integer, pay_rate integer, NI_no integer, Week_no integer, Constraint pays_key primary key (NI_no,week_no), constraint NI_number_pays foreign key (NI_no) references employee(NI_number)); There is no requirement that primary and foreign keys have the same name. Referential Integrity is automatically maintained.   employee name department NI_number hours_worked pay_rate NI_no Week_no pays 1..1 NI_number_pays 0..* 4/21/2019

(prevents duplicate values in non primary key columns) A Unique Constraint (prevents duplicate values in non primary key columns) Constraint manager_unique unique (Manager_number) Alternate keys would use UNIQUE clause.   4/21/2019

A Check Constraint Limits the set of valid values for a column. It is possible to have multiple check constraints for a column. create table employee (name char(20), department varchar(20), NI_number integer, constraint emp_key primary key (NI_number), constraint valid_dept check (department in ('Sales', 'Field Service', 'Logistics', 'MIS'))); N.B. It is not possible to use SQL sub-queries in Check constraints in SQL*Server. 4/21/2019

Mask constraint valid_product check (product_id like 'ab%'); where valid_product is a name for the constraint and product_id is a column name. % matches any character string so a valid string is 'ab' followed by any character. constraint valid_product check ((product_id like 'ab%' ) or (product_id like 'zz%')); % matches any character string so valid string is 'ab' followed by any character OR 'zz' followed by any valid string. _ matches any single character. 4/21/2019

Ranges constraint customer_numbers check (customer_number between ‘1111’ and ’9999’) 4/21/2019

Default Constraint Replaces a missing value on insert create table employee (name char(20), department varchar(20), NI_number integer, constraint emp_key primary key (NI_number), default department_default 'Sales' for department); Column must not have not null constraint System functions can be used with default to provide system information e.g. current user, date, time etc.. See Transact*SQL Help 'niladic functions'. 4/21/2019

Alter Table Used to change pre-existing tables.e.g. alter table movie_title add column total_sales_revenue decimal(9,2); alter table movie_title alter column total_sales_revenue decimal(10,2); alter table movie_title drop column total_sales_revenue; alter table movie_title add constraint pkey foreign key (lead_actor) references lead_actor_table; 4/21/2019

Outcome At the end of the process of creating an SQL schema a script should exist This will be a text file or a component in the data dictionary It is portable between database systems, so the database structure can be moved from system to system 4/21/2019

Summary The database structure is independent of database system, operating system and hardware It can be implemented on a client or a server machine The database structure can be changed easily 4/21/2019

Steps Take a complete data model Create a text file containing all the SQL commands which form the schema. One table per entity Store the file (script) with subscript .SQL Load and execute the commands (script) using the DBMS SQL tool (QueryAnalyser or Access2000) 4/21/2019