Structured Query Language. Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with.

Slides:



Advertisements
Similar presentations
Data Definition and Integrity Constraints
Advertisements

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.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 28 Database Systems I The Relational Data Model.
Structured Query Language Review ISYS 650. Language Overview Three major components: –Data definition language, DDL Create, Drop and Alter Tables or Views.
Introduction to Structured Query Language, SQL. SQL Select Command SELECT * FROM tableName WHERE criteria;
A Guide to SQL, Seventh Edition. Objectives Understand the concepts and terminology associated with relational databases Create and run SQL commands in.
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.
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.
Database Management System LICT 3011 Eyad H. Elshami.
Introduction to Structured Query Language SQL. SQL Select Command SELECT * FROM tableName WHERE criteria;
Structured Query Language S Q L. What is SQL It is a database programming language developed by IBM in the early 1970’s. It is used for managing and retrieving.
SQL Overview Defining a Schema CPSC 315 – Programming Studio Slides adapted from those used by Jeffrey Ullman, via Jennifer Welch Via Yoonsuck Choe.
Copyright © Curt Hill SQL The Data Definition Language.
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
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.
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.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
1 The Relational Model Instructor: Mohamed Eltabakh
Chapter 8 Part 1 SQL-99 Schema Definition, Constraints, Queries, and Views.
Ms. Hatoon Al-Sagri CCIS – IS Department SQL-99 :Schema Definition, Constraints, Queries, and Views 1.
Chapter 7 SQL HUANG XUEHUA. SQL SQL server2005 introduction Install components  management studio.
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.
SQL FUNDAMENTALS SQL ( Structured Query Language )
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
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.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
A Guide to MySQL 3. 2 Introduction  Structured Query Language (SQL): Popular and widely used language for retrieving and manipulating database data Developed.
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.
Tables and Constraints Oracle PL/SQL. Datatypes The SQL Data Definition Language Commands (or DDL) enable us to create, modify and remove database data.
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.
Chapter 4 Constraints Oracle 10g: SQL. Oracle 10g: SQL 2 Objectives Explain the purpose of constraints in a table Distinguish among PRIMARY KEY, FOREIGN.
Visual Programing SQL Overview Section 1.
Sql DDL queries CS 260 Database Systems.
Introduction to Query Language and SQL. Basic Query Language Operators Selection Projection Join Aggregates –Sum, Count, Max, Min, Avg SubTotal Calculated.
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.
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
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.
1 CS 430 Database Theory Winter 2005 Lecture 11: SQL DDL.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
LECTURE FOUR Introduction to SQL DDL with tables DML with tables.
Physical Model Lecture 11. Physical Data Model The last step is the physical design phase, In this phase data is – Store – Organized and – Access.
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.
3 A Guide to MySQL.
Managing Tables, Data Integrity, Constraints by Adrienne Watt
Data Definition and Data Types
SQL: Schema Definition and Constraints Chapter 6 week 6
ORACLE SQL Developer & SQLPLUS Statements
DATABASE MANAGEMENT SYSTEM
Instructor: Mohamed Eltabakh
SQL OVERVIEW DEFINING A SCHEMA
SQL data definition using Oracle
DATABASE SQL= Structure Query Language مبادئ قواعد بيانات
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.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
SQL (Structured Query Language)
Presentation transcript:

Structured Query Language

Brief History Developed in early 1970 for relational data model: –Structured English Query Language (SEQUEL) –Implemented with IBM System R 1987 first ISO standard version 1992 SQL SQL 3 –Object-relational model 2003:SQL 2003 –SQL/XML Products: DB2, Oracle, MS SQL, MySQL

Language Overview Three major components: –Data definition language Create Table –Data manipulation language Updating database: –Insert, Delete, Update Query database: –Select –Data control language (DCL) Help DBA control the database: –Grant/revoke privileges to access the database, creating procedures, etc. Interface with database: –Entering command interactively at the DBMS command prompt. –Embedded in a procedural language

Data Definition Language SQL Identifiers –Character set: A-Z, a-z, 0-9, _ –<= 128 characters –Start with a letter –Cannot contain spaces

SQL Data Types Boolean –True, False, Unknown (for Null) Character –Fixed length: CHARACTER(n), CHAR(n), CHAR – default to 1 character –Varying length: CHARACTER VARYING(n): Maximum length is n VARCHAR(n) Numeric –NUMERIC(i,j), DECIMAL(i,j), DEC(i,j) –INTEGER, INT, SMALLINT – up to –FLOAT, REAL, DOUBLE PRECISION Date: –DATE –TIME –TIMESTAMP Large objects: –CHARACTER LARGE OBJECT –BINARY LARGE OBJECT

