SQL: Overview and DDL Ch. 10.1 - 10.6 John Ortiz.

Slides:



Advertisements
Similar presentations
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Advertisements

Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL.
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.
The Relational Model Class 2 Book Chapter 3 Relational Data Model Relational Query Language (DDL + DML) Integrity Constraints (IC) (From ER to Relational)
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.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
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.
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.
Introduction to Structured Query Language (SQL)
A Guide to MySQL 3. 2 Objectives Start MySQL and learn how to use the MySQL Reference Manual Create a database Change (activate) a database Create tables.
Oracle SQL*plus John Ortiz. Lecture 10SQL: Overview2 Overview  SQL: Structured Query Language, pronounced S. Q. L. or sequel.  A standard language for.
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
Database Systems Lecture 5 Natasha Alechina
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)
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
The Relational Model These slides are based on the slides of your text book.
ASP.NET Programming with C# and SQL Server First Edition
The Relational Model. Review Why use a DBMS? OS provides RAM and disk.
Copyright © 2007 Ramez Elmasri and Shamkant B. Navathe Chapter 8 SQL-99: Schema Definition, Constraints, and Queries and Views.
Database Design lecture 3_1 1 Database Design Lecture 3_1 Data definition in SQL.
Database Technical Session By: Prof. Adarsh Patel.
Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.
CS 3630 Database Design and Implementation. Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial.
1 The Relational Model Instructor: Mohamed Eltabakh
Chapter 8 Part 1 SQL-99 Schema Definition, Constraints, Queries, and Views.
Oracle Data Definition Language (DDL) Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
1 Creating and Modifying Database Objects. 2 An Oracle database consists of multiple user accounts Each user account owns database objects Tables Views.
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,
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.
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.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
The Relational Model Content based on Chapter 3 Database Management Systems, (Third Edition), by Raghu Ramakrishnan and Johannes Gehrke. McGraw Hill, 2003.
Visual Programing SQL Overview Section 1.
IS 380 Introduction to SQL This lectures covers material from: database textbook chapter 3 Oracle chapter: 3,14,17.
1 Chapter 2: Creating and Modifying Database Objects.
CMPT 258 Database Systems The Relationship Model (Chapter 3)
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.
1 CS 430 Database Theory Winter 2005 Lecture 10: Introduction to SQL.
Altering Tables and Constraints Database Systems Objectives Add and modify columns. Add, enable, disable, or remove constraints. Drop a table. Remove.
CS34311 The Relational Model. cs34312 Why Relational Model? Currently the most widely used Vendors: Oracle, Microsoft, IBM Older models still used IBM’s.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
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.
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.
Physical Model Lecture 11. Physical Data Model The last step is the physical design phase, In this phase data is – Store – Organized and – Access.
Chapter 3 The Relational Model. Why Study the Relational Model? Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. “Legacy.
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.
CS 3630 Database Design and Implementation
SQL: Schema Definition and Constraints Chapter 6 week 6
SQL and SQL*Plus Interaction
ORACLE SQL Developer & SQLPLUS Statements
Instructor: Mohamed Eltabakh
The Relational Model Relational Data Model
Oracle Data Definition Language (DDL)
SQL-1 Week 8-9.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Chapter 2: Creating And Modifying Database Tables
Chapter # 7 Introduction to Structured Query Language (SQL) Part I.
Presentation transcript:

SQL: Overview and DDL Ch John Ortiz

Lecture 10SQL: Overview2 Overview  SQL: Structured Query Language, pronounced S. Q. L. or sequel.  A standard language for relational, object- relational databases: SQL-86 by ANSI, SQL1 by ISO (87), SQL-89, SQL2 (92), SQL Web site:  Implemented by all commercial R/OR DBMSs: Oracle, Sybase, Ingres, Informix, DB2, SQL Server, … (with various degree of conformity)  We focus on SQL2 & SQL-1999 features and also Oracle SQL.

Lecture 10SQL: Overview3 Components of SQL  Have statements for  Schema definition & update  tables, indexes, views, …  Data Manipulation  query, insertion, deletion, update, …  Data integrity  constraints, …  System administration  users, data security, performance tuning, …  Application development  procedures, transaction, embedded SQL, …  …

Lecture 10SQL: Overview4 SQL: A Simple Example  Create a Students table create table Students ( SSN char(9) not null, Name varchar2(20), GPA number(3,2), Age number(2), primary key (SSN));  Insert a Student tuple insert into Students values ( , ‘John Smith’, 3.25, 29);

Lecture 10SQL: Overview5 SQL: A Simple Example (cont.)  Retrieve names of students whose GPA is higher than 3.0 and Age is less than 30. select Name from Students where GPA > 3.0 and Age < 30;  What data types are supported?  What constraints can be expressed? How?  What if I want to change the schema?  How to change data?  How to express more complex queries?

