Simple SQL. Peter Y. Wu. Dept of Computer and Information Systems Robert Morris University

Similar documents
SQL Structured Query Language Introduction

CSC Web Programming. Introduction to SQL

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

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

COGS 121 HCI Programming Studio. Week 03 - Tech Lecture

Chapter 3: Introduction to SQL. Chapter 3: Introduction to SQL

Basic SQL. Dr Fawaz Alarfaj. ACKNOWLEDGEMENT Slides are adopted from: Elmasri & Navathe, Fundamentals of Database Systems MySQL Documentation

EGCI 321: Database Systems. Dr. Tanasanee Phienthrakul

Computing for Medicine (C4M) Seminar 3: Databases. Michelle Craig Associate Professor, Teaching Stream

Relational Algebra and SQL

Chapter 3: Introduction to SQL

QQ Group

Basic SQL. Basic SQL. Basic SQL

Practice for Test 1 (not counted for credit, but to help you prepare) Time allowed: 1 hour 15 minutes

Copyright 2016 Ramez Elmasri and Shamkant B. Navathe

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

The SQL data-definition language (DDL) allows defining :

Lecture 07. Spring 2018 Borough of Manhattan Community College

Database Management Systems,

Announcements (September 14) SQL: Part I SQL. Creating and dropping tables. Basic queries: SFW statement. Example: reading a table

Chapter 3: Introduction to SQL

1Z0-071 Exam Questions Demo Oracle. Exam Questions 1Z Oracle Database 12c SQL.

CS143: Relational Model

SQL language. Jaroslav Porubän, Miroslav Biňas, Milan Nosáľ (c)

Unit 1 - Chapter 4,5

1Z0-071 Exam Questions Demo Oracle. Exam Questions 1Z Oracle Database 12c SQL.

Chapter-14 SQL COMMANDS

GIFT Department of Computing Science Data Selection and Filtering using the SELECT Statement

SQL - Basics. SQL Overview

Slides by: Ms. Shree Jaswal

Introduction to SQL Part 1 By Michael Hahsler based on slides for CS145 Introduction to Databases (Stanford)

CS 582 Database Management Systems II

SQL. SQL Data Manipulation: Queries

ASSIGNMENT NO 2. Objectives: To understand and demonstrate DDL statements on various SQL objects

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

Insertions, Deletions, and Updates

SQL Functionality SQL. Creating Relation Schemas. Creating Relation Schemas

Chapter 9: Working with MySQL

Why Relational Databases? Relational databases allow for the storage and analysis of large amounts of data.

Getting Information from a Table

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

Introduction SQL DRL. Parts of SQL. SQL: Structured Query Language Previous name was SEQUEL Standardized query language for relational DBMS:

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

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

Database System Concepts, 5th Ed.! Silberschatz, Korth and Sudarshan See for conditions on re-use "

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

chapter 2 G ETTING I NFORMATION FROM A TABLE

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

Introduction to SQL. ECE 650 Systems Programming & Engineering Duke University, Spring 2018

CIS Slightly-different version of Week 10 Lab, also intro to SQL UPDATE and DELETE, and more

A Flat file database. Problems with a flat file database (data redundancy)

SQL: Data Definition Language. csc343, Introduction to Databases Diane Horton Fall 2017

EE221 Databases Practicals Manual

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

CGS 3066: Spring 2017 SQL Reference

Structure Query Language (SQL)

CMPT 354: Database System I. Lecture 3. SQL Basics

Review. Objec,ves. Example Students Table. Database Overview 3/8/17. PostgreSQL DB Elas,csearch. Databases

SQL: DDL. John Ortiz Cs.utsa.edu

DATABASE TECHNOLOGY. Spring An introduction to database systems

STRUCTURED QUERY LANGUAGE (SQL)

SQL: Part II. Announcements (September 18) Incomplete information. CPS 116 Introduction to Database Systems. Homework #1 due today (11:59pm)

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

SQL: Aggregate Functions with Grouping

NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E)

Creating and Managing Tables Schedule: Timing Topic

COSC 304 Introduction to Database Systems SQL DDL. Dr. Ramon Lawrence University of British Columbia Okanagan

Database Management Systems,

Outline. Textbook Chapter 6. Note 1. CSIE30600/CSIEB0290 Database Systems Basic SQL 2

COMP 430 Intro. to Database Systems

DATABASE TECHNOLOGY - 1MB025

SQL Data Definition: Table Creation

The Relational Model of Data (ii)

1 SQL Structured Query Language

Announcements (September 18) SQL: Part II. Solution 1. Incomplete information. Solution 3? Solution 2. Homework #1 due today (11:59pm)

30. Structured Query Language (SQL)

MySQL Creating a Database Lecture 3

Principles of Data Management

SQL DATA DEFINITION LANGUAGE

DATABASE TECHNOLOGY - 1MB025

Retrieving Data Using the SQL SELECT Statement. Copyright 2009, Oracle. All rights reserved.


Access Intermediate

