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

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. A C P K Siriwardhana MSc, BSc in Computer Science FIRST COURSE

11. Introduction to SQL

CSC Web Programming. Introduction to SQL

Group A: Assignment No 2

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

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

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

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

SQL. Structured Query Language

Jarek Szlichta

30. Structured Query Language (SQL)

COMP519: Web Programming Autumn 2015

STRUCTURED QUERY LANGUAGE (SQL)

PHP. How Web Applications interact with server side databases CRUD. Connecting and using mysql from PHP PHP provides many mysql specific functions

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


Chapter. Accessing MySQL Databases Using PHP

Full file at

More loops. Control structures / flow control. while loops. Loops / Iteration / doing things over and over and over and over...

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

Structure Query Language (SQL)

Unit 1 - Chapter 4,5

Chapter-14 SQL COMMANDS

Structured Query Language (SQL) Part A. KSE 521 Topic 10 Mun Yi

Database Processing: Fundamentals, Design, and Implementation

Unit 27 Web Server Scripting Extended Diploma in ICT

Database-Aware Fault Localization for Dynamic Web Applications

Web Application Development (WAD) V th Sem BBAITM (Unit 4) By: Binit Patel

MySQL. Prof.Sushila Aghav

PHP Development - Introduction

Databases and SQL. Lecture outline. CSE 190 M (Web Programming) Spring 2008 University of Washington

2) SQL includes a data definition language, a data manipulation language, and SQL/Persistent stored modules. Answer: TRUE Diff: 2 Page Ref: 36

DATABASE MANAGEMENT SYSTEMS PREPARED BY: ENGR. MOBEEN NAZAR

PHP: Cookies, Sessions, Databases. CS174. Chris Pollett. Sep 24, 2008.

CGS 3066: Spring 2017 SQL Reference

SQL BASICS WITH THE SMALLBANKDB STEFANO GRAZIOLI & MIKE MORRIS

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

Database Management Systems by Hanh Pham GOALS

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

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

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

COMS 469: Interactive Media II

LAB 5: STRUCTURED QUERY LANGUAGE (SQL) PART 2

MIS2502: Data Analytics SQL Getting Information Out of a Database Part 1: Basic Queries

Implementing Table Operations Using Structured Query Language (SQL) Using Multiple Operations. SQL: Structured Query Language

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

Test Bank for Database Processing Fundamentals Design and Implementation 13th Edition by Kroenke

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

Lab # 4 Hands-On. DDL and DML Advance SQL Statements Institute of Computer Science, University of Tartu, Estonia

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

What is SQL? Toolkit for this guide. Learning SQL Using phpmyadmin

Oracle Database 11g: SQL and PL/SQL Fundamentals

Networks and Web for Health Informatics (HINF 6220)

SQL Commands & Mongo DB New Syllabus

Almost all new data created today is digital Problem: How to organize the data and store it? Capacity Speed Life of the data Number of users

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

Relational Database Language

Structured Query Language

Overview of MySQL Structure and Syntax [2]

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

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

How to use SQL to create a database

Restricting and Sorting Data. Copyright 2004, Oracle. All rights reserved.

IELM 511 Information Systems Design Labs 5 and 6. DB creation and Population

Oracle Database: SQL and PL/SQL Fundamentals NEW

The Structured Query Language Get Started

Oracle Database: SQL and PL/SQL Fundamentals Ed 2

Lecture 06. Fall 2018 Borough of Manhattan Community College

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

Data Infrastructure IRAP Training 6/27/2016

Database Management Systems,

ASSIGNMENT NO Computer System with Open Source Operating System. 2. Mysql

L e a r n S q l select where

EE221 Databases Practicals Manual

Lecture 5. Monday, September 15, 2014

Princess Nourah bint Abdulrahman University. Computer Sciences Department

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

Exact Numeric Data Types

QUETZALANDIA.COM. 5. Data Manipulation Language

NCSS: Databases and SQL

PHP: Databases and Classes. CS174. Chris Pollett. Sep 29, 2008.

Fundamentals of Database Systems

Chapter 7 PHP Files & MySQL Databases

Lecture 7: Web hacking 3, SQL injection, Xpath injection, Server side template injection, File inclusion

Data Manipulation Language (DML)

RESTRICTING AND SORTING DATA

Read this before starting!

Databases (MariaDB/MySQL) CS401, Fall 2015

MySQL by Examples for Beginners

Chapter 9: Working with MySQL

Institute of Aga. Network Database LECTURER NIYAZ M. SALIH

Introduction to SQL. From Rob and Coronel (2004), Database Systems: Design, Implementation, and Management

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

Mysql interview questions and answers

DATABASE MANAGEMENT SYSTEMS

Databases and SQL programming overview

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

Institute of Aga. Microsoft SQL Server LECTURER NIYAZ M. SALIH

Department of Computer Science & IT

Transcription:

CMPSC 117: WEB DEVELOPMENT SQL stands for Structured Query Language SQL lets you access and manipulate databases SQL is an ANSI (American National Standards Institute) standard 1

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 2