Lecture 10SQL: Overview6 Outline  Oracle SQL interface: sqlplus  SQL DDL  Interactive SQL queries, update, view  PL/SQL for procedures, constraints, triggers  Embedded SQL (Oracle Pro*C/C++, JDBC)  Oracle SQL  Standard SQL  Oracle manual: Oracle SQL Reference Manual (on-line).

Lecture 10SQL: Overview7 Oracle DBMS  A major commercial DBMS from Oracle (Object-Relational), supporting SQL.  Components: Oracle server, SQL*Plus, Precompilers (Pro*C/C++, …), JDBC driver, SQLJ, WebDB, Forms, Report, …  Platforms: Client-server, PC, Unix or Windows, Distributed, Parallel, …  Multiuser, Security, Transactions, Recovery,  We use it for course project and homework assignments.

Lecture 10SQL: Overview8 Oracle DB User Account  Needed for every DB user. Has separate user id & password.  Access through CSLan UNIX accounts.  Environment variables for Oracle access:  Oracle_BASE, Oracle_HOME, PATH, LD_PATH, CLASS_PATH, …  On-line Instructions for using Oracle.  “Using Oracle in CSLab” on course web page.  On-line Oracle manuals (URL is given in the previous article).

Lecture 10SQL: Overview9 Oracle SQL*Plus  Interactive user environment.  Runs SQL statements, PL/SQL statements, and other SQL*Plus commands  Allow simple command editing  Documentation:  On-line “help” command within sqlplus  On-line manual.  User login: Unix$sqlplus SQL> exit

Lecture 10SQL: Overview10 Use SQL*Plus to Run SQL  SQL*plus understands only Oracle SQL.  An SQL statement must end with semicolon ; a slash / on a line by itself, or a blank line.  An SQL statement can be typed in, or loaded from a.sql file (get filename).  An SQL can be executed from the command buffer (run), or from a.sql file  SQL statement in buffer can be written to a.sql file (save filename)

Lecture 10SQL: Overview11 Common SQL*Plus Commands  Display schema: describe table_name  Run Unix commands: !command  Run editor on.sql file: edit filename  Set host editor: define_editor=vi  Save a journal file: spool filename spool off  Customize environment: set option  Options: echo on, pause on, pagesize 30, …  View current settings: show all  Configuration file: login.sql

Lecture 10SQL: Overview12 SQL*Plus Command Editing  Show command in buffer: list  A set of simple line editing commands.  Better use external editor, say, vi or emacs.

Lecture 10SQL: Overview13 SQL Data Definition Language  Used by DBA or Designer to specify schema  A set of statements used to define and to change the definition of tables, columns, data types, constraints, views, indexes, …  SQL DDL & DML are integrated.  A DDL statement often needs to contain some DML statements.

Lecture 10SQL: Overview14 A Sample University Schema  Students(SID:string, Name:string, Age:integer, Sex:char, Major:string, GPA:real)  Courses(Cno:string, Title:string, Hours:integer, Dept:string)  Enrollment(SID:string, Cno:string, Year:string, Grade:string)  Offers(Cno:string, Year:integer, FID:string)  Faculty(FID:string, Name:string, Rank:string, Dept:string, Salary:real)  Departments(Name:string, Location:string, ChairID:string)

Lecture 10SQL: Overview15 Create Students Table  In SQL*Plus: SQL> create table Students 2 (SID char(9) not null, 3 Name varchar2(25), 4 Age integer, 5 Sex char(1), 6 Major char(4), 7 GPA number(3,2), 8 primary key (SID));

Lecture 10SQL: Overview16 Create Tables Syntax create table Table-Name ( Col-Name Type Deft-Val Col-Constraint, … Col-Name Type Deft-Val Col-Constraint, Table-Constraint, … Table-Constraint);

Lecture 10SQL: Overview17 Oracle SQL Built-in Data Types  char(n). String of n < 2000 char  varchar2(n). String up to n <= 4000 char  long. Char string of length up to 2GB  number(n,m). n digits, m after decimal point.  number. Integer or real up to 40 digits  integer. Integer up to 40 digits  blob. Binary data up to 4 GB  date. DD-MMM-YY  time. HH:MM:SS  These may differ from SQL2 & SQL-1999.

Lecture 10SQL: Overview18 SQL Integrity Constraints  Rules or regulations imposed to ensure data integrity.  Column Constraints.  Table Constraints.  Assertions (Multiple-table Constraints).  Triggers.  Primary Key, Foreign Key, Check, Not Null, Unique, …

Lecture 10SQL: Overview19 Column Definition  Syntax for column definition: col_name data_type [default value] [column constraints]  Syntax for column constraints: [constraint constraint_name] [not] null | check condition | unique | primary key | references table_name [(column)] [on delete cascade]

