STRUCTURED QUERY LANGUAGE

Slides:



Advertisements
Similar presentations
Structure Query Language (SQL) COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
Advertisements

Structured Query Language - SQL Carol Wolf Computer Science.
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.
SQL SQL stands for Structured Query Language SQL allows you to access a database SQL is an ANSI standard computer language SQL can execute queries against.
Introduction to Structured Query Language (SQL)
Your Oracle Account UserName is the same as your UWP username Followed Not case sensitive Initial Password: UWPstudent Password is case sensitive.
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.
ASP.NET Programming with C# and SQL Server First Edition
LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
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.
CSC 2720 Building Web Applications Database and SQL.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
Chapter 5: Part 1: DDL STRUCTURED QUERY LANGUAGE (SQL)
1 Structured Query Language (SQL). 2 Contents SQL – I SQL – II SQL – III SQL – IV.
7 1 Chapter 7 Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
SQL: Data Manipulation I Chapter 5 CIS 458 Sungchul Hong.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
SQL Unit – 2 Base Knowledge Presented By Mr. R.Aravindhan.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
Features of SQL SQL is an English-like language . It uses words such as select , insert , delete as part of its commend set. SQL is an a non-procedural.
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.
SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Visual Programing SQL Overview Section 1.
SQL Jan 20,2014. DBMS Stores data as records, tables etc. Accepts data and stores that data for later use Uses query languages for searching, sorting,
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.
©Silberschatz, Korth and Sudarshan1 Structured Query Language (SQL) Data Definition Language Domains Integrity Constraints.
SQL.. AN OVERVIEW lecture3 1. Overview of SQL 2  Query: allow questions to be asked of the data and display only the information required. It can include.
ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah.
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
SQL Introduction to database and SQL. Chapter 1: Databases and Database Users 6 Introduction to Databases Databases touch all aspects of our lives. Examples:
Lecture # 24 Introduction to SQL Muhammad Emran Database Systems.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
Understand Data Definition Language (DDL) Database Administration Fundamentals LESSON 1.4.
SQL Basics Review Reviewing what we’ve learned so far…….
Getting started with Accurately Storing Data
Fundamentals of DBMS Notes-1.
Structured Query Language (SQL) DDL
CHAPTER 7 DATABASE ACCESS THROUGH WEB
Chapter 10 SQL DDL.
Chapter 5 Introduction to SQL.
“Introduction To Database and SQL”
國立臺北科技大學 課程:資料庫系統 Chapter 7 SQL Data Definition.
Managing Tables, Data Integrity, Constraints by Adrienne Watt
REV 00 Chapter 4 SQL and QBE DDC 2483 – Database Systems.
SQL: Schema Definition and Constraints Chapter 6 week 6
Minggu 5, Pertemuan 9 SQL: Data Definition
“Introduction To Database and SQL”
DATABASE MANAGEMENT SYSTEM
Structured Query Language (Data definition Language)
Lecturer: Mukhtar Mohamed Ali “Hakaale”
SQL OVERVIEW DEFINING A SCHEMA
Data Model.
SQL .. An overview lecture3.
SQL-1 Week 8-9.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Structured Query Language
Chapter # 7 Introduction to Structured Query Language (SQL) Part I.
MIS2502: Data Analytics SQL 4– Putting Information Into a Database
Session - 6 Sequence - 1 SQL: The Structured Query Language:
DATABASE Purpose of database
SQL (Structured Query Language)
Presentation transcript:

STRUCTURED QUERY LANGUAGE (SQL) Part 1: DDL

Objectives The purpose and importance of SQL. To understand the basic components of SQL. Advantages of SQL To be able to apply SQL command. How to use DDL in SQL How to use DML in SQL

Introduction to SQL Ideally, database language should allow user to: create the database and relation structures; perform insertion, modification, deletion of data from relations; perform simple and complex queries. The language must perform these tasks with minimal user effort and command structure/syntax of the language must be easy to learn. The language must be portable.

Introduction to SQL What is SQL? SQL stands for Structured Query Language SQL lets you access and manipulate databases SQL is an ANSI (American National Standards Institute) standard

Introduction to SQL SQL: Structured Query Language that can: SQL can execute queries against a database SQL can retrieve data from a database SQL can insert records in a database SQL can update records in a database SQL can delete records from a database SQL can create new databases SQL can create new tables in a database SQL can create stored procedures in a database SQL can create views in a database SQL can set permissions on tables, procedures, and views

