More SQL Data Definition

Slides:



Advertisements
Similar presentations
Structured Query Language (SQL)
Advertisements

Dr. Alexandra I. Cristea CS 252: Fundamentals of Relational Databases: SQL5.
9 Creating and Managing Tables. Objectives After completing this lesson, you should be able to do the following: Describe the main database objects Create.
Data Definition Language (DDL)
Creating Tables. 2 home back first prev next last What Will I Learn? List and provide an example of each of the number, character, and date data types.
Fundamentals of Database Systems Fourth Edition El Masri & Navathe
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Introduction to Structured Query Language (SQL)
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.
SQL Data Definition II Stanislava Armstrong 1SQL Data Definition II.
SQL Keys and Constraints Justin Maksim. Key Declaration Key constraint defined within the CREATE TABLE command Key can be declared using either the PRIMARY.
Introduction to Structured Query Language (SQL)
Using SQL Queries to Insert, Update, Delete, and View Data © Abdou Illia MIS Spring 2015 Wednesday 1/28/2015 Chapter 3A.
Database Systems Lecture 5 Natasha Alechina
Introduction To Databases IDIA 618 Fall 2014 Bridget M. Blodgett.
Oracle Data Definition Language (DDL)
SQL's Data Definition Language (DDL) – View, Sequence, Index.
Module 9: Managing Schema Objects. Overview Naming guidelines for identifiers in schema object definitions Storage and structure of schema objects Implementing.
More SQL Select Database Systems Lecture 8 Natasha Alechina.
Structured Query Language (SQL) A2 Teacher Up skilling LECTURE 2.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
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.
The Relational Model These slides are based on the slides of your text book.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor Ms. Arwa.
SQL Unit 11 Inserting, Updating, and Deleting Data; Changing the Database Design Kirk Scott 1.
Chapter 6 Additional Database Objects
Missing Information Database Systems Lecture 10 Natasha Alechina.
Constraints  Constraints are used to enforce rules at table level.  Constraints prevent the deletion of a table if there is dependencies.  The following.
Physical DB Issues, Indexes, Query Optimisation Database Systems Lecture 13 Natasha Alechina.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Other database objects (Sequence). What Is a Sequence? A sequence: Automatically generates sequential numbers Is a sharable object Is typically used to.
Chapter 6 Additional Database Objects Oracle 10g: SQL.
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
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.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
BIS Database Systems School of Management, Business Information Systems, Assumption University A.Thanop Somprasong Chapter # 7 Introduction to Structured.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Oracle 11g: SQL Chapter 4 Constraints.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Module Coordinator Tan Szu Tak School of Information and Communication Technology, Politeknik Brunei Semester
Visual Programing SQL Overview Section 1.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
Chapter 12 Additional Database Objects. Chapter Objectives  Define the purpose of a sequence and state how it can be used by an organization  Explain.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
A Guide to SQL, Eighth Edition Chapter Six Updating Data.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
Chapter 12Introduction to Oracle9i: SQL1 Chapter 12 Additional Database Objects.
CSC314 DAY 8 Introduction to SQL 1. Chapter 6 © 2013 Pearson Education, Inc. Publishing as Prentice Hall SQL OVERVIEW  Structured Query Language  The.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
 CONACT UC:  Magnific training   
Physical Model Lecture 11. Physical Data Model The last step is the physical design phase, In this phase data is – Store – Organized and – Access.
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.
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.
CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
LEC-8 SQL. Indexes The CREATE INDEX statement is used to create indexes in tables. Indexes allow the database application to find data fast; without reading.
Chapter 5 Introduction to SQL.
Insert, Update and the rest…
Oracle Data Definition Language (DDL)
Instructor: Samia arshad
Presentation transcript:

More SQL Data Definition Database Systems Lecture 6 Natasha Alechina

More SQL Data Definition In this Lecture More SQL DROP TABLE ALTER TABLE INSERT, UPDATE, and DELETE Data dictionary Sequences For more information Connolly and Begg chapters 5 and 6 More SQL Data Definition

More SQL Data Definition Creating Tables From last lecture… CREATE TABLE Columns Data types [NOT] NULL, DEFAULT values Constraints Primary keys Unique columns Foreign keys CREATE TABLE <name> ( <col-def-1>, <col-def-2>, : <col-def-n>, <constraint-1>, <constraint-k>) More SQL Data Definition

