Introduction to Structured Query Language (SQL) COM S 461 2013-Fall Instructor: Ying Cai Iowa State University 1 SQL: What and Why ? (1/2) Programming Language: Instructions Result Programmers Computer 2 SQL: What and Why ? (2/2) Query Language: Query Result DB Users DBMS (Database Server) 3 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 http://dev.mysql.com/downloads/ MariaDB https://downloads.mariadb.org/ 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) The Clinet/Server Structure Client 1 DB Connection Client 2 Database Server Client 3 6 Programming with SQL SQL Data Definition Language Data Manipulation Language Data Control Language 1.Create DB/Table 2.Change DB/Table 3.Delete DB/Table 1.Add Data 2.Update Data 3.Remove Data 4. Get Data Access Control Authorization … 7 Data Definition Language (Basic) Keywords of DDL: CREATE • Create a new Database/Table ALTER • Change the definition of an existing DB/table DROP • Delete an existing DB/table. 8 Syntax of CREATE Create a new database. CREATE DATABASE db_name; Create a new table Data types: CREATE TABLE table_name ( column_1 INT, column_2 CHAR(10) NOT NULL, column_3 VARCHAR(256), column_4 INT DEFAULT 0, …… ); • • • • • INT/INTEGER(size) DECIMAL(size,d) CHAR(size) VARCHAR(size) Date(yyyymmdd) 9 Syntax of CREATE Create a new database. CREATE DATABASE db_name; Create a new table Data types: CREATE TABLE table_name ( column_1 INT, column_2 CHAR(10) NOT NULL, column_3 VARCHAR(256), column_4 INT DEFAULT 0, …… ); Colunm_1 Colunm_2 • • • • • (Empty) (Empty) INT/INTEGER(size) DECIMAL(size,d) CHAR(size) VARCHAR(size) Date(yyyymmdd) Colunm_3 Colunm_4 (Empty) (Empty) 10 Syntax of ALTER Add a column to an exiting table. ALTER TABLE table_name ADD column_name data_type; Change a column of an exiting table. ALTER TABLE table_name ALTER COLUMN column_name new_data_type; or or CHANGDE COLUMN old_name new_name data_type; MODIFY COLUMNB column_name new_data_type; Delete a column from an exiting table. ALTER TABLE table_name DROP COLUMN column_name; 11 Syntax of DROP Delete a Database DROP DATABASE db_name Delete a Table. DROP TABLE table_name 12 Data Manipulation Language (Basic) Keywords of DML: SELECT • Read data from DB UPATE • Change the value of existing items INSERT • Add new items into DB DELETE • Remove existing items from DB 13 Syntax of SELECT Use SELET to get desired data in given format: SELECT column_1 column_2 … FROM table_1 table_2 … WHERE condition ORDER BY expression ASC/DESC Example 1: Get students’ Uid and name, who have GPA higher than 3.5, sorted by their GPA in descending order. SELECT uid, student_name FROM students WHERE gpa > 3.5 ORDER BY gpa DESC 14 Syntax of SELECT Example 2: Get all information about female students who is older than 20. SELECT * FROM students WHERE ( 2013 – YoB ) > 20 AND gener = ‘F’ Example 3: Get the average speed of all cars in a table where only moving distance and time are stored. SELECT (distance/time) AS speed FROM cars WHERE time > 0 15 Syntax of SELECT 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 Uid Name cid Name Students 001 Andy 100 Database 001,002 002 Jack 101 Algorithm 002 16 Syntax of SELECT 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 Uid Name cid Name Students 001 Andy 100 Database 001,002 001 Andy 101 Algorithm 002 002 Jack 100 Database 001,002 002 Jack 101 Algorithm 002 17 Syntax of SELECT 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 18 Syntax of SELECT Example 5: We have three tables: Uid Name CID Name Uid CID 001 Andy 100 Database 001 100 002 Jack 111 Algorithm 002 111 Student Course TA Show the name of each course and the name of the TA assigned to this course. SELECT course.name, student.name FROM students course TA WHERE Courese.Cid = TA.Cid AND Student.Uid = TA.Uid 19 Syntax of SELECT Select Distinct values (No duplicates) SELECT DISTINCT column_1, column_2 … FROM table_1, table_2 … WHERE conditions … Example 6: Show students with distinct birthday and name. SELECT DISTINCT Name DoB FROM Student Course 20 Syntax of SELECT 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 21 Syntax of SELECT Example 7: Show the name of 100 students with the highest GPA. SELECT name FROM students ORDER BY gpa DESC LIMIT 100 22 Syntax of Update Update the value of existing item(s). Update table_name SET column_1 = value_1 column_2 = value_2 …… WHERE conditions… 23 Syntax of Insert Insert new items into a table INSERT INTO table_name VALURES (value_1, value_2 … ), # row 1 (value_1, value_2 … ), # row 2 …. … 24 Syntax of Delete Delete items from a table DELETE FROM table_name WHERE conditions… DELETE without any condition will delete all items in the table, which is forbidden by many DBMS. 25 SQL Functions AVG() Calculate the Average of selected data column SELECT AVG( column_name| expression) AS value_name FROM table_1, table_2 … WHERE conditions … ORDER BY/LIMIT is no longer necessory. 26 SQL Functions COUNT () Count the number of items selected. SELECT COUNT( column_name| expression) AS value_name FROM table_1, table_2 … WHERE conditions … MAX () / MIN () Return the Maximal/Minimal value in selected data. SELECT MAX/MIN( column_name| expression) AS value_name FROM table_1, table_2 … WHERE conditions … 27 SQL Functions SUM() Calculate the sum of selected values. SELECT SUM( column_name| expression) AS value_name FROM table_1, table_2 … WHERE conditions … 28 SQL Leaning resources General Introductions and Examples: W3CSchool http://www.w3schools.com/sql/ SQL reference MySQL: http://dev.mysql.com/doc/refman/5.6/en/ T-SQL (Microsoft): http://technet.microsoft.com/enus/library/ms189826(v=sql.90).aspx OracleSQL: http://docs.oracle.com/cd/B19306_01/server.102/b14200 /toc.htm 29 Q&A 30