Introduction to SQL Briefly, SQL can: create the database and relation structures; perform insertion, modification, deletion of data from relations; perform simple and complex queries.

Introduction to SQL 2 major components: Data Definition Language (DDL) defining database structure. allows database objects such as schemas, domains, tables, views and indexes to be created and destroyed. Data Manipulation Language (DML) retrieving and updating data. used to populate and query the tables. data manipulation.

Introduction to SQL 2 major components: Data Definition Language (DDL) The DDL part of SQL permits database tables to be created or deleted. It also define indexes (keys), specify links between tables, and impose constraints between tables. The most important DDL statements in SQL are: CREATE DATABASE - creates a new database ALTER DATABASE - modifies a database CREATE TABLE - creates a new table ALTER TABLE - modifies a table DROP TABLE – removes a table from a database CREATE INDEX - creates an index (search key) DROP INDEX - removes an index

Introduction to SQL 2 major components: Data Manipulation Language (DML) The query and update commands form the DML part of SQL: SELECT - extracts data from a database UPDATE - updates data in a database DELETE - deletes data from a database INSERT INTO - inserts new data into a database

Advantages of SQL Advantages of SQL are: SQL is relatively easy to learn: non-procedural - you specify what information you require, rather than how to get it; Consists of standard English words: CREATE TABLE book (ISBN VARCHAR(15), title VARCHAR(95), price DECIMAL(4,2)); Can be used by a range of users: Database Administrator (DBA) Management Personnel Application Developers Many others type of end-users

Writing SQL Commands SQL statement consists of reserved words and user defined words. Reserved words are a fixed part of SQL and must be spelt exactly as required and can not be split across lines. User-defined words are made up by user and represent names of various database objects such as relations, columns, views.

Writing SQL Commands Most components of an SQL statement are case insensitive, except for literal character data. More readable with indentation and lineation: Each clause should begin on a new line. Start of a clause should line up with start of other clauses. If clause has several parts, should each appear on a separate line and be indented under start of clause.

SQL Statement Example CREATE TABLE customer (custNO INTEGER PRIMARY KEY, lastNAME VARCHAR(30), firstNAME VARCHAR(30), address VARCHAR(30));

*Microsoft Access supports some but not all of the SQL specification!* SQL Limitations SQL is standard, BUT……. Although SQL is an ANSI (American National Standards Institute) standard, there are many different versions of the SQL language. However, to be compliant with the ANSI standard, they all support at least the major commands (such as SELECT, UPDATE, DELETE, INSERT, WHERE) in a similar manner. Note: Most of the SQL database programs also have their own proprietary extensions in addition to the SQL standard! *Microsoft Access supports some but not all of the SQL specification!*

SQL Constraints Constraints are used to limit the type of data that can go into a table. Constraints can be specified when a table is created with the CREATE TABLE statement or after the table is created with the ALTER TABLE statement

SQL Constraints NOT NULL UNIQUE PRIMARY KEY FOREIGN KEY DEFAULT enforces a column to NOT accept NULL values. UNIQUE uniquely identifies each record in a database table. UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a column or set of columns. A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it. you can have many UNIQUE constraints per table, but only one PRIMARY KEY constraint per table. PRIMARY KEY Primary keys must contain unique values. A primary key column cannot contain NULL values. Each table should have a primary key, and each table can have only one primary key. FOREIGN KEY A FOREIGN KEY in one table points to a PRIMARY KEY in another table DEFAULT used to insert a default value into a column. The default value will be added to all new records, if no other value is specified.

Literals All non-numeric literals must be enclosed in single quotes: ‘New York’ ‘London’ All numeric literals must not be enclosed in quotes: 200 650.00

DDL Data Definition Language Defining database structure/schema

Data Definition Languages (DDL) DBMS creates tables using SQL commands Database administrator creates schema Logical group of tables or logical database Groups tables by owner Enforces security Relations and other database objects exist in an environment. Each environment contains one or more catalogs, and each catalog consists of set of schemas. Schema is named collection of related database objects. Objects in a schema can be tables, views, domains, assertions, collations, translations, and character sets. All have same owner.

Data Definition Language (DDL) Data Definition SQL commands describe the database structure or schema: COMMAND/OPTION DESCRIPTION CRATE SCHEMA AUTHORIZATION Creates a new database schema CREATE TABLE Create a new table in the user’s database schema NOT NULL Constraints that ensures that a column doesn’t have a null value UNIQUE have a duplicate values ALTER TABLE Modify a table definition DROP TABLE Permanently remove a table and its structure