Lecture 10SQL: Overview20 Column Constraints  not null. Can not take null value.  unique. Can not have identical non-null values  primary key. Both not null and unique  references T(A). All non-null values must be currently in T.A.  check (condition). Values must satisfy the check condition.  Can be expressed as table constraints, too.

Lecture 10SQL: Overview21 Column Constraints Example SQL> create table Courses (CNo char(6) primary key, Title varchar2(50) not null, Hours integer default 3 check (Hours > 0 and hours < 6), Dept varchar2(20) references Departments(Name));

Lecture 10SQL: Overview22 Table Constraints  Syntax for table constraints: [constraint constraint_name] check condition | unique (column {, column}) | primary key (column {, column}) | foreign key (column {, column}) references table_name[(column {, column})] [on delete cascade]

Lecture 10SQL: Overview23 Table Constraints Example SQL> create table Enrollment (SID char(9) not null references Students, CNo varchar2(7) not null, Year number(2) not null, Grade char(2), primary key (SID, CNo, Year), foreign key (CNo) references Courses);

Lecture 10SQL: Overview24 Table Constraints Example (cont.) SQL> create table Students (SID char(9) primary key, Name varchar2(25), Age integer check(Age > 18 and Age < 100), Sex char check(Sex in {‘F’, ‘M’}), Major varchar2(4) GPA number (3,2) not null, constraint ic12 check (GPA >= 2.0 and (Major = ‘IS’ or GPA >= 3.0)));

Lecture 10SQL: Overview25 Referential Integrity & Data Update  Assume that Courses.Dept references Departments.Name. What should the system do to students if we change a department’s name or delete a department?  SQL provides four options:  No action. Disallow such an update.  Cascade. Accept update and update all affected foreign key values.  Set default. Accept update & set default FK.  Set null. Accept update & set FK to null.

Lecture 10SQL: Overview26 Referential Integrity Example SQL>create table Courses ( CNo char(6) not null primary key, Title varchar(35) not null, Hours int check (Hours between 1 and 5), Dept varchar(20), foreign key (Dept) references Departments(Name) on delete no action on update cascade);

Lecture 10SQL: Overview27 Drop Table  Delete schema definition of a table. drop table Table-Name;  Problem: drop table Departments will fail if it is referenced by foreign keys.  Solution: drop table Departments cascade constraints; All referential constraints will be dropped before the table is dropped.

Lecture 10SQL: Overview28 Alter Table  Change table schema (even after entering data)  Add a new column. alter table Students add (Address varchar2(40));  Add a new constraint. alter table Students add (unique(Address));  Modify a column definition. alter table Students modify (Name varchar2(30));

Lecture 10SQL: Overview29 Alter Table (cont.)  Remove a column. alter table Students drop (Address);  Enable and disable a constraint on a table alter table Students enable constraint ic12; alter table Students disable constraint ic12;  Newly added column can not be specified as not null.  Can not modify a column to a type of a smaller size.

Lecture 10SQL: Overview30 Simple Update Statements  Insert Statement: insert into table_name [(column {, column})] [values (expression {, expression})]  Update Statement: update table_name [corr_name] set column = {expression | null} {, column = {expression | null}} [where search_condition]  Delete Statement: delete from table_name

Lecture 10SQL: Overview31 Example of Update insert into Students values (` ', `Kathy', 26, ‘F’, ‘CS’, null) or insert into Students (Name, SID, Age, Major, Sex) values (`Kathy', ` ', 26, ‘CS’, ‘F’)

Lecture 10SQL: Overview32 Example of Update (cont.)  Increase the GPA of the student with SID = by 0.5. update Students set GPA = GPA where SID = ' ‘  Delete all tuples from Students. delete from Students  The schema of Students remains.

Lecture 10SQL: Overview33 Data Dictionary  Data dictionary (system catalog) contains information about all database objects (tables, views, indexes, sequences, etc).  Common Oracle Data Dictionary Tables  user_objects(object_name, object_id, object_type, created, last_ddl_time, timestamp, status)  Example database objects include tables, views, sequences, indexes, and packages.

Lecture 10SQL: Overview34 Data Dictionary (cont.)  user_tables(table_name, tablespace_name, num_rows, blocks, empty_blocks, avg_row_len)  user_tab_columns(name, table_name, column_name, data_type, data_length, nullable, column_id, default_length, data_default, num_distinct, low_value, high_value)  Use select * from dictionary to see all system tables and views.  Use describe table-name to view a schema.

Lecture 10SQL: Overview35 DDL Summary  Specify appropriate data type for each column. You may also define your own domains.  Specify as many constraints as needed for applications.  Specify desirable actions for foreign key constraints.  Not all constraints can be specified at the same time. It is necessary to update schemas.  Major schema change after data is entered is very costly.

Lecture 10SQL: Overview36 Look Ahead  Next topic: Interactive SQL  Read textbook:  Oracle 8 Programming: A primer by R. Sunderraman. Chapter 2.