I NTRODUCTION TO SQL Presented by : Satrio Agung Wicaksono.

Slides:



Advertisements
Similar presentations
Data Definition and Integrity Constraints
Advertisements

Virtual training week 4 structured query language (SQL)
Overview Begin 6:00 Quiz15 mins6:15 Review Table Terms25 mins6:40 Short Break10 mins6:50 SQL: Creating Tables60 mins7:50 Break10 mins8:00 Lab – Creating.
Database Systems: Design, Implementation, and Management Tenth Edition
Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Database Constraints. Database constraints are restrictions on the contents of the database or on database operations Database constraints provide a way.
Database Systems Lecture 5 Natasha Alechina
DATABASES AND SQL. Introduction Relation: Relation means table(data is arranged in rows and columns) Domain : A domain is a pool of values appearing in.
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 6: Integrity and Security Thomas Nikl 19 October, 2004 CS157B.
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 Technical Session By: Prof. Adarsh Patel.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Data Modeling and Database Design
Chapter 10 – Database Creation1 IT238: Data Modeling and Database Design Unit 6: Database Creation Instructor: Qing Yan, M.D., Ph.D.
10 Copyright © 2009, Oracle. All rights reserved. Using DDL Statements to Create and Manage Tables.
SQL (DDL & DML Commands)
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.
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
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.
SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.
Database Lab Lecture 1. Database Languages Data definition language ( DDL ) Data definition language –defines data types and the relationships among them.
Prince Sultan University Dept. of Computer & Information Sciences CS 340 Introduction to Database Systems.
Managing Constraints. 2 home back first prev next last What Will I Learn? Four different functions that the ALTER statement can perform on constraints.
SQL: Part 1 Original materials supplied by the Oracle Academic Initiative (OAI). Edited for classroom use by Professor Laku Chidambaram. Not for commercial.
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
Constraints Lesson 8. Skills Matrix Constraints Domain Integrity: A domain refers to a column in a table. Domain integrity includes data types, rules,
DATA AND SCHEMA MODIFICATIONS CHAPTERS 4,5 (6/E) CHAPTER 8 (5/E) 1.
Chapter 5 : Integrity And Security  Domain Constraints  Referential Integrity  Security  Triggers  Authorization  Authorization in SQL  Views 
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.
Session 1 Module 1: Introduction to Data Integrity
©Silberschatz, Korth and Sudarshan1 Structured Query Language (SQL) Data Definition Language Domains Integrity Constraints.
ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah.
1 CS 430 Database Theory Winter 2005 Lecture 10: Introduction to SQL.
Relational Database Management System(RDBMS) Structured Query Language(SQL)
Distribution of Marks For Second Semester Internal Sessional Evaluation External Evaluation Assignment /Project QuizzesClass Attendance Mid-Term Test Total.
SQL: Structured Query Language It enables to create and operate on relational databases, which are sets of related information stored in tables. It is.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
MICROSOFT ACCESS – CHAPTER 5 MICROSOFT ACCESS – CHAPTER 6 MICROSOFT ACCESS – CHAPTER 7 Sravanthi Lakkimsety Mar 14,2016.
LECTURE TWO Introduction to Databases: Data models Relational database concepts Introduction to DDL & DML.
Physical Model Lecture 11. Physical Data Model The last step is the physical design phase, In this phase data is – Store – Organized and – Access.
Constraints Advanced Database Systems Dr. AlaaEddin Almabhouh.
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.
SQL Basics Review Reviewing what we’ve learned so far…….
1 Section 1 - Introduction to SQL u SQL is an abbreviation for Structured Query Language. u It is generally pronounced “Sequel” u SQL is a unified language.
Data Integrity & Indexes / Session 1/ 1 of 37 Session 1 Module 1: Introduction to Data Integrity Module 2: Introduction to Indexes.
Database Constraints Ashima Wadhwa. Database Constraints Database constraints are restrictions on the contents of the database or on database operations.
Getting started with Accurately Storing Data
Fundamentals of DBMS Notes-1.
CHAPTER 7 DATABASE ACCESS THROUGH WEB
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
SQL-1 Week 8-9.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Chapter # 7 Introduction to Structured Query Language (SQL) Part I.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Presentation transcript:

I NTRODUCTION TO SQL Presented by : Satrio Agung Wicaksono

D EFINITION Structured Query Language (SQL) is a high-level language that allows users to manipulate relational data One of the strengths of SQL is that users need only specify the information they need without having to know how to retrieve it

T HREE SQL C ATEGORIES DDL – Data definition language used to define, change, or drop database objects DML – Data manipulation language used to read and modify data DCL – Data control language used to grant and revoke authorizations

H ISTORY OF SQL Don Chamberlin and Ray Boyce from IBM Corporation developed the SQL language in the 1970's as part of the System R project; a project established to provide a practical implementation to Codd's relational model Originally, the language was termed “Structured English Query Language” or SEQUEL, but it was later changed to SQL as SEQUEL was a registered trademark of a UK based company SQL was adopted as a standard language in 1986 by the American National Standards Institute ( ANSI ) and by the International Standards Organization ( ISO ) in 1987.

