Chapter 5 Introduction to SQL.

Slides:



Advertisements
Similar presentations
Structured Query Language (SQL)
Advertisements

Database Systems: Design, Implementation, and Management Tenth Edition
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Introduction to Structured Query Language (SQL)
Introduction to Structured Query Language (SQL)
A Guide to SQL, Seventh Edition. Objectives Understand the concepts and terminology associated with relational databases Create and run SQL commands in.
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)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Structured Query Language SQL: An Introduction. SQL (Pronounced S.Q.L) The standard user and application program interface to a relational database is.
Introduction to SQL J.-S. Chou Assistant Professor.
A Guide to SQL, Eighth Edition Chapter Three Creating Tables.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
ASP.NET Programming with C# and SQL Server First Edition
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
LOGO 1 Lab_02: Basic SQL. 2 Outline  Database Tables  SQL Statements  Semicolon after SQL Statements?  SQL DML and DDL  SQL SELECT Statement  SQL.
15 Structured Query Language (SQL). 2 Objectives After completing this section, you should be able to: Understand Structured Query Language (SQL) and.
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
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.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
Chapter 7 Working with Databases and MySQL PHP Programming with MySQL 2 nd Edition.
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
Database: SQL and MySQL
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting MySQL – Selecting Data.
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)
Using Special Operators (LIKE and IN)
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.
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.
Open Source Server Side Scripting ECA 236 Open Source Server Side Scripting MySQL – Inserting Data.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
1 DBS201: Introduction to Structure Query Language (SQL) Lecture 1.
Database Management COP4540, SCS, FIU Structured Query Language (Chapter 8)
Database Systems: Design, Implementation, and Management Eighth Edition Chapter 7 Introduction to Structured Query Language (SQL)
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
Database UpdatestMyn1 Database Updates SQL is a complete data manipulation language that can be used for modifying the data in the database as well as.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
CMPT 258 Database Systems The Relationship Model (Chapter 3)
ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 5: SQL I Rob Gleasure robgleasure.com.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
Fundamentals of DBMS Notes-1.
Web Systems & Technologies
CHAPTER 7 DATABASE ACCESS THROUGH WEB
Insert, Update and the rest…
 2012 Pearson Education, Inc. All rights reserved.
ATS Application Programming: Java Programming
JDBC.
STRUCTURED QUERY LANGUAGE
SQL Tutorial.
Chapter 7 Working with Databases and MySQL
Chapter 8 Working with Databases and MySQL
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Chapter 7 Introduction to Structured Query Language (SQL)
Chapter # 7 Introduction to Structured Query Language (SQL) Part I.
MySQL Database System Installation Overview SQL summary
SQL Basics BCHB697.
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

Chapter 5 Introduction to SQL

SQL Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user specifies what must be done (ex. create a table), but not how it is to be done. Several SQL dialects exist, in different RDBMSs; minor differences among them.

SQL – Creating DB Tables Use CREATE TABLE command for each db table, basic syntax: CREATE TABLE tablename ( column_1 data_type [constraints] , column_2 data_type [constraints], … column_k data_type [constraints] , PRIMARY KEY (column_i [, column_j …]) ] ); Notes: End each SQL command with a semicolon to execute it; A comma separates all table element (column, PK) definitions;

sql.sql CREATE TABLE users ( … first_name VARCHAR(20) NOT NULL, };

VARCHAR Variable length string Size varies from record to record.

create_myusers.sql sql.sql How would you create the table in db_fall17_frank? J:\>mysql -h cscdb.nku.edu -u frank -D db_fall17_frank -p < sql.sql Enter password: ******

MySQL - SHOW and DESCRIBE After you create the tables in your database, you can view: All tables in the currently used database with: show tables; The structure of any of the tables in the current database with: describe table_name;

myusers J:\>mysql -h csweb.hh.nku.edu -u frank -p Enter password: ****** mysql> use db_fall17_frank; mysql> show tables; mysql> describe users;

SQL – Deleting DB Tables Use DROP TABLE command for each db table you want to delete; basic syntax: DROP TABLE tablename; Note: This command deletes all the rows in the table tablename and the table tablename itself!

DML – Adding Table Rows SQL requires the use of the INSERT command to enter data into a table. INSERT command’s basic syntax: INSERT INTO tablename VALUES (value_1, value_2, … , value_n); This version of INSERT: adds one table row (tuple) at a time requires a value to be specified for every column of the table; values are specified in the same order columns were defined in

