Populating and Querying tables Insert, Update, Delete and View (DML)

Slides:



Advertisements
Similar presentations
SQL’s Data Definition Language (DDL) n DDL statements define, modify and remove objects from data dictionary tables maintained by the DBMS n Whenever you.
Advertisements

Let’s try Oracle. Accessing Oracle The Oracle system, like the SQL Server system, is client / server. For SQL Server, –the client is the Query Analyser.
Building a database. Implementation of desirable features Integrity –A field’s validation can be declared when the field is declared. If this validation.
Maintaining Referential Integrity Pertemuan 2 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
Introduction to Structured Query Language (SQL)
SQL components In Oracle. SQL in Oracle SQL is made up of 4 components: –DDL Data Definition Language CREATE, ALTER, DROP, TRUNCATE. Creates / Alters.
Populating and Querying tables Insert and mostly View (DML)
Using SQL to create tables Ways of using Databases.
Overview Relational Databases and SQL Pertemuan 1 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
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)
SQL DDL constraints Restrictions on the columns and tables 1SQL DDL Constraints.
Week 2 Lecture 2 Structure of a database. External Schema Conceptual Schema Internal Schema Physical Schema.
Oracle Data Definition Language (DDL)
Introduction to DBMS and SQL Introduction to DBMS and SQL GUIDED BY : MR. YOGESH SAROJ (PGT-CS) MR. YOGESH SAROJ (PGT-CS) Presented By : JAYA XII –COM.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Database Design lecture 3_1 1 Database Design Lecture 3_1 Data definition in SQL.
SQL data definition using Oracle1 SQL Data Definition using Oracle.
Database Technical Session By: Prof. Adarsh Patel.
Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.
SQL data definition using Oracle1 SQL Data Definition using Oracle.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Chapter 5: Part 1: DDL STRUCTURED QUERY LANGUAGE (SQL)
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
SQL: DDL John Ortiz Cs.utsa.edu.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
© 2009 Pearson Education, Inc. Publishing as Prentice Hall 1 Chapter 7 (Part a): Introduction to SQL Modern Database Management 9 th Edition Jeffrey A.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
1 SQL - II Data Constraints –Applying data constraints Types of data constraints –I/O constraints The PRIMARY KEY constraints The FOREIGN KEY constraints.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
CREATE TABLE CREATE TABLE statement is used for creating relations Each column is described with three parts: column name, data type, and optional constraints.
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
7 1 Constraints & Triggers Chapter Constraints and triggers? Constraints: Certain properties that the DBMS is required to enforce –E.g. primary.
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
SQL introduction 2013.
Visual Programing SQL Overview Section 1.
IMS 4212: Data Manipulation 1 Dr. Lawrence West, MIS Dept., University of Central Florida Additional Data Manipulation Statements INSERT.
Lecture5: SQL Overview, Oracle Data Type, DDL and Constraints Ref. Chapter6 Lecture4 1.
IS 380 Introduction to SQL This lectures covers material from: database textbook chapter 3 Oracle chapter: 3,14,17.
Week 8-9 SQL-1. SQL Components: DDL, DCL, & DML SQL is a very large and powerful language, but every type of SQL statement falls within one of three main.
ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
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.
Starting with Oracle SQL Plus. Today in the lab… Connect to SQL Plus – your schema. Set up two tables. Find the tables in the catalog. Insert four rows.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
Physical Model Lecture 11. Physical Data Model The last step is the physical design phase, In this phase data is – Store – Organized and – Access.
Understand Data Definition Language (DDL) Database Administration Fundamentals LESSON 1.4.
Lec-7. The IN Operator The IN operator allows you to specify multiple values in a WHERE clause. SQL IN Syntax SELECT column_name(s) FROM table_name WHERE.
Fundamentals of DBMS Notes-1.
Rob Gleasure robgleasure.com
CS 3630 Database Design and Implementation
Chapter 5 Introduction to SQL.
Rob Gleasure robgleasure.com
Managing Tables, Data Integrity, Constraints by Adrienne Watt
SQL: Schema Definition and Constraints Chapter 6 week 6
STRUCTURED QUERY LANGUAGE
SQL data definition using Oracle
Oracle Data Definition Language (DDL)
SQL-1 Week 8-9.
Database Management System
Presentation transcript:

Populating and Querying tables Insert, Update, Delete and View (DML)