D EFINING A RELATIONAL DATABASE SCHEMA IN SQL Data Types Like any programming language, databases also support a limited set of data types, which can be used to define the types of data a column can store. Basic data types include integer, float, decimal, char, date, time, blob, and so on

D ATES AND T IMES D ATA T YPES All databases support various date and time specific data types and functions DB2 has the following data types for date and time. : Date (YYYY-MM-DD) Time (HH:MM:SS) Timestamp (YYYY-MM-DD-HH:MM:SS:ssssss) The following, is a partial set of functions specialized for date and time: Year Month Day Dayname Hour Minute Second Microsecond

C REATING A TABLE A table is a data set, organized and stored in rows and columns. A table holds data for like items, for example students, professors, subjects, books etc Entities in a data model generally map to tables when implemented in databases. Attributes of entities map to columns of the table example: create table myTable (col1 integer)

D EFAULT V ALUES When data is inserted into a table, you may want to automatically generate default values for a few columns Using default value : To define a column that will generate a department number as an incremented value of the last department number

NULL VALUES A NULL represents an unknown state For example, a table that stores the course marks of students can allow for NULL values. This could mean to the teacher that the student did not submit an assignment, or did not take an exam. It is different from a mark of zero, where a student did take the exam, but failed on all the questions when you don't want a NULL to be allowed. For example, if the country field is required for your application, ensure you prevent NULL values as follows create table myTable (name varchar(30), country varchar(20) NOT NULL)

C ONSTRAINTS Constraints allow you to define rules for the data in your table. There are different types of constraints: A UNIQUE constraint prevents duplicate values in a table. This is implemented using unique indexes and is specified in the CREATE TABLE statement using the keyword UNIQUE. A NULL is part of the UNIQUE data values domain. A PRIMARY KEY constraint is similar to a UNIQUE constraint, however it excludes NULL as valid data. Primary keys always have an index associated with it. A REFERENTIAL constraint is used to support referential integrity which allows you to manage relationships between tables. This is discussed in more detail in the next section. A CHECK constraint ensures the values you enter into a column are within the rules specified in the constraint.

C ONSTRAINTS, C ONT ’ D … The following example shows a table definition with several CHECK constraints and a PRIMARY KEY defined:

R EFERENTIAL INTEGRITY Referential integrity establishes relationships between tables. Using a combination of primary keys and foreign keys, it can enforce the validity of your data. Referential integrity reduces application code complexity by eliminating the need to place data level referential validation at the application level. Only tables that have columns defined as UNIQUE or PRIMARY KEY can be referenced in other tables as foreign keys for referential integrity

R EFERENTIAL INTEGRITY, C ONT ’ D … Referential integrity can be defined during table definition or after the table has been created as shown in the example below where three different syntaxes are illustrated: Sintax 1 : CREATE TABLE DEPENDANT_TABLE (ID INTEGER REFERENCES BASE_TABLE(UNIQUE_OR_PRIMARY_KEY), NAME VARCHAR(9), : ); Sintax 2 : CREATE TABLE DEPENDANT_TABLE (ID INTEGER, NAME VARCHAR(9), : :, CONSTRAINT constraint_name FOREIGN KEY (ID) REFERENCES BASE_TABLE(UNIQUE_OR_PRIMARY_KEY) );

R EFERENTIAL INTEGRITY, C ONT ’ D … Sintak 3: CREATE TABLE DEPENDANT_TABLE (ID INTEGER, NAME VARCHAR(9), : ); ALTER TABLE DEPENDANT_TABLE ADD CONSTRAINT constraint_name FOREIGN KEY (ID) REFERENCES BASE_TABLE(UNIQUE_OR_PRIMARY_KEY);

R EFERENTIAL INTEGRITY, C ONT ’ D … There are different rules to handle deletes and updates and the behavior depends on the following constructs used when defining the tables: CASCADE As the name suggests, with the cascade option the operation is cascaded to all rows in the dependant tables that are referencing the row or value to be modified or deleted in the base table. SET NULL With this option all the referring cells in dependant tables are set to NULL NO ACTION With this option no action is performed as long as referential integrity is maintained before and after the statement execution. RESTRICT With this option, the update or delete of rows having references to dependant tables are not allowed to continue The statement below shows where the delete and update rules are specified: ALTER TABLE DEPENDANT_TABLE ADD CONSTRAINT constraint_name FOREIGN KEY column_name ON DELETE ON UPDATE ; A delete action type can be a CASCADE, SET NULL, NO ACTION, or RESTRICT. An update action type can be a NO ACT ON, or RESTRICT

C REATING A SCHEMA Just in the same way we store and manage data files on a computer in directories or folders and keep related or similar files together; a schema in DB2 is a database object that allows you to group related database objects together. In DB2, every object has two parts, a schema name, and the name of the object

C REATING A SCHEMA, C ONT ’ D … To create a schema, use this statement: create schema mySchema To create a table with the above schema, explicitly include it in the CREATE TABLE statement as follows: create table mySchema.myTable (col1 integer) When the schema is not specified, DB2 uses an implicit schema, which is typically the user ID used to connect to the database. You can also change the implicit schema for your current session with the SET CURRENT SCHEMA command as follows: set current schema mySchema

