SQL. A C P K Siriwardhana MSc, BSc in Computer Science FIRST COURSE

Similar documents
Structured Query Language (SQL) lab syllabus 4 th science. SQL is used to communicate with a database. it is the standard language for relational

SQL stands for Structured Query Language. SQL lets you access and manipulate databases

Group A: Assignment No 2

11. Introduction to SQL

Data Base Lab. The Microsoft SQL Server Management Studio Part-3- By :Eng.Alaa I.Haniy.

Unit 1 - Chapter 4,5

CSC Web Programming. Introduction to SQL

COMP519: Web Programming Autumn 2015

MySQL. Prof.Sushila Aghav

SQL BASICS WITH THE SMALLBANKDB STEFANO GRAZIOLI & MIKE MORRIS

SQL. Structured Query Language

UNIT-3. The entity-relationship model (or ER model) is a way of graphically representing the

What is MySQL? [Document provides the fundamental operations of PHP-MySQL connectivity]

Practical 8. SHANKER SINH VAGHELA BAPU INSTITUTE OF TECHNOLGY Subject: - DBMS Branch: - IT/CE Semester: - 3rd. 1. What does SQL stand for?

Alper VAHAPLAR

CSE 626: Data mining. Instructor: Sargur N. Srihari. Phone: , ext. 113

Fundamentals of Database Systems

DATABASE MANAGEMENT SYSTEMS

Databases - 4. Other relational operations and DDL. How to write RA expressions for dummies

Downloaded from

Database Foundations. 6-4 Data Manipulation Language (DML) Copyright 2015, Oracle and/or its affiliates. All rights reserved.


Graphical Joins in More Detail

INDEX. 1 Basic SQL Statements. 2 Restricting and Sorting Data. 3 Single Row Functions. 4 Displaying data from multiple tables

Private Institute of Aga NETWORK DATABASE LECTURER NIYAZ M. SALIH

STRUCTURED QUERY LANGUAGE (SQL)

S.Y. Diploma : Sem. III [CO/CM/IF/CD/CW] Relational Database Management System

Unit 27 Web Server Scripting Extended Diploma in ICT

IT360: Applied Database Systems. SQL: Structured Query Language DDL and DML (w/o SELECT) (Chapter 7 in Kroenke) SQL: Data Definition Language

3ISY402 DATABASE SYSTEMS

How to use SQL to create a database

WHAT IS SQL. Database query language, which can also: Define structure of data Modify data Specify security constraints

STRUCTURED QUERY LANGUAGE (SQL)

Chapter-14 SQL COMMANDS

SQL: Concepts. Todd Bacastow IST 210: Organization of Data 2/17/ IST 210

Data Infrastructure IRAP Training 6/27/2016

Introduction to SQL. IT 5101 Introduction to Database Systems. J.G. Zheng Fall 2011

SQL. Char (30) can store ram, ramji007 or 80- b

Database Management Systems,

Chapter 7. Introduction to Structured Query Language (SQL) Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel

30. Structured Query Language (SQL)

Database Management Systems by Hanh Pham GOALS

Structure Query Language (SQL)

Chapter 13 : Informatics Practices. Class XI ( As per CBSE Board) SQL Commands. New Syllabus Visit : python.mykvs.in for regular updates

COSC344 Database Theory and Applications. Lecture 5 SQL - Data Definition Language. COSC344 Lecture 5 1

Chapter 4. Basic SQL. SQL Data Definition and Data Types. Basic SQL. SQL language SQL. Terminology: CREATE statement

tablename ORDER BY column ASC tablename ORDER BY column DESC sortingorder, } The WHERE and ORDER BY clauses can be combined in one

Data Manipulation Language (DML)

Structured Query Language (SQL)

1Z Oracle Database 11g - SQL Fundamentals I Exam Summary Syllabus Questions

Database Systems: Design, Implementation, and Management Tenth Edition. Chapter 7 Introduction to Structured Query Language (SQL)

CS 327E Lecture 2. Shirley Cohen. January 27, 2016

CMPT 354: Database System I. Lecture 2. Relational Model

COSC344 Database Theory and Applications. Lecture 6 SQL Data Manipulation Language (1)

SQL DATA MANIPULATION. Prepared By: Dr. Vipul Vekariya.

Chapter 4: SQL Basics

3/3/2008. Announcements. A Table with a View (continued) Fields (Attributes) and Primary Keys. Video. Keys Primary & Foreign Primary/Foreign Key

