Introduction to Structured Query Language (SQL) COM S 461 2013-Fall Instructor: Ying Cai Iowa State University 1.

Slides:



Advertisements
Similar presentations
SQL Rohit Khokher.
Advertisements

Basic SQL Introduction Presented by: Madhuri Bhogadi.
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Introduction to Structured Query Language (SQL)
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
Introduction to Structured Query Language (SQL)
Fundamentals, Design, and Implementation, 9/e Chapter 6 Introduction to Structured Query Language (SQL)
Structured Query Language Part I Chapter Three CIS 218.
Introduction to Structured Query Language (SQL)
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.
SQL Basics. SQL SQL (Structured Query Language) is a special-purpose programming language designed from managing data in relational database management.
Introduction to SQL J.-S. Chou Assistant Professor.
Rationale Aspiring Database Developers should be able to efficiently query and maintain databases. This module will help students learn the Structured.
Session 5: Working with MySQL iNET Academy Open Source Web Development.
Chapter 9 SQL and RDBMS Part C. SQL Copyright 2005 Radian Publishing Co.
ASP.NET Programming with C# and SQL Server First Edition
1 IT420: Database Management and Organization SQL - Data Manipulation Language 27 January 2006 Adina Crăiniceanu
PHP Programming with MySQL Slide 8-1 CHAPTER 8 Working with Databases and MySQL.
 SQL stands for Structured Query Language.  SQL lets you access and manipulate databases.  SQL is an ANSI (American National Standards Institute) standard.
Database A collection of related data. Database Applications Banking: all transactions Airlines: reservations, schedules Universities: registration, grades.
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.
Oracle Database Administration Lecture 2 SQL language.
CPS120: Introduction to Computer Science Lecture 19 Introduction to SQL.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
SQL Data Definition Language (DDL) Using Microsoft SQL Server 1SDL Data Definition Language (DDL)
SQL SQL Server : Overview SQL : Overview Types of SQL Database : Creation Tables : Creation & Manipulation Data : Creation & Manipulation Data : Retrieving.
Chapter 5 MYSQL Database. Introduction to MYSQL MySQL is the world's most popular open-source database. Open source means that the source code, the programming.
Database Processing: Fundamentals, Design, and Implementation, 9/e by David M. KroenkeChapter 6/1 Copyright © 2004 Please……. No Food Or Drink in the class.
SQL: DDL. SQL Statements DDL - data definition language –Defining and modifying data structures (metadata): database, tables, views, etc. DML - data manipulation.
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.
Topic 1: Introduction to SQL. SQL stands for Structured Query Language. SQL is a standard computer language for accessing and manipulating databases SQL.
SQL Unit – 2 Base Knowledge Presented By Mr. R.Aravindhan.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
SQL Structured Query Language 1. Data Definition Language (DDL) is used to manage table and define data structure i.e. CREATE, ALTER, DROP Data Control.
SQL. คำสั่ง SQL SQL stands for Structured Query Language is a standard language for accessing and manipulating databases.
DBSQL 5-1 Copyright © Genetic Computer School 2009 Chapter 5 Structured Query Language.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
Visual Programing SQL Overview Section 1.
5. Simple SQL using Oracle1 Simple SQL using Oracle 5. Working with Tables: Data management and Retrieval 6. Working with Tables: Functions and Grouping.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
ITEC 3220A Using and Designing Database Systems Instructor: Prof. Z. Yang Course Website: 3220a.htm
SQL in Oracle. Set up Oracle access at IU You need to install Oracle Client: – – For windows:
>> Introduction to MySQL. Introduction Structured Query Language (SQL) – Standard Database Language – Manage Data in a DBMS (Database Management System)
1 MySQL and SQL. 2 Topics  Introducing Relational Databases  Terminology  Managing Databases MySQL and SQL.
There are two types of MySQL instructions (Data Definition Language) DDL: Create database, create table, alter table,,,. (Data Manipulation Language) DML.
ECMM6018 Enterprise Networking For Electronic Commerce Tutorial 6 CGI/Perl and databases.
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.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
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.
Understand Data Definition Language (DDL) Database Administration Fundamentals LESSON 1.4.
SQL Reminder Jiankang Yuan Martin Lemke. SQL Reminder - SELECT SELECT column_name1, column_name2, … FROM table_name SELECT * FROM table_name.
 MySQL is a database system used on the web  MySQL is a database system that runs on a server  MySQL is ideal for both small and large applications.