More SQL Data Definition Deleting Tables To delete a table use DROP TABLE [IF EXISTS] <name> Example: DROP TABLE Module BE CAREFUL with any SQL statement with DROP in it You will delete any information in the table as well You won’t normally be asked to confirm There is no easy way to undo the changes More SQL Data Definition

More SQL Data Definition Changing Tables Sometimes you want to change the structure of an existing table One way is to DROP it then rebuild it This is dangerous, so there is the ALTER TABLE command instead ALTER TABLE can Add a new column Remove an existing column Add a new constraint Remove an existing constraint More SQL Data Definition

More SQL Data Definition ALTERing Columns Examples ALTER TABLE Student ADD COLUMN Degree VARCHAR(50) DROP COLUMN Degree To add or remove columns use ALTER TABLE <table> ADD COLUMN <col> DROP COLUMN <name> More SQL Data Definition

More SQL Data Definition ALTERing Constraints Examples ALTER TABLE Module ADD CONSTRAINT ck UNIQUE (title) DROP CONSTRAINT ck To add or remove columns use ALTER TABLE <table> ADD CONSTRAINT <definition> DROP CONSTRAINT <name> More SQL Data Definition

More SQL Data Definition INSERT, UPDATE, DELETE INSERT - add a row to a table UPDATE - change row(s) in a table DELETE - remove row(s) from a table UPDATE and DELETE use ‘WHERE clauses’ to specify which rows to change or remove BE CAREFUL with these - an incorrect WHERE clause can destroy lots of data More SQL Data Definition

More SQL Data Definition INSERT INSERT INTO <table> (col1, col2, …) VALUES (val1, val2, …) The number of columns and values must be the same If you are adding a value to every column, you don’t have to list them SQL doesn’t require that all rows are different (unless a constraint says so) More SQL Data Definition

More SQL Data Definition INSERT Student ID 1 2 Name John Mary Year 3 INSERT INTO Student (ID, Name, Year) VALUES (2, ‘Mary’, 3) Student ID 1 2 Name John Mary Year Student ID 1 Name John Year INSERT INTO Student (Name, ID) VALUES (‘Mary’, 2) Student ID 1 2 Name John Mary Year 3 INSERT INTO Student VALUES (2, ‘Mary’, 3) More SQL Data Definition

More SQL Data Definition UPDATE UPDATE <table> SET col1 = val1 [,col2 = val2…] [WHERE <condition>] All rows where the condition is true have the columns set to the given values If no condition is given all rows are changed so BE CAREFUL Values are constants or can be computed from columns More SQL Data Definition

More SQL Data Definition UPDATE Student ID 1 2 3 4 Name John Mark Anne Jane Year UPDATE Student SET Year = 1, Name = ‘Jane’ WHERE ID = 4 Student ID 1 2 3 4 Name John Mark Anne Mary Year Student ID 1 2 3 4 Name John Mark Anne Mary Year UPDATE Student SET Year = Year + 1 More SQL Data Definition

More SQL Data Definition DELETE Removes all rows which satisfy the condition DELETE FROM <table> [WHERE <condition>] If no condition is given then ALL rows are deleted - BE CAREFUL Some versions of SQL also have TRUNCATE TABLE <T> which is like DELETE FROM <T> but it is quicker as it doesn’t record its actions More SQL Data Definition

More SQL Data Definition DELETE Student ID 1 2 Name John Mark Year 1 3 DELETE FROM Student WHERE Year = 2 Student ID 1 2 3 4 Name John Mark Anne Mary Year Student DELETE FROM Student or TRUNCATE TABLE Student ID Name Year More SQL Data Definition

More SQL Data Definition SELECT The SQL command you will use most often Queries a set of tables and returns results as a table Lots of options, we will look at many of them Usually more than one way to do any given query SQL’s SELECT is different from the relational algebra’s selection  SELECT in SQL does all of the relational algebra But it is a bit different because SQL differs from the relational model More SQL Data Definition

More SQL Data Definition SQL SELECT Overview SELECT [DISTINCT | ALL] <column-list> FROM <table-names> [WHERE <condition>] [ORDER BY <column-list>] [GROUP BY <column-list>] [HAVING <condition>] ([]- optional, | - or) More SQL Data Definition

More SQL Data Definition Simple SELECT SELECT <columns> FROM <table> <columns> can be A single column A comma-separated list of columns * for ‘all columns’ Given a table Student with columns stuID stuName stuAddress stuYear More SQL Data Definition