D B M G. SQL language: basics. Managing tables. Creating a table Modifying table structure Deleting a table The data dictionary Data integrity

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Simple Quesries in SQL & Table Creation and Data Manipulation

SQL Commands & Mongo DB New Syllabus

Downloaded from

Database Management Systems,

Chapter 4. Basic SQL. Copyright 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

MySQL Introduction. By Prof. B.A.Khivsara

CMP-3440 Database Systems

DB Creation with SQL DDL

Relational Database Management Systems for Epidemiologists: SQL Part I

Chapter # 7 Introduction to Structured Query Language (SQL) Part II

Insertions, Deletions, and Updates

Structured Query Language

1) Introduction to SQL

STRUCTURED QUERY LANGUAGE (SQL)

SQL functions fit into two broad categories: Data definition language Data manipulation language

SQL. SQL Functions. SQL Aggregate Functions. SQL Scalar functions. SQL Aggregate Functions. The AVG() Function

Web Security. Attacks on Servers 11/6/2017 1

Networks and Web for Health Informatics (HINF 6220)

SQL Server 2012 Development Course

The Structured Query Language Get Started

Chapter 9: Working with MySQL

Database design process

Islamic University of Gaza Faculty of Engineering Department of Computer Engineering Fall 2011 ECOM 4113: Database System Lab Eng.

Exact Numeric Data Types

SQL DATA MANIPULATION. Prepared By: Vipul Vekariya M.E( Computer) Gardi College Of Eng. & Tech. Rajkot.

Database Foundations. 6-3 Data Definition Language (DDL) Copyright 2015, Oracle and/or its affiliates. All rights reserved.

Chapter 9: Working With Data

Database and table creation

SQL - Subqueries and. Schema. Chapter 3.4 V4.0. Napier University

Lecture 07. Spring 2018 Borough of Manhattan Community College

Sql Server Syllabus. Overview

Database Technology. Topic 3: SQL. Olaf Hartig.

Relational Database Development

Welcome to SQL tutorial

NCSS: Databases and SQL

SIT772 Database and Information Retrieval WEEK 6. RELATIONAL ALGEBRAS. The foundation of good database design

DS Introduction to SQL Part 1 Single-Table Queries. By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

Fname A variable character field up to 15 characters in length. Must have a value Lname A variable character field up to 15

Introduction. Sample Database SQL-92. Sample Data. Sample Data. Chapter 6 Introduction to Structured Query Language (SQL)

EXAMINATIONS 2009 END-YEAR. COMP302 / SWEN304 Database Systems / Database System Engineering. Appendix

Lecture7: SQL Overview, Oracle Data Type, DDL and Constraints Part #2

Transcription:

SQL A C P K Siriwardhana MSc, BSc in Computer Science FIRST COURSE

Organizing Data New Perspectives on Microsoft Office 2007: Windows Edition 2

Databases and Relationships A collection of related tables is called a database, or a relational database You connect the records in the separate tables through a common field A primary key is a field, or a collection of fields, whose values uniquely identify each record in a table When you include the primary key from one table as a field in a second table to form a relationship between the two tables, it is called a foreign key in the second table New Perspectives on Microsoft Office 2007: Windows Edition 3

Databases and Relationships New Perspectives on Microsoft Office 2007: Windows Edition 4

Creating a Table in SQL Mode New A C Perspectives P K Siriwardhana, on Microsoft Office MSc, 2007: BSc Windows Computer Edition Science 5

Creating a Table in SQL Mode Create table staff ( id number, name char(20), title char(15), h_rate number, manager char(10), primary key (id) ); New Perspectives on Microsoft Office 2007: Windows Edition 6

Execute SQL Click the Design button on the Quick Access Toolbar. Click the RUN button Then Save the query New Perspectives on Microsoft Office 2007: Windows Edition 7

Insert data in to a table using SQL INSERT Insert into staff values(2, "Namal", "Mr", 1000, "Vijethung"); Insert Data Only in Specified Columns INSERT INTO staff (P_Id, LastName, FirstName) VALUES (5, 'Tjessem', 'Jakob') New A C Perspectives P K Siriwardhana, on Microsoft Office MSc, 2007: BSc Windows Computer Edition Science 8

Retrieve data from a table using Select SQL Select * from staff; Select id,name,h_rate from staff; Select id, name, h_rate from staff where id =2; Select id, name, h_rate from staff where id =2 or id = 4; New A C Perspectives P K Siriwardhana, on Microsoft Office MSc, 2007: BSc Windows Computer Edition Science 9