CREATE TABLE CREATE TABLE tableName(fields and data type separated by commas); Ex. –CREATE TABLE employee( eid CHAR(5), ename VARCHAR(40), sex CHAR, salary NUMERIC(9,2), hire_Date DATE);

Integrity Enhancement Feature Required data: NOT NULL –eid CHAR(5) NOT NULL, Default value:DEFAULT sex CHAR DEFAULT ‘M’, Field domain: CHECK(condition) –salary NUMERIC(9,2) CHECK (salary >= 100 AND salary <=10000), –sex CHAR DEFAULT ‘M’ CHECK (sex in (‘M’,’F”)), PRIMARY KEY –PRIMARY KEY(sid) –PRIMARY KEY(sid, cid) Unique – allow null value, the PRIMARY KEY constraint does not allow null. –ESSN CHAR(9) UNIQUE

–CREATE TABLE employee( eid CHAR(5) PRMARY KEY, ename VARCHAR(40), sex CHAR DEFAULT ‘M’ CHECK (sex in (‘M’,’F”)), salary NUMERIC(9,2), hire_Date DATE);

Composite Key Example create table orderdetail (oid char(3), cid char(5), qty numeric(5,2), primary key (oid,cid));

ALTER TABLE ADD/Modify/DROP COLUMN a new field from a table. Ex. ALTER TABLE employee ADD phone CHAR(8); ALTER TABLE employee Modify phone CHAR(9); ALTER TABLE employee DROP COLUMN Phone;

Adding Constraints with the ALTER TABLE command Constraints: –PRIMARY KEY, CHECK, UNIQUE PRIMARY KEY: –ALTER TABLE tablename –ADD CONSTRAINT constraintname –PRIMARY KEY (columnname); CHECK –ALTER TABLE tablename –ADD CONSTRAINT constraintname –CHECK (criteria);

Examples ALTER TABLE emp ADD CONSTRAINT empkey PRIMARY KEY (empid); ALTER TABLE emp ADD CONSTRAINT validSalary CHECK (salary between 100 AND 20000); Note: Constraints information are stored in table: USER_CONSTRAINTS. You can use the DESCRIBE command to show fields in this table.

Dropping Constraints ALTER TABLE tablename DROP CONSTRAINT constraintname;

Disable/Enable Constraints ALTER TABLE tablename DISABLE CONSTRAINT constraintname; ALTER TABLE tablename ENABLE CONSTRAINT constraintname;

Creating Table Through SubQuery CREATE TABLE tableName AS (Select query) Ex. –CREATE TABLE newEmp –AS (SELECT empid, ename,salary FROM emp);

Renaming a Table Rename oldName to newName

Removing a Table DROP TABLE tableName

SQL Insert Command INSERT INTO tableName VALUES (field values separated by commas); INSERT INTO tableName (Column names separated by commas)VALUES (field values separated by commas); Ex 1. Customer table with CID, CNAME, CITY, RATING. a. INSERT INTO CUSTOMER VALUES (‘C1’, ‘SMITH’, ‘SF’, ‘A’); b. INSERT INTO CUSTOMER (CID, CNAME,RATING) VALUES (‘C1’, ‘SMITH’, ‘A’);

Record with Date Field Oracle date format: –‘dd-mmm-yyyy’ Example: insert into orders values('O7','c2','s1','10-oct-2007');

Inserting records from an existing table INSERT INTO stu2 (select * from student);

SQL Delete Command DELETE FROM tableName [WHERE criteria]; Ex 1. Delete a record from the Customer table. DELETE FROM CUSTOMER WHERE CID = ‘C1’;

SQL Update Command UPDATE tableName SET field = new value [WHERE criteria]; Ex 1. UPDATE CUSTOMER SET RATING = ‘A’ WHERE CID=‘C1’; Ex 2. UPDATE CUSTOMER SET CITY = ‘SF’, RATING = ‘A’ WHERE CID=‘C1’;

Dealing with Null Null is a key word. We can use Null in the INSERT, UPDATE, and DELETE command. Use IS NULL (or IS NOT NULL) in a criteria. Examples: –INSERT INTO emp VALUES (‘e95’,’June’,’f’,NULL,5000); –UPDATE emp SET salary=null where empid=‘e99’; –SELECT * FROM emp WHERE salary IS NULL; –SELECT * FROM emp WHERE salary= NULL (not working);