SQL can be divided into two parts: The Data Manipulation Language (DML) The Data Definition Language (DDL). 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 3

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 - deletes a table CREATE INDEX - creates an index (search key) DROP INDEX - deletes an index The SELECT statement is used to select data from a database. The result is stored in a result table, called the result-set. 4

SQL SELECT Syntax: SELECT column_name(s) FROM table_name and SELECT * FROM table_name Note: SQL is not case sensitive. SELECT is the same as select. The "Persons" table: Now we want to select the content of the columns named "LastName" and "FirstName" from the table above. 5

We use the following SELECT statement: SELECT LastName,FirstName FROM Persons The result-set will look like this: Now we want to select all the columns from the "Persons" table. We use the following SELECT statement: SELECT * FROM Persons Tip: The asterisk (*) is a quick way of selecting all columns! 6

The result-set will look like this: The DISTINCT keyword can be used to return only distinct (different) values. SELECT DISTINCT column_name(s) FROM table_name 7

The "Persons" table: Now we want to select only the distinct values from the column named "City" from the table above. We use the following SELECT statement: SELECT DISTINCT City FROM Persons The result-set will look like this: 8

The WHERE clause is used to extract only those records that fulfill a specified criterion. SQL WHERE Syntax: SELECT column_name(s) FROM table_name WHERE column_name operator value The "Persons" table: Now we want to select only the persons living in the city "Sandnes" from the table above. 9

We use the following SELECT statement: SELECT * FROM Persons WHERE City='Sandnes The result-set will look like this: SQL uses single quotes around text values (most database systems will also accept double quotes). Although, numeric values should not be enclosed in quotes. 10

For text values: For numeric values: With the WHERE clause, the following operators can be used: 11

The AND operator displays a record if both the first condition and the second condition is true. The OR operator displays a record if either the first condition or the second condition is true. SELECT * FROM Persons WHERE FirstName='Tove' AND LastName='Svendson' The result-set will look like this: 12

SELECT * FROM Persons WHERE FirstName='Tove' OR FirstName='Ola The result-set will look like this: You can also combine AND and OR (use parenthesis to form complex expressions). Now we want to select only the persons with the last name equal to "Svendson" AND the first name equal to "Tove" OR to "Ola": 13

We use the following SELECT statement: SELECT * FROM Persons WHERE LastName='Svendson' AND (FirstName='Tove' OR FirstName='Ola') The result-set will look like this: The ORDER BY keyword is used to sort the result-set by a specified column. The ORDER BY keyword sort the records in ascending order by default. If you want to sort the records in a descending order, you can use the DESC keyword. SQL ORDER BY Syntax SELECT column_name(s) FROM table_name ORDER BY column_name(s) ASC DESC 14

<?php?> mysql_connect( localhost, username, password ) or die(mysql_error()); <?php mysql_connect( localhost, username, password ) or die(mysql_error()); echo Connected to MySQL! ; mysql_select_db( cmpsc117 ) or die(mysql_error()); echo Connected to Database cmpsc117 ;?> 15

<?php [ ] $result = mysql_query( SELECT * FROM Works_In ) or die(mysql_error()); $row = mysql_fetch_array($result); echo company:.$row[ company ]; echo position:.$row[ position ];?> <?php [ ] while ($row = mysql_fetch_array($result)) { echo company:.$row[ company ]. position:.$row[ position ]; echo <br/> ;}?> 16

The INSERT INTO statement is used to insert a new row in a table. 17

Two forms of writing the INSERT INTO statement: The first form doesn't specify the column names where the data will be inserted, only their values: INSERT INTO table_name VALUES (value1, value2, value3,...) The second form specifies both the column names and the values to be inserted: INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...) "Persons" table: INSERT INTO Persons VALUES (4,'Nilsen', 'Johan', 'Bakken 2', 'Stavanger') 18

The following SQL statement will add a new row, but only add data in the "P_Id", "LastName" and the "FirstName" columns: INSERT INTO Persons (P_Id, LastName, FirstName) VALUES (5, 'Tjessem', 'Jakob') 19

The UPDATE statement is used to update existing records in a table. UPDATE table_name SET column1=value1, column2=value2,.. WHERE some_column=some_value Note: The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated! UPDATE Persons SET Address='Nissestien 67', City='Sandnes' WHERE LastName='Tjessem' AND FirstName='Jakob' 20

UPDATE Persons SET Address='Nissestien 67', City='Sandnes' 21

The DELETE statement is used to delete rows in a table. DELETE FROM table_name WHERE some_column=some_value Note: The WHERE clause specifies which record or records that should be deleted. If you omit the WHERE clause, all records will be deleted! 22

DELETE FROM Persons WHERE LastName='Tjessem' AND FirstName='Jakob' 23

DELETE FROM table_name or DELETE * FROM table_name Note: Be very careful when deleting records. You cannot undo this statement! 24