Retrieve data from a table using SQL Select count(name) from staff; SELECT COUNT(name) FROM staff WHERE name='kamal'; Select id, name, h_rate from staff where id =2 or id = 4; New A C Perspectives P K Siriwardhana, on Microsoft Office MSc, 2007: BSc Windows Computer Edition Science 10

SQL DDL and DML DDL CREATE DATABASE - creates a new database ALTER DATABASE - modifies a database CREATE TABLE - creates a new table ALTER TABLE - modifies a table DROP TABLE - deletes a table CREATE INDEX - creates an index (search key) DROP INDEX - deletes an index New A C Perspectives P K Siriwardhana, on Microsoft Office MSc, 2007: BSc Windows Computer Edition Science 11

SQL DML and DDL 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 New A C Perspectives P K Siriwardhana, on Microsoft Office MSc, 2007: BSc Windows Computer Edition Science 12

The INSERT INTO Statement Person 3 Pettersen Kari Storgt 20 Stavanger INSERT INTO Persons VALUES (4,'Nilsen', 'Johan', 'Bakken 2', 'Stavanger') Insert Data Only in Specified Columns INSERT INTO Persons (P_Id, LastName, FirstName) VALUES (5, 'Tjessem', 'Jakob') New Perspectives on Microsoft Office 2007: Windows Edition 13

The SQL SELECT DISTINCT Statement SELECT DISTINCT column_name(s) FROM table_name Person 3 Pettersen Kari Storgt 20 Stavanger SELECT DISTINCT City FROM Persons City Sandnes Stavanger New Perspectives on Microsoft Office 2007: Windows Edition 14

The WHERE Clause The SQL WHERE clause is used to specify a condition while fetching the data from single table or joining with multiple tables. If the given condition is satisfied then only it returns specific value from the table. SELECT column_name(s) FROM table_name WHERE column_name operator value SELECT * FROM Persons WHERE City='Sandnes' New A C Perspectives P K Siriwardhana, on Microsoft Office MSc, 2007: BSc Windows Computer Edition Science 15

The WHERE Clause SELECT column_name(s) FROM table_name WHERE column_name operator value person 3 Pettersen Kari Storgt 20 Stavanger SELECT * FROM Persons WHERE City='Sandnes' New Perspectives on Microsoft Office 2007: Windows Edition 16

The WHERE Clause (Cont..) Operators Allowed in the WHERE Clause Operator Description = Equal <> Not equal > Greater than < Less than >= Greater than or equal <= Less than or equal BETWEEN Between an inclusive range LIKE Search for a pattern IN If you know the exact value you want to return for at least one of the columns New Perspectives on Microsoft Office 2007: Windows Edition 17

AND & OR Operators AND Operator SELECT * FROM Persons WHERE FirstName='Tove' AND LastName='Svendson' Operartor 1 Svendson Ola Timoteivn 10 Sandnes 3 Pettersen Kari Storgt 20 Stavanger New Perspectives on Microsoft Office 2007: Windows Edition 18

AND & OR Operators OR Operator SELECT * FROM Persons WHERE FirstName='Tove' OR FirstName='Ola' Operartor 3 Pettersen Kari Storgt 20 Stavanger New Perspectives on Microsoft Office 2007: Windows Edition 19

AND & OR Operators Combining AND & OR SELECT * FROM Persons WHERE LastName='Svendson'AND (FirstName='Tove' OR FirstName='Ola') Operartor 3 Pettersen Kari Storgt 20 Stavanger New Perspectives on Microsoft Office 2007: Windows Edition 20

SQL ORDER BY Keyword SELECT column_name(s) FROM table_name ORDER BY column_name(s) ASC DESC Person 3 Pettersen Kari Storgt 20 Stavanger 4 Nilsen Tom Vingvn 23 Stavanger SELECT * FROM Persons ORDER BY LastName 4 Nilsen Tom Vingvn 23 Stavanger 3 Pettersen Kari Storgt 20 Stavanger New Perspectives on Microsoft Office 2007: Windows Edition 21

The UPDATE Statement Person 3 Pettersen Kari Storgt 20 Stavanger 4 Nilsen Johan Bakken 2 Stavanger 5 Tjessem Jakob UPDATE Persons SET Address='Nissestien 67', City='Sandnes' WHERE p_id = 5 Person 3 Pettersen Kari Storgt 20 Stavanger 4 Nilsen Johan Bakken 2 Stavanger 5 Tjessem Jakob Nissestien 67 Sandnes New Perspectives on Microsoft Office 2007: Windows Edition 22