Web Systems & Technologies
SQL Query Getting to the data ……..
Introduction to Structured Query Language(SQL)
SQL in Oracle.
The Database Exercises Fall, 2009.
Chapter 7 Working with Databases and MySQL
Structured Query Language
MENAMPILKAN DATA DARI SATU TABEL (Chap 2)
Chapter # 7 Introduction to Structured Query Language (SQL) Part II.
Database systems Lecture 3 – SQL + CRUD
Access: SQL Participation Project
Database Management System
Section 4 - Sorting/Functions
Presentation transcript:

Introduction to Structured Query Language (SQL) COM S Fall Instructor: Ying Cai Iowa State University 1

SQL: What and Why ? (1/2) Programmers Computer Instructions Result 2 Programming Language:

SQL: What and Why ? (2/2) DB Users DBMS (Database Server) Query Result 3 Query Language:

Contents Setup a Database Server on your Computer. How to define data using SQL: Data Definition Language (DDL) Create Tables/Update Tables… How to manipulate/access data using SQL: Data Manipulation Language (DML) Insert Data/Update Data/Read Data… SQL Functions 4

Setup Database Server (1/2) Step 1. Download (Installer/Source code available) MySQL MariaDB Step 2. Setup & Configuration Port. Remote Access. Root User. Step 3. Connect to server. You’ll need: User/PSW, Server IP/Port. 5

Setup Database Server (2/2) Database Server Client 1 Client 2 Client 3 The Clinet/Server Structure 6 DB Connection

Programming with SQL SQL Data Definition Language 1.Create DB/Table 2.Change DB/Table 3.Delete DB/Table Data Manipulation Language 1.Add Data 2.Update Data 3.Remove Data 4. Get Data Data Control Language Access Control Authorization … 7

Data Definition Language 8 (Basic) Keywords of DDL: Create a new Database/Table CREATE Change the definition of an existing DB/table ALTER Delete an existing DB/table. DROP

Syntax of CREATE Create a new database. Create a new table 9 CREATE TABLE table_name ( column_1 INT, column_2 CHAR(10) NOT NULL, column_3 VARCHAR(256), column_4 INT DEFAULT 0, … ) ; CREATE DATABASE db_name; Data types: INT/INTEGER(size) DECIMAL(size,d) CHAR(size) VARCHAR(size) Date(yyyymmdd)

Syntax of CREATE Create a new database. Create a new table 10 CREATE TABLE table_name ( column_1 INT, column_2 CHAR(10) NOT NULL, column_3 VARCHAR(256), column_4 INT DEFAULT 0, … ) ; CREATE DATABASE db_name; Data types: INT/INTEGER(size) DECIMAL(size,d) CHAR(size) VARCHAR(size) Date(yyyymmdd) Colunm_1Colunm_2Colunm_3Colunm_4 (Empty)

Syntax of ALTER Add a column to an exiting table. Change a column of an exiting table. Delete a column from an exiting table. 11 ALTER TABLE table_name ADD column_name data_type; ALTER TABLE table_name ALTER COLUMN column_name new_data_type; or CHANGDE COLUMN old_name new_name data_type; or MODIFY COLUMNB column_name new_data_type; ALTER TABLE table_name DROP COLUMN column_name;

Syntax of DROP Delete a Database Delete a Table. 12 DROP DATABASE db_name DROP TABLE table_name

Data Manipulation Language 13 (Basic) Keywords of DML: Read data from DB SELECT Change the value of existing items UPATE Add new items into DB INSERT Remove existing items from DB DELETE

