<<

CSC309 Tutorial: SQL

TA: Lei Jiang March 24, 2003 Outline

§ SQL: Overview

§ SQL:

§ SQL: Queries

§ The Pointbase SQL: Overview

§ SQL (Structured )

• is a standard language for RDBMS.

• allows user to create new and modify existing

• allows user to pose complex queries to the database and retrieve data

§ Some common RDBMS that support SQL: Oracle, Sybase, MS SQL Server, Access, IBM DB2, etc.

§ In this course, we use the Pointbase database. http://www.pointbase.com SQl: Data Definition Language

§ Use SQL DDL to create or tables. We can also define indexes (keys), specify links between tables, and impose constraints between database tables.

The most important DDL statements in SQL are: CREATE -creates a new database table ALTER TABLE -alters (changes) a database table DROP TABLE -deletes a database table CREATE INDEX -creates an index (search key) DROP INDEX-deletes an index SQL: CREATE

§ Create a table: Syntax: CREATE TABLE tbl_name( col1 data_type,col2 data_type, ... ) Example: CREATE TABLE employee ( ID int, Name varchar(30), Title varchar(20), Dept varchar(20), Salary decimal(10,2) ) Data types: may vary from system to system Data Type Description integer(size) Hold integers only. The maximum number of digits are specified in int(size) parenthesis. smallint(size) tinyint(size) decimal(size,d) Hold numbers with fractions. The maximum number of digits are numeric(size,d) specified in "size". The maximum number of digits to the right of the decimal is specified in "d". char(size) Holds a fixed length string (can contain letters, numbers, and special characters). The fixed size is specified in parenthesis. varchar(size) Holds a variable length string (can contain letters, numbers, and special characters). The maximum size is specified in parenthesis. date(yyyymmdd) Holds a date

SQL: DROP, ALTER

§ Alter a table (Add/Delete Columns)

Syntax: ALTER TABLE table_name ADD column_name datatype Example: ALTER TABLE employee ADD Address varchar

Syntax: ALTER TABLE table_name DROP column_name Note: Some systems may not support dropping columns of a table

§ Destroy a table

DROP TABLE table_name SQL: INSERT

§ Insert data to the table

insert an entire Example: INSERT INTO employee VALUES (111, ‘John’, ‘programmer’, ‘testing’, 50000.0)

insert data to only specific columns Example: INSERT INTO employee (id, name, title) VALUES (211, ‘Tom’, ‘manager’)

ID NAME TITLE DEPT SALARY

111 Johnprogrammer testing 50000.00 211 Tommanager-- SQL: UPDATE, DELETE

§ Update the table with new data Syntax: UPDATE table_name SET column_name = new_value WHERE column_name = some_value Example: UPDATE employee SET salary=60000.0 WHERE name=‘John’

§ Delete data from the table Syntax:DELETE FROM table_name WHERE column_name =some_value Example: DELETE FROM employee WHERE name=‘Tom’ SQL: SELECT