Inserting data into a table To add a row of data to a table INSERT INTO VALUES (value1, value2, …valueN) If the value list matches the column list exactly, there is no need to list the column names. If the value list does not match the column list exactly, there is a need to list the column names.

Example insert into Expert values ( ,'Dr. O''Meara','Orthopaedic'); Where the expert table was set up as:- CREATE TABLE Expert ( Expert_Id numeric(7,0), Expert_Name varchar(50), Expertise_area varchar(15), PRIMARY KEY (Expert_Id)) Note also, if you want to put an apostrophe in a string, put in two apostrophes.

Referential integrity The above tables have no foreign keys and a straightforward insert does not compromise referential integrity. If a table is constrained by foreign key values, then there is a need to see what is in the other table. Retrieving data from a database –Data can be retrieved from a table or set of tables using the SELECT statement

Revisiting Joe’s Yard…

Hierarchy of data structure

Layers of tables The tables Customer, Staff and Supplier have only primary keys. They are the foundation layer. Layer 1. The tables Docket, Stock and Supplier Order have foreign keys that only reference the foundation layer. They are Layer 2. Order line and Supplier order line depend on the tables in Layer 2. They are layer 3.

Remember the bricks…

Layer 1 The customer row is added, with key CustomerId, the Staff with key StaffNo and the Supplier with key SupplierId.

Layer 2 Docket Stock Supplier Order CustomerStaff Supplier The stock and the supplier Order depend on the Supplier, both having foreign key SupplierId. The Docket depends on BOTH Staff and Customer, having foreign keys CustomerPdSig, CustomerRcvdSig and CustomerId.

Layer 3 SupplierStaff Customer Docket Stock Supplier Order Both the supplier order line and the order line depend on the stock, having stockcode as a foreign key and part of their key. Order line depends on docket. Supplier order line depends on Supplier Order

The built database

Recap Look back at the blocks. –The table creates are the structure or the framework - i.e. the architect’s drawing –The inserts are like the bricks. You cannot insert into a table unless there is supporting data in the table on which it depends. Do –Creates starting with the one(s) with no dependents –Inserts starting with the one(s) with no dependents –Deletes starting with the one(s) on which no other depends –Drops starting with the one(s) on which no other depends

Revisiting database structure External Schema Conceptual Schema Internal Schema Physical Schema

The external schema –Level visible to user –Multiple views of the system e.g. View an order - see limited product and customer information –Only the database Administrator may access the whole database at this level Each external view is defined by means of an external schema –Provides definitions of each external view. –Written in a Data Definition Language. –individual to the user or user group. –accessed through a 3GL, a query language or a special purpose forms or menu-based language

SQL, DML and DDL Data manipulation and definition

SQL Standard Query Language is used widely to access databases. Implemented SQL (Oracle, MS SQL Server, Access) varies a little from the standard, but is reasonably standard It consists of a data definition language - DDL (Create, Alter, Drop) And a Data Manipulation Language - DML (Select, Union, Update, Delete, Insert Into, Select Into, Transform, Parameter)