DDL: Data Type Data type are type of data that can be defined using SQL: Data Type Description integer(size) int(size) smallint(size) tinyint(size) Hold integers only. The maximum number of digits are specified in parenthesis. decimal(size,d) numeric(size,d) Hold numbers with fractions. The maximum number of digits are specified in "size". The maximum number of digits to the right of the decimal is specified in "d". char(size) Holds a fixed length string (can contain letters, numbers, and special characters). The fixed size is specified in parenthesis. varchar(size) Holds a variable length string (can contain letters, The maximum size is specified in parenthesis. date(yyyymmdd) Holds a date

DDL: Create Table This example demonstrates how you can specify primary key and a maximum length for some columns: Example: CREATE TABLE staff (staffNO VARCHAR(10) NOT NULL PRIMARY KEY, staffNAME VARCHAR(25), city VARCHAR(25) DEFAULT “Jengka”); An empty table ‘staff’ is created with staffNO as a primary key: and a default value for city attribute

DDL: Create Table There are a lots of ways that you can used to determine a PK of a table. Here are some of the examples: CREATE TABLE staff (staffNO VARCHAR(10)PRIMARY KEY, staffNAME VARCHAR(25), city VARCHAR(25)); CREATE TABLE staff (staffNO VARCHAR(10)NOT NULL PRIMARY KEY, staffNAME VARCHAR(25), city VARCHAR(25)); CREATE TABLE staff (staffNO VARCHAR(10), staffNAME VARCHAR(25), city VARCHAR(25), PRIMARY KEY(staffNO));

DDL: Create Table Assume that you have table staff as stated below. Then you want to create table printer that have relation with table staff. You can archive this task by defining Foreign Key .in table printer as shown: An empty table ‘printer’ is created with printerNO as primary key and staffNO as a foreign key: CREATE TABLE staff (staffNO VARCHAR(10)PRIMARY KEY, staffNAME VARCHAR(25), city VARCHAR(25)); CREATE TABLE printer (printerNO VARCHAR(10)NOT NULL PRIMARY KEY, description VARCHAR(25), mod VARCHAR(25), FOREIGN KEY (staffNO) REFERENCES staff(staffNO)); printerNO desciption mod staffNO

DDL: Create Table You also can create table printer first. Then create another table staffprinter that contain composite primary key by using staffNO and printerNO to make relationship between those two table as depicted below: An empty table ‘staffprinter’ is created with composite primary key (printerNO and staffNO) CREATE TABLE printer (printerNO VARCHAR(10) NOT NULL PRIMARY KEY, description VARCHAR(25), mod VARCHAR(25)); CREATE TABLE staffprinter (staffNO VARCHAR(10) NOT NULL REFERENCES staff, printerNO VARCHAR(10) NOT NULL REFERENCES printer, qty INT, PRIMARY KEY (staffNo, printerNo)); staffNO printerNO qty

DDL: Drop Table DROP command will delete the table and its structure. If use with RESTRICT: a table that contains Foreign Key will not be deleted If use with CASCADE: all attributes will also be deleted DROP TABLE staff; DROP TABLE printer RESTRICT;

DDL: Alter Table ALTER command is used if there is a change on the table’s attribute. The following SQL statement will add an attribute to table ‘staff’ ALTER TABLE staff ADD telno char(11); The following SQL statement will rename the column color in table ‘printer’ ALTER TABLE printer RENAME COLUMN color TO printerColor; The following SQL statement will add a primary key to table ‘staff’ ALTER TABLE staff ADD PRIMARY KEY(staffNO); ALTER TABLE printer ADD color char(11); ALTER TABLE printer RENAME COLUMN mod TO model; ALTER TABLE staff ADD PRIMARY KEY (staffNO);

DDL in brief: SQL Statement Descriptions CREATE TABLE Create a new table PK *NOT NULL :: a column does not have a null value *UNIQUE :: a column does not have duplicate value FK, attributes, data types of attributes, length of attributes DROP TABLE Delete table and its structure -RESTRICT :: table with FK will not be deleted -CASCADE :: all attribute will be deleted ALTER TABLE Change table attribute -ADD :: add table attribute -RENAME COLUMN :: rename an attribute