Syntax of SELECT 14 Use SELET to get desired data in given format: Example 1: Get students’ Uid and name, who have GPA higher than 3.5, sorted by their GPA in descending order. SELECT column_1 column_2 … FROM table_1 table_2 … WHERE condition ORDER BY expression ASC/DESC SELECT uid, student_name FROM students WHERE gpa > 3.5 ORDER BY gpa DESC

Syntax of SELECT 15 Example 2: Get all information about female students who is older than 20. Example 3: Get the average speed of all cars in a table where only moving distance and time are stored. SELECT * FROM students WHERE ( 2013 – YoB ) > 20 AND gener = ‘F’ SELECT (distance/time) AS speed FROM cars WHERE time > 0

Syntax of SELECT 16 Select from Multiple Tables: (Multiple Table Query) The DBMS will combine the two tables to generate a new Virtual Table (Cross Join). SELECT * FROM students course UidName 001Andy 002Jack cidNameStudents 100Database001, Algorithm002

Syntax of SELECT 17 Select from Multiple Tables: (Multiple Table Query) The DBMS will combine the two tables to generate a new Virtual Table (Cross Join). SELECT * FROM students course UidNamecidNameStudents 001Andy100Database001, Andy101Algorithm002 Jack100Database001, Jack101Algorithm002

Syntax of SELECT 18 Example 4: Show the uid and name of all students who registered for the Database course. SELECT student.uid student.name FROM students course WHERE course.name = “Database” AND student.id IN course.students

Syntax of SELECT 19 Example 5: We have three tables: Show the name of each course and the name of the TA assigned to this course. UidName 001Andy 002Jack CIDName 100Database 111Algorithm UidCID SELECT course.name, student.name FROM students course TA WHERE Courese.Cid = TA.Cid AND Student.Uid = TA.Uid StudentCourseTA

Syntax of SELECT 20 Select Distinct values (No duplicates) Example 6: Show students with distinct birthday and name. SELECT DISTINCT column_1, column_2 … FROM table_1, table_2 … WHERE conditions … SELECT DISTINCT Name DoB FROM Student Course

Syntax of SELECT 21 Select a given number of items SELECT TOP NUMBER column_1 column_2 FROM table_1 table_2 WHERE conditions… or SELECT column_1 column_2 FROM table_1 table_2 WHERE conditions… LIMIT NUMBER

Syntax of SELECT 22 Example 7: Show the name of 100 students with the highest GPA. SELECT name FROM students ORDER BY gpa DESC LIMIT 100

Syntax of Update 23 Update the value of existing item(s). Update table_name SET column_1 = value_1 column_2 = value_2 … WHERE conditions…

Syntax of Insert 24 Insert new items into a table INSERT INTO table_name VALURES (value_1, value_2 … ), # row 1 (value_1, value_2 … ), # row 2 …. …

Syntax of Delete 25 Delete items from a table DELETE without any condition will delete all items in the table, which is forbidden by many DBMS. DELETE FROM table_name WHERE conditions…

SQL Functions 26 AVG() Calculate the Average of selected data column ORDER BY/LIMIT is no longer necessory. SELECT AVG( column_name| expression) AS value_name FROM table_1, table_2 … WHERE conditions …

SQL Functions 27 COUNT () Count the number of items selected. MAX () / MIN () Return the Maximal/Minimal value in selected data. SELECT COUNT( column_name| expression) AS value_name FROM table_1, table_2 … WHERE conditions … SELECT MAX/MIN( column_name| expression) AS value_name FROM table_1, table_2 … WHERE conditions …

SQL Functions 28 SUM() Calculate the sum of selected values. SELECT SUM( column_name| expression) AS value_name FROM table_1, table_2 … WHERE conditions …

SQL Leaning resources 29 General Introductions and Examples: W3CSchool SQL reference MySQL: T-SQL (Microsoft): us/library/ms189826(v=sql.90).aspxhttp://technet.microsoft.com/en- us/library/ms189826(v=sql.90).aspx OracleSQL: /toc.htm /toc.htm

Q&A 30