Database Management Systems by Hanh Pham GOALS

DATABASTEKNIK - 1DL116

GlobAl EDITION. Database Concepts SEVENTH EDITION. David M. Kroenke David J. Auer

GIFT Department of Computing Science. CS-217: Database Systems. Lab-4 Manual. Reporting Aggregated Data using Group Functions

CSIE30600 Database Systems Basic SQL 2. Outline

SQL: Part III. Announcements. Constraints. CPS 216 Advanced Database Systems

1) Introduction to SQL

Chapter 4: SQL. Basic Structure

The Relational Model. Week 2

WHAT IS A DATABASE? There are at least six commonly known database types: flat, hierarchical, network, relational, dimensional, and object.

SQL - Tables. SQL - Create a SQL Table. SQL Create Table Query:

Using the Set Operators. Copyright 2006, Oracle. All rights reserved.

Relational Model History. COSC 416 NoSQL Databases. Relational Model (Review) Relation Example. Relational Model Definitions. Relational Integrity

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

Introduction to Data Management. Lecture #4 (E-R Relational Translation)

NESTED QUERIES AND AGGREGATION CHAPTER 5 (6/E) CHAPTER 8 (5/E)

The Structured Query Language Get Started

Transcription:

Simple SQL Peter Y. Dept of Computer and Information Systems Robert Morris University Simple SQL create table drop table insert into table values ( ) delete from table where update table set a to v where select * from table distinct all order by 2 (c) Peter Y - RMU. 1

Tables in SQL Query language for relational database Table = relation Column = attribute Row = tuple NOTE: SQL tables allow duplicate rows; will disallow duplicates only when a key constraint (primary key or secondary key) is violated. 3 Create Table To create a table, the simple SQL: Create table T1 (C1 number, C2 char(8), C3 varchar2(10)); Creates a table by the name T1, with 3 columns C1, C2, C3. C1takes numbers for data values. C2takes strings of 8 characters, padded with blanks. C3takes strings of up to 10 characters. The table T1 is then created, and the table has no data. Number of rows (records) in table T1 is zero, now. 4 (c) Peter Y - RMU. 2