More SQL Data Definition Sample SELECTs SELECT * FROM Student stuID stuName stuAddress stuYear 1 Anderson 15 High St 2 Brooks 27 Queen’s Rd 3 Chen Lenton Hall 4 D’Angelo Derby Hall 5 Evans 6 Franklin 13 Elm St 7 Gandhi 8 Harrison More SQL Data Definition

More SQL Data Definition Sample SELECTs SELECT stuName FROM Student stuName Anderson Brooks Chen D’Angelo Evans Franklin Gandhi Harrison More SQL Data Definition

More SQL Data Definition Sample SELECTs SELECT stuName, stuAddress FROM Student stuName stuAddress Anderson 15 High St Brooks 27 Queen’s Rd Chen Lenton Hall D’Angelo Derby Hall Evans Franklin 13 Elm St Gandhi Harrison More SQL Data Definition

More SQL Data Definition Being Careful Before running DELETE FROM Student WHERE Year = 3 run SELECT * FROM Student When using DELETE and UPDATE You need to be careful to have the right WHERE clause You can check it by running a SELECT statement with the same WHERE clause first More SQL Data Definition

More SQL Data Definition Sequences Often we want to assign each row a unique number These are useful as primary keys Using integers to reference rows is more efficient We would like the DBMS to do this In most versions of SQL we can use autoincrementing fields to do this Details differ between versions Usually the first entry is assigned 1, the next 2, and so on, but Oracle lets you change this More SQL Data Definition

More SQL Data Definition Sequences In Oracle we use a Sequence A sequence is a source of numbers We can declare several sequences, giving each a name, a start point, and a step size We can then generate unique numbers by asking for the next element from a sequence More SQL Data Definition

More SQL Data Definition Sequences in Oracle To declare a sequence: CREATE SEQUENCE <name> [START WITH <value>] [INCREMENT BY <value>] If no START WITH or INCREMENT BY values are given they default to 1 To get the next value from a sequence <sequence name>.nextVal More SQL Data Definition

More SQL Data Definition Sequence Example Creating a sequence CREATE SEQUENCE mySeq START WITH 1 Using a sequence SELECT mySeq.nextVal FROM DUAL; INSERT INTO Student (stuID, stuName, stuAddress) VALUES (mySeq.nextVal, 'Steve Mills', '13 Elm Street') More SQL Data Definition

SQL and the Data Dictionary The data dictionary or catalogue stores Information about database tables Information about the columns of tables Other information - users, locks, indexes, and more This is ‘metadata’ Some DBMSs let you query the catalogue In Oracle you can access the metadata in several ways There are ‘system tables’ with metadata in them You can also DESCRIBE tables More SQL Data Definition

Oracle Data Dictionary To find out what tables and sequences you have defined use SELECT table_name FROM user_tables The user_tables table is maintained by Oracle It has lots of columns, so don’t use SELECT * FROM user_tables More SQL Data Definition

Oracle Data Dictionary To find the details of a table use DESCRIBE <table name> Example: SQL> DESCRIBE Student; Name Null? Type ------------ -------- ---------- STUID NOT NULL NUMBER(38) STUNAME NOT NULL VARCHAR2(50) STUADDRESS VARCHAR2(50) STUYEAR NUMBER(38) More SQL Data Definition

More SQL Data Definition This Lecture in Exams Track cID Num Title Time aID 1 Violent 239 1 1 2 Every Girl 410 1 3 Breather 217 1 1 4 Part of Me 279 1 1 Star 362 1 2 2 Teaboy 417 2 CD cID Title Price Mix 9.99 Compilation 12.99 Artist aID Name Stellar Cloudboy More SQL Data Definition

More SQL Data Definition This Lecture in Exams Add £2.50 to the price of all CDs that cost more than £10.00. (2 marks) Add a new column, Genre, to the CD table. This column should hold a string of up to 100 characters, and if no genre is provided then it should default to the value “Unknown”. (3 marks) Add a track titled “Runnin” by the artist “Fat Freddy’s Drop” which is 12 minutes and 27 second long to the CD titled “Compilation”. For this part only, you may assume that the tables contain exactly the information shown above. More SQL Data Definition

More SQL Data Definition Next Lecture SQL SELECT WHERE clauses SELECT from multiple tables JOINs For more information Connolly and Begg Chapter 5 Ullman and Widom, Chapter 6.5,6.1. More SQL Data Definition