M ODIFYING DATABASE OBJECTS Once a database object is created, it may be necessary to change its properties to suit changing business requirements. Dropping and recreating the object is one way to achieve this modification; however, dropping the object has severe side effects. A better way to modify database objects is to use the ALTER SQL statement. For example, assuming you would like to change a table definition so that NULLs are not allowed for a given column, you can try this SQL statement: alter table myTable alter column col1 set not null

R ENAMING DATABASE OBJECTS Once database objects are created, they can be renamed using the SQL statement, RENAME. To rename any database object use the following SQL syntax: RENAME to Where the object type can be for example, a table, table space, or index. Not all database objects can be renamed after they are created. To rename a column, the ALTER TABLE SQL statement should be used in conjunction with RENAME. For example: ALTER TABLE RENAME COLUMN TO

D ROPING DATABASE TABLE DROP TABLE statement allows you to remove tables from your schema: DROP TABLE SCHEMA.TABLE_NAME Ex : DROP TABLE DB2ADMIN.EMPLOYEE

D ATA MANIPULATION WITH SQL Selecting Data Inserting Data Deleting Data Updating Data

EMPLOYEE T ABLE - P ART 1 (1 OF 2)

EMPLOYEE T ABLE - P ART 1 (2 OF 2)

DEPARTMENT T ABLE

PROJECT T ABLE

S TRUCTURE OF AN SQL Q UERY SELECT - Defines result columns Column names Arithmetic expressions Literals (text or numeric) Scalar functions Column functions Concatenation FROM - Table or view names WHERE - Conditions (qualifies rows) ORDER BY - Sorts result rows

R ETRIEVING A LL C OLUMNS, A LL R OWS

R ETRIEVING A LL C OLUMNS, L IMITED R OWS

S ELECTING S PECIFIC C OLUMNS

S ELECT WITH O RDERED O UTPUT (1 OF 2)

S ELECT WITH O RDERED O UTPUT (2 OF 2)

A LTERNATE ORDER BY S PECIFICATIONS SELECT LASTNAME, FIRSTNME, WORKDEPT, JOB, SEX FROM EMPLOYEE ORDER BY WORKDEPT DESC, JOB, LASTNAME, SEX DESC Equivalent ORDER BY clauses: ORDER BY WORKDEPT DESC, JOB ASC, LASTNAME ASC, SEX DESC ORDER BY 3 DESC, 4, 1, 5 DESC ORDER BY 3 DESC, 4 ASC, 1 ASC, 5 DESC ORDER BY 3 DESC, JOB, LASTNAME, 5 DESC ORDER BY WORKDEPT DESC, 4 ASC, 1 ASC, SEX DESC

S UPPRESSING D UPLICATE O UTPUT R OWS (1 OF 2)

S UPPRESSING D UPLICATE O UTPUT R OWS (2 OF 2)

R ETRIEVING R OWS BY C HARACTER C OMPARISON

R ETRIEVING R OWS BY N UMERICAL C OMPARISON

C OMPARISON O PERATORS SELECT * FROM EMPLOYEE WHERE SALARY = equal to OR SALARY <> not equal to OR SALARY > greater than OR SALARY >= greater than or equal to OR SALARY < less than OR SALARY <= less than or equal to

AND AND OR - P RINCIPLE

M ULTIPLE C ONDITIONS - AND

M ULTIPLE C ONDITIONS - OR

M ULTIPLE C ONDITIONS - AND/OR

M ULTIPLE C ONDITIONS - P ARENTHESES

SELECT WITH IN

SELECT WITH BETWEEN

R ETRIEVING R OWS - N ULL C OMPARISON

P ARTIAL S TRING S EARCH - LIKE

P ARTIAL S TRING S EARCH - E XAMPLES WITH '%'

P ARTIAL S TRING S EARCH - E XAMPLE WITH '_'

N EGATION

M AINTAINING D ATA TESTEMP Table The visual shows the contents of the TESTEMP table. This table will be the basis for the exercises of this unit

DDL C REATE T ABLE TESTEMP CREATE TABLE TESTEMP ( EMPNO CHAR(6) NOT NULL, LASTNAME VARCHAR(15) NOT NULL, WORKDEPT CHAR(3), HIREDATE DATE, SALARY DECIMAL(9,2), BONUS DECIMAL(9,2) )

I NSERTING R OWS INTO THE T ABLE

I NSERT M ULTIPLE R OWS

U PDATE C OLUMNS (1 OF 2)

U PDATE C OLUMNS (2 OF 2)

D ELETE R OWS

R EMINDERS You must be authorized to execute CREATE/DROP table statements You must be authorized to execute statements which change table content Dropping a table drops dependent objects Search condition in the WHERE clause qualifies rows to be updated or deleted Inserting a row into a view actually inserts the row into the table on which the view is based Updating a row of a view updates the row of the table on which the view is based Omitting the WHERE clause of an UPDATE or DELETE statement causes all rows of the table or view to be updated or deleted