sql.sql INSERT INTO users … SHA1('mypass') – 160 bit hash digest of password NOW() – function return the current system date and time

INSERT Example: Notes: insert into customers values (1, "Julie Smith", "25 Oak Street", "Airport West"); insert into orders values (NULL, 1, 49.99, "2007-04-15"); Notes: The row contents are delimited by parentheses Attribute entries are separated by commas String and date values must be quoted (‘ or “) Numerical entries are not enclosed in any special characters

users INSERT INTO users VALUES (NULL, ‘Charles', ‘Frank', ‘frank@nku.edu', SHA1(‘secret'), NOW());

SELECT SELECT command is used to list the contents of a table (or more tables). The simplest form (syntax) of a SELECT query is: SELECT column_list FROM tablename; Column_list represents one or more attributes from tablename, separated by commas

SELECT SELECT name, city FROM customers; Example: SELECT name, city FROM customers;  the result contains all the rows and only the two specified columns of table customers.

sql.sql SELECT user_id, username FROM users; SELECT * FROM users; Asterisk (*) can be used as a wildcard character to list all attributes for the selected rows (when column_list is *) SELECT user_id, username FROM users;

Listing Table Rows Can limit data selected by placing conditional restrictions on the rows to be included in the output → with the WHERE clause of the SELECT statement. Syntax: SELECT column_list FROM tablename [ WHERE condition_list ] ; Will retrieve all the rows that match the conditions specified in the optional WHERE clause If no rows match the specified criteria  result = an empty set of tuples (not an error!) condition_list = one or more conditional expressions connected by logical operators

Listing Table Rows (cont) WHERE clause example: SELECT title FROM books WHERE price > 40; Conditional restrictions in the condition_list : column_name comparison_operator value expression comparison_operator expression expression – with columns and values (constants) as operands; Comparisons include the usual “suspects” =, !=, <, etc. Also can use BETWEEN value1 AND value2 If a data field is empty, can test with IS NULL operator SELECT name FROM customers WHERE address IS NULL;  Finds records with no address specified

Listing Table Rows (cont) Can match patterns with LIKE Pattern can be simple characters up to RE % matches any number of characters, _ matches 1 character Can match multiple conditions using AND and OR Can negate a condition using NOT Example: SELECT title FROM books WHERE price > 40 AND author like “Thomas%”;

Listing Table Rows Notes: Example: Can sort results of query with ORDER BY: SELECT column_list FROM tablename [ORDER BY column1 [ASC | DESC], column2 [ASC | DESC] …]; Notes: Although ORDER BY produces a sorted output, the actual table contents are unaffected by the ORDER BY clause! ORDER BY clause must be listed last in SELECT (except for LIMIT) Example: SELECT title, price, author FROM books ORDER BY price, author;  all books info sorted by price and, for same price, ordered by the author default

users mysql> select * from users; mysql> select email from users where last_name='Frank'; mysql> select * from users where email like '%edu';

Updating Table Rows Use the UPDATE command to modify data in a table. UPDATE command’s syntax: UPDATE tablename SET column_1 = expression_1 [, column_2 = expression_2 …] [WHERE condition_list]; expression = a simple value (76 or ‘Florida’), or a formula (price – 10) condition_list = one or more conditional expressions connected by logical operators (and, or, not) If more than one attribute is to be updated per tuple, separate modifications with commas

Updating Table Rows (cont) Examples: UPDATE books SET price = price * 1.1; increase all the book prices by 10% UPDATE customers SET address = ‘250 Olsens Road’ WHERE customer_id = 4; update a certain customer’s address – PK used to identify the customer Notes: The WHERE clause is optional If a WHERE clause is not specified, all rows from the specified table will be modified.

users mysql> select * from users; mysql> UPDATE users SET email='mike@authors.com' WHERE user_id = 18; mysql> select * from users where user_id='18';

DML – Deleting Table Rows Use the DELETE command to delete data from a table. DELETE command’s syntax: DELETE FROM tablename [WHERE condition_list ]; Notes: WHERE clause is optional If a WHERE clause is not specified, all rows from the specified table will be deleted

Deleting Table Rows (cont) Examples: DELETE FROM customers WHERE customer_id = 4; delete data about a customer who didn’t place orders for a long time DELETE FROM books WHERE price < 4; delete all cheap books (none, one, or more tuples can satisfy the condition) DELETE FROM books; delete all books; table is not deleted, but remains empty

users mysql> DELETE FROM users WHERE user_id = 8 LIMIT 1; mysql> select * from users limit 10;