SQL DELETE Statement Persons 3 Pettersen Kari Storgt 20 Stavanger 4 Nilsen Johan Bakken 2 Stavanger 5 Tjessem Jakob Nissestien 67 Sandnes DELETE FROM Persons WHERE LastName='Tjessem' AND FirstName='Jakob' Persons 3 Pettersen Kari Storgt 20 Stavanger 4 Nilsen Johan Bakken 2 Stavanger New Perspectives on Microsoft Office 2007: Windows Edition 23

SQL ALTER TABLE Statement ADD Column Person ALTER TABLE table_name ADD COLUMN column_name datatype 3 Pettersen Kari Storgt 20 Stavanger ALTER TABLE Person ADD COLUMN DateOfBirth date DateOfBirth 3 Pettersen Kari Storgt 20 Stavanger New Perspectives on Microsoft Office 2007: Windows Edition 24

SQL ALTER TABLE Statement Change Data Type Example ALTER TABLE table_name ALTER column_name datatype ALTER TABLE Persons ALTER COLUMN DateOfBirth year DateOfBirth 3 Pettersen Kari Storgt 20 Stavanger New Perspectives on Microsoft Office 2007: Windows Edition 25

SQL ALTER TABLE Statement DROP Column ALTER TABLE table_name drop column_name DateOfBirth 3 Pettersen Kari Storgt 20 Stavanger ALTER TABLE Persons DROP DateOfBirth Person 3 Pettersen Kari Storgt 20 Stavanger New Perspectives on Microsoft Office 2007: Windows Edition 26

Databases and Relationships Create table customer ( cus_id char(15) primary key, F_name char(20), l_name char(55), phone char(20) ); Create table contract ( con_num number primary key, cus_id char(20) references customer(cus_id), con_amt currency, sig_date date ); New Perspectives on Microsoft Office 2007: Windows Edition 27

Create table customer ( cus_id char(15) primary key, F_name char(20), l_name char(55), phone char(20) ); Create table contract ( con_num number primary key, cus_id char(15) references customer(cus_id), con_amt currency, sig_date date ); New Perspectives on Microsoft Office 2007: Windows Edition 28

Add foreign key - 2 nd method CREATE TABLE Orders ( O_Id int NOT NULL, OrderNo int NOT NULL, P_Id int, PRIMARY KEY (O_Id), FOREIGN KEY (P_Id) REFERENCES Persons(P_Id) ) New A C Perspectives P K Siriwardhana, on Microsoft Office MSc, 2007: BSc Windows Computer Edition Science 29

Add foreign key - 3 nd method CREATE TABLE Orders ( O_Id int NOT NULL, OrderNo int NOT NULL, P_Id int, PRIMARY KEY (O_Id), CONSTRAINT fk_perorders FOREIGN KEY (P_Id) REFERENCES Persons(P_Id) ) New Perspectives on Microsoft Office 2007: Windows Edition 30

Add foreign key 4 th method ALTER TABLE Orders ADD FOREIGN KEY (P_Id) REFERENCES Persons(P_Id) New Perspectives on Microsoft Office 2007: Windows Edition 31

Add foreign key 5 th method ALTER TABLE Orders ADD CONSTRAINT fk_perorders FOREIGN KEY (P_Id) REFERENCES Persons(P_Id) New Perspectives on Microsoft Office 2007: Windows Edition 32

Drop Foreign key ALTER TABLE Orders DROP FOREIGN KEY fk_perorders New A C Perspectives P K Siriwardhana, on Microsoft Office MSc, 2007: BSc Windows Computer Edition Science 33

Drop Foreign key ALTER TABLE Orders DROP CONSTRAINT fk_perorders New A C Perspectives P K Siriwardhana, on Microsoft Office MSc, 2007: BSc Windows Computer Edition Science 34

Person 3 Pettersen Kari Storgt 20 Stavanger New Perspectives on Microsoft Office 2007: Windows Edition 35

Working with Where Customer : cus_id, f_name, l_name, phone Contract : con_id, cus_id, con_amt, sig_date Select Customer.cus_id, Customer.f_name, Customer.l_name, Contract.con_amt, Contract.sig_date from Customer,Contract where Customer.cus_id = Contract.cus_id; New Perspectives on Microsoft Office 2007: Windows Edition 36