DLL - Add a table CREATE TABLE table_name {(column_name data_type [NOT NULL] [UNIQUE][DEFAULT default_option] [CHECK (search_condition)][,…]} [PRIMARY KEY (list_of_columns),] {[UNIQUE (list_of_columns),][,…]} {[FOREIGN KEY {(list_of_foreign_key_columns) REFERENCES parent_table_name[(list_of_candidate_key_columns)], [MATCH {PARTIAL | FULL} {[CHECK (search_condition)][,…]}) [] optional, {} one or more, lower case – user supplied names, upper case reserved words, [|] either or, () are part of the syntax.

Basic create DDL - Create a table Create table Publisher (PubID VARCHAR(10) NOT NULL, PubName VARCHAR(100), PubPhone VARCHAR(20)) Create table Books (ISBN VARCHAR(13) NOT NULL, Title VARCHAR(100), Price Decimal(7,2), PubID VARCHAR(10))

With keys Create table Publisher (PubID VARCHAR(10) UNIQUE NOT NULL, PubName VARCHAR(100), PubPhone VARCHAR(20), primary key (PubID)) Create table Books (ISBN VARCHAR(13), Title VARCHAR(100), Price Decimal(7,2), PubID VARCHAR(10), PRIMARY KEY (ISBN), FOREIGN KEY (PubID) REFERENCES PUBLISHER)

DDL - Add a table Syntax is as follows:- CREATE TABLE TableName (ColumnDefinition,… [,Multi-ColumnConstraint,…]); Column definition is ColumnDefinition ::= ColumnName DataType[(size)][Single-ColumnConstraint]

Constraints

DDL - Single column constraint Single-ColumnConstraint:== CONSTRAINT IndexName [PRIMARY KEY | UNIQUE | REFERENCES ReferencedTable [(ReferencedColumn,…)] ] The ReferencedColumn is only necessary if the field being referenced is not the primary key of the referenced table

DDL - Multi-column constraints This constraint clause can appear after all column definitions: CONSTRAINT IndexName [PRIMARY KEY (ColumnName,…) | UNIQUE (ColumnName,…) | FOREIGN KEY (ReferencingColumn,…) REFERENCES ReferencedTable[(ReferencedColumn,…)] ]

DDL - Create a table Create table Publisher (PubID TEXT(10) CONSTRAINT PrimaryKeyName PRIMARY KEY, PubName TEXT(100), PubPhone TEXT(20)); Create table Books (ISBN TEXT(13) CONSTRAINT PrimaryKeyName PRIMARY KEY, Title TEXT(100), Price MONEY, PubID TEXT(10) CONSTRAINT FOREIGN KEY PubID REFERENCES PUBLISHER(PubID));

Constraints on a single column [ NULL | NOT NULL ] { PRIMARY KEY | UNIQUE } FOREIGN KEY ] REFERENCES ref_table [ ( ref_column ) –[ ON DELETE { CASCADE | NO ACTION } ] –[ ON UPDATE { CASCADE | NO ACTION } ] CHECK ( logical_expression )

Table constraints. ::= [ CONSTRAINT constraint_name ] – { [ { PRIMARY KEY | UNIQUE } { ( column [ ASC | DESC ] [,...n ] ) } [ WITH FILLFACTOR = fillfactor ] [ ON { filegroup | DEFAULT } ] ] –FOREIGN KEY [ ( column [,...n ] ) ] REFERENCES ref_table [ ( ref_column [,...n ] ) ] [ ON DELETE { CASCADE | NO ACTION } ] [ ON UPDATE { CASCADE | NO ACTION } ] [ NOT FOR REPLICATION ] | CHECK [ NOT FOR REPLICATION ] ( search_conditions )

Widely used constraints Primary key Foreign key Unique Check (logical expression) Not null – do we always want this?

DDL - ALTER or DROP TABLE Alter is used to –Add a new column to the table –Delete a column from the table Drop is used to delete a table

DDL - Syntax for Alter and Drop ALTER TABLE TableName ADD ColName ColType[(size)] [unique][not null] | DROP COLUMN ColName DROP TABLE TableName

Create examples This allows certain constraints to be placed on a column or attribute –E.g. CREATE TABLE CLASS (CLASS_name char(10) CHECK (class_name in ('wmt1','wmt2','wmt3')), class_tutor char(10) unique, not null, max_students integer CHECK max_students between 1 and 100 default 50, primary key (class_name))

Table Manipulation CREATE table … ALTER table … DROP table … These operations work on the TABLE This is equivalent to a file.

Alter /* Put in a field that allows the accident to be categorised as Trivial, Moderate, Serious or Disastrous */ alter table Accident add column accident_category char(1) check (accident_category in ('T','M','S','D'));

Row manipulation INSERT adds new rows of data to a table. UPDATE modifies existing data in a table. DELETE removes rows of data from a table.

UPDATE SET WHERE <condition; e.g. UPDATE Northwind.dbo.Products SET UnitPrice = UnitPrice * 1.1 WHERE CategoryID = 2

DELETE DELETE FROM WHERE Delete from BusDriver where Staff_No = ;

Exercise The following tables form part of a database held in a relational DBMS: –Hotel (Hotel_No, Name, Address) –Room (Room_No, Hotel_No, Type, Price) –Booking (Hotel_No,Guest_No, Date_from, Date_To, Room_No) –Guest (Guest_No, Name, Address) where –Hotel contains hotel details and Hotel_No is the primary key, –Room contains room details for each hotel and (Hotel_No, Room_No) forms the primary key, –Booking contains details of the bookings and the primary key comprises (Hotel_No, Guest_No and Date_From) –Guest contains guest details and Guest_No is the primary key. Create the database.