SELECT [ALL | DISTINCT] column1[,column2] FROM table1[,table2] [WHERE "conditions"] [GROUP BY "column-list"] [HAVING "conditions] [ORDER BY "column-list" [ASC | DESC] ]

Example: comparison operators SELECT * = Equal FROM employee > Greater than WHERE salary>50000.0 < Less than >= Greater than or equal to SELECT * <= Less than or equal to FROM employee <> or != Not equal to WHERE Name LIKE ‘jo%’ LIKE String comparison test

SQL: Aggregation Operators

MIN returns the smallest value in a given column MAX returns the largest value in a given column SUM returns the sum of the numeric values in a given column AVG returns the average value of a given column COUNT returns the total number of values in a given column COUNT(*) returns the number of rows in a table

n SELECT AVG(salary) FROM employee WHERE title = 'Programmer‘

n SELECT COUNT(*) FROM employee SQL: GROUP BY

The GROUP BY clause will gather all of the rows together that contain data in the specified column(s) and will allow aggregate functions tobe performed on the one or more columns.

SELECT dept, MIN(salary) AS MINIMUM FROM employee GROUP BY dept

Original table: Result:

NAME TITLE DEPT__SALARY___ DEPT MINIMUM Johnprogmer150000.00 1 40000.00 Tommanager280000.00 2 80000.00 Bobprogmer 140000.00 330000.00 David tester330000.00 SQL: HAVING

The HAVING clause allows you to specify conditions on the rows for each group -in other words, which rows should be selected will be based on the conditions you specify. The HAVING clause should follow the GROUP BY clause.

SELECT dept, MIN(salary) AS MIN_SALARY FROM employee GROUP BY dept HAVING MIN(salary)>=40000.0

Original table: Result:

NAME TITLE DEPT__SALARY___ DEPT MIN_SALARY Johnprogmer150000.00 1 40000.00 Tommanager280000.00 2 80000.00 Bobprogmer 140000.00 David tester330000.00 SQL: ORDER BY

ORDER BY allows you to display the results of your query in a sorted order (either ascending order (ASC) or descending order (DESC) based on the columns that you specify to order by. Default is by ASC.

SELECT dept, MIN(salary) AS MIN_SALARY FROM employee GROUP BY dept HAVING MIN(salary)>=40000.0 ORDER BY dept DESC

Original table: Result:

NAME TITLE DEPT__SALARY___ DEPT MINAGE Johnprogmer150000.00 2 80000.00 Tommanager280000.00 140000.00 Bobprogmer 140000.00 David tester330000.00 SQL: Boolean Operators

The AND operator can be used to two or more conditions in the WHERE clause. Both sides of the AND condition must be true in order for the condition to be met and for those rows to be displayed.

SELECT * FROM employee WHERE salary>=50000 AND title=‘programmer’

The OR operator can be used to join two or more conditions in the WHERE clause also. However, either side of the OR operator can be true and the condition will be met

SELECT * FROM employee WHERE title=‘tester’ OR title=‘programmer’ SQL: IN, BETWEEN

The IN conditional operator is a set membership test operator. It is used to test whether or not a value (stated before the keyword IN) is"in" the list of values provided after the keyword IN.

SELECT * FROM employee WHERE name IN (‘John', 'Roberts', 'Ruiz');

The BETWEEN conditional operator is used to test whether or not a value (stated before the keyword BETWEEN) is "between" the two values stated after the keyword BETWEEN.

SELECT * FROM employee WHERE salary BETWEEN 40000 AND 60000; SQL: Table Join

Queries involve more than one table.

Table employee: Table: contact

ID NAME TITLE DEPT_____ ID ADDRESS PHONENUM 111 John programmer 1 111 23 Yonge ST (416) 123-4567 211 Tom manager 2 112 34 YongeST (416) 223-4567 112 Bob programmer 1 211 45 Yonge ST (416) 143-4567 311 David tester 3 311 56 YongeST (416) 153-4567

SELECT name, address, phonenum Result: FROM employee, contact WHERE employee.id=contact.id NAME ADDRESS PHONENUM John 23 YongeST (416) 123-4567 AND employee.title=‘programmer’ Bob 34 YongeST (416) 223-4567 The Pointbase Database qPointbaseis a full-featured, written . CDF Resource: http://www.cdf.toronto.edu/~t2aboels/#JDBC

qYou can start PointBasein three different ways: Using PointBaseCommander Using PointBaseConsole Using a JDBC Application

qUsing PointbaseDatabase on CDF

§ Create .ini file to tell pointbase where to store the database A typicall pointbase.ini will have a line: database.home=

§ To start pointbase commander: java –cp .:/h/u2/csc309h/lib/poitbase/pbembedded41ev.jar:/h/u2/csc309h/lib/pointbase/pbtools41ev.jar com.poitbase.tools.toolsCommander

§ You can then issue any SQL command to interact with the DB, for example, to check that pointbase was installed successfully, type “select tablename from systables”

§ More examples at http://www.cdf.toronto.edu/~t2aboels/resources/JDBC/pointbase/example/