Create Table create table T1 (C1 number, C2 char(8), C3 varchar2(10)); T1 C1 C2 C3 5 Create Table create table <table-name> (<column#1-name> <domain-name-for-column#1>, <column#2-name> <domain-name-for-column#2>, ); Domain-name can be any data type name, or a user-defined domain-name for some specific data type. Standard SQL has a set of data types for use; each vendor DBMS may have its own set. 6 (c) Peter Y - RMU. 3

create table: some data types for standard SQL, but every vendor system is different. char(n) fixed-length character string varchar(n) variable length character string number numerical value up to 40 spaces number(n) numerical value up to n spaces number(n,d) n spaces with d after decimal point integer same as number, integer values only integer(n) same as integer, but n spaces only date a date of the Gregorian Calendar: 28-Jul-2000 7 Drop Table To remove a table, drop it with SQL: drop table T1; The table T1 is removed (deleted). The table is gone. The data, if any in the table, is also gone. Can the drop table statement produce an error? 8 (c) Peter Y - RMU. 4

Insert data into a table Data in a table must be inserted one row at a time; to insert a row into a table insert into T1 (C1,C2,C3) values (33, apple, orange ); A row is inserted; note the corresponding values for each column. 9 Insert data into a table The values should match the column names. insert into T1 (C2,C3,C1) values ( peach, pineapple,7); A row is inserted; note the corresponding values for each column. 10 (c) Peter Y - RMU. 5

Look at the contents of a table To show the contents of a table: select * from T1; C1 C2 C3 ===== ======== ============ 33 apple orange 7 peach pineapple The content of table T1 is listed: all 3 columns, and all rows. 11 Look at the contents of a table To select just some of the columns to list select C2, C1 from T1; C2 C1 ========= ===== apple 33 peach 7 Only the selected columns are listed; those omitted are not listed. 12 (c) Peter Y - RMU. 6

SQL: insert 2 forms of SQL insert statement: insert into <table-name> [ (<column>,, <column>) ] values (<expr>,, <expr>); insert into <table-name> [ (<column>,, <column>) ] ( <select-statement> ); The second form of insert uses a sub-query the topic will be discussed again later, in more advanced SQL. 13 insert: a new record create table T1 (c1 integer, c2 integer); insert into T1 values (11,23); insert into T1 (c1,c2) values (22,45); insert into T1 (c2,c1) values (67,33); insert into T1 (c1) values (44); T1 C1 C2 11 23 22 45 33 67 44 14 (c) Peter Y - RMU. 7

insert: a whole table using sub-query create table T2 (N1 integer, N2 integer); insert into T2 (N2,N1) select c1,c2 from T1; T1 C1 C2 11 23 22 45 33 67 44 T2 N1 N2 23 11 45 22 67 33 44 15 SQL: delete Delete statement: delete from <table-name> [ where <search-condition> ]; Thewhere <search-condition> is optional. To delete all rows in the table T1: delete from T1; /* delete all rows in T1 */ The rows are deleted; the table becomes empty. The table is still there! 16 (c) Peter Y - RMU. 8

SQL: delete delete from T1 where c1 = 44; T1 T1 T1 C1 C2 C1 C2 C1 C2 11 23 11 23 22 45 22 45 33 67 33 67 44 delete from T1 where c2 = c1*2+1; Table T1 is now empty, but the table is still there. NOTE: delete may result in the update/deletion of rows in some other tables, to resolve referential integrity constraint violation. 17 SQL: update Update statement: update <table-name> [<tuple-variable>] set <column> = <expr>, <column> = <expr> where <serach-condition>; Update picks on the particular rows of the table which meet the search-condition, and updates the specified columns to the new values of the corresponding expressions. NOTE: update may result in the update/deletion of rows in some other tables, to resolve referential integrity constraint violation. 18 (c) Peter Y - RMU. 9

SQL: update update T2 set N1 = N2*2-1 where N2 = 44; T2 T2 T2 N1 N2 N1 N2 N1 N2 23 11 23 11 50 11 45 22 45 22 50 22 67 33 67 33 67 33 44 87 44 87 44 update T2 set N1 = 50 where N1 < 50; 19 SQL: select The basic SQL query has the form: Select C1,C2,,Cm from T1,T2,,Tn where <condition>; C1,C2,..Cm: column names (or expressions of) T1,T2,..Tn: table names simple query: use 1 table Example select instructor from course where topic = 4240; 20 (c) Peter Y - RMU. 10

SQL: select select instructor from course where CNum = 4240; INSTRUCTOR ---------- 21 SQL: select select title, enroll from course; TITLE ENROLL -------------------------------------- -------- Information Systems 25 Data Structures 30 Database Management 28 Java Programming 17 Electronic Commerce 20 22 (c) Peter Y - RMU. 11

SQL: select using expression select title, 30-Enroll as Available Seats from course; TITLE Available Seats -------------------------------------- --------------- Information Systems 5 Data Structures 0 Database Management 2 Java Programming 13 Electronic Commerce 10 23 SQL: select everything with * select * from course; CNUM SECTION TITLE INSTRUCTOR ENROLL ----- ------- --------------------- ---------- -------- 2240 A Data Structure 30 24 (c) Peter Y - RMU. 12

SQL: select another example select * from course where instructor = ; CNUM SECTION TITLE INSTRUCTOR ENROLL ----- ------- -------------------- ----------- -------- 25 What tables do I have? The information about your tables are also kept in a table called the system catalog. In Oracle it is abbreviated as cat select * from cat; TABLE_NAME TABLE_TYPE ================ =========== T1 TABLE Oracle SQL*Plus will then list the content of table cat, telling you what tables you have. 26 (c) Peter Y - RMU. 13

Duplicates and their Removal The result of an SQL query is also a table. Recall that the Relational Model does not allow duplicate rows in the same table, but DBMS supporting SQL allows that. The query result table may contain duplicates. How do the duplicate rows come about? For example, listing instructors in the course table select Instructor from Course where Enroll < 35; 27 Duplicates and their Removal To list the instructors only instructor names teaching as least one course with less than 35 students. select Instructor from course where Enroll < 35 ; Instructor Wood 28 (c) Peter Y - RMU. 14

To Remove Duplicates Use the keyword distinct immediately after the select keyword. select distinct Instructor from Course where Enroll < 35 ; Instructor Wood Removal of duplicate entries can be very very costly taking a lot of time (why?!); Caution: do that only when it is necessary! 29 Duplicates and their Removal The keyword all is optional: it is the default anyway no removal of duplicate entries unless keyword distinct is there. select all Instructor from Course where Enroll < 35 ; Instructor Wood 30 (c) Peter Y - RMU. 15

SQL: order by clause A table has NO pre-defined order. It is a set, therefore no order (& no duplicates). When we present the result of an SQL query as a table, we may specify order. Use the order by clause order by column [asc desc], The column may also be an expression. 31 SQL: order by clause select distinct Instructor from course where Enroll < 35; Find the instructors teaching courses with available seats. Instructor Wood Actually, there is NO guarantee about the order of the instructor names presented in the result of an SQL. 32 (c) Peter Y - RMU. 16

SQL: order by clause select distinct Instructor from course where Enroll < 35 order by instructor; -- List the names in alphabetical order. Instructor Wood The instructor names are sorted in the default ascending order when presented in the result. 33 SQL: order by clause select distinct instructor from course order by instructor desc; -- List the names in reverse alphabetical (descending) order. Instructor Wood The default is ascending order. 34 (c) Peter Y - RMU. 17

SQL: order by clause select distinct instructor, title from course order by instructor asc, title desc; Instructor Wood Title Electronic Comm Data Structures Information Syst Java Program Database Man Instructor names are in alphabetical order; Titles in reversed alphabetical order within each group identified by the instructor.. 35 (c) Peter Y - RMU. 18