Presentation is loading. Please wait.

Presentation is loading. Please wait.

ITEC 3220A Using and Designing Database Systems Instructor: Gordon Turpin Course Website: Office: CSEB3020.

Similar presentations

Presentation on theme: "ITEC 3220A Using and Designing Database Systems Instructor: Gordon Turpin Course Website: Office: CSEB3020."— Presentation transcript:

1 ITEC 3220A Using and Designing Database Systems Instructor: Gordon Turpin Course Website: Office: CSEB3020

2 Chapter 7 Introduction to Structured Query Language (SQL)

3 3 Introduction to SQL SQL functions fit into two broad categories: –Data definition language SQL includes commands to create –Database objects such as tables, indexes, and views –Commands to define access rights to those database objects –Data manipulation language Includes commands to insert, update, delete, and retrieve data within the database tables

4 4 SQL Data Definition Commands

5 5 Data Manipulation Commands

6 6 Creating the Database Two tasks must be completed –create the database structure –create the tables that will hold the end- user data First task –RDBMS creates the physical files that will hold the database –Tends to differ substantially from one RDBMS to another

7 7 Data Types CHAR (n): Character string n characters long DATE: Dates in the form DD-MON-YYYY OR MM/DD/YYYY DECIMAL (p, q): Decimal number p digits long with q if these being decimal places to the right of the decimal points INTEGER: Range from -2147483648 to 2147483647 SMALLINT: Similar to INTEGER but does not occupy as much space. It ranges from -32768 to 32767 NULL

8 8 Creating Table Structures Use one line per column (attribute) definition Use spaces to line up the attribute characteristics and constraints Table and attribute names are capitalized NOT NULL specification UNIQUE specification Primary key attributes contain both a NOT NULL and a UNIQUE specification RDBMS will automatically enforce referential integrity for foreign keys Command sequence ends with a semicolon

9 9 Table Creation Steps Steps in table creation: 1.Identify data types for attributes 2.Identify columns that can and cannot be null 3.Identify columns that must be unique 4.Identify primary key-foreign key mates 5.Determine default values 6.Identify constraints on columns (domain specifications) 7.Create the table CREATE TABLE ( );

10 10 An Example CREATE TABLE STUDENT( 2 STU_NUM INTEGER NOT NULL UNIQUE, 3 STU_NAME VARCHAR(15) NOT NULL, 4 GPA DECIMAL(3,2) NOT NULL, 5 PRIMARY KEY(STU_NUM)); Table created. Define attributes and their data types Semicolon indicates end of command Message indicates table was created Comma indicates end of description of an attribute Not null specifications Unique specifications Identify primary key

11 11 SQL Integrity Constraints Adherence to entity integrity and referential integrity rules is crucial –Entity integrity enforced automatically if primary key specified in CREATE TABLE command sequence –Referential integrity can be enforced in specification of FOREIGN KEY –Other specifications to ensure conditions met: ON DELETE RESTRICT ON UPDATE CASCADE

12 12 Advanced Data Definition Commands All changes in the table structure are made by using the ALTER command –Followed by a keyword that produces specific change –Three options are available ADD MODIFY DROP

13 13 Changing a Column’s Data Type ALTER can be used to change data type Some RDBMSs (such as Oracle) do not permit changes to data types unless the column to be changed is empty

14 14 Changing a Column’s Data Characteristics Use ALTER to change data characteristics If the column to be changed already contains data, changes in the column’s characteristics are permitted if those changes do not alter the data type

15 15 Adding or Dropping a Column Use ALTER to add a column –Do not include the NOT NULL clause for new column Use ALTER to drop a column –Some RDBMSs impose restrictions on the deletion of an attribute

16 16 Data Manipulation Commands Adding table rows Saving table changes Listing table rows Updating table rows Restoring table contents Deleting table rows Inserting table rows with a select subquery

17 17 Common SQL Data Manipulation Commands

18 18 Data Entry Enter data into a table INSERT INTO VALUES (attribute 1 value, attribute 2 value, … etc.);

19 19 Listing Table Rows SELECT –Used to list contents of table Syntax –SELECT columnlist FROM tablename Columnlist represents one or more attributes, separated by commas Asterisk can be used as wildcard character to list all attributes

20 20 Updating Table Rows UPDATE –Modify data in a table Syntax –UPDATE tablename SET columnname = expression [, columname = expression] [WHERE conditionlist]; If more than one attribute is to be updated in the row, separate corrections with commas

21 21 Saving Table Changes Changes made to table contents are not physically saved on disk until –Database is closed –Program is closed –COMMIT command is used Syntax –COMMIT –Will permanently save any changes made to any table in the database

22 22 Restoring Table Contents ROLLBACK –Used to restore the database to its previous condition –Only applicable if COMMIT command has not been used to permanently store the changes in the database Syntax –ROLLBACK; COMMIT and ROLLBACK only work with data manipulation commands that are used to add, modify, or delete table rows

23 23 Deleting Table Rows DELETE –Deletes a table row Syntax –DELETE FROM tablename [WHERE conditionlist ]; WHERE condition is optional If WHERE condition is not specified, all rows from the specified table will be deleted

24 24 Inserting Table Rows with a Select Subquery INSERT –Inserts multiple rows from another table (source) –Uses SELECT subquery Query that is embedded (or nested) inside another query Executed first Syntax –INSERT INTO tablename SELECT columnlist FROM tablename

25 25 Selecting Rows with Conditional Restrictions Select partial table contents by placing restrictions on rows to be included in output –Add conditional restrictions to the SELECT statement, using WHERE clause Syntax –SELECT columnlist FROM tablelist [ WHERE conditionlist ] ;

26 26 Comparison Operators

27 27 Arithmetic Operators: The Rule of Precedence Perform operations within parentheses Perform power operations Perform multiplications and divisions Perform additions and subtractions

28 28 Special Operators BETWEEN –Used to check whether attribute value is within a range IS NULL –Used to check whether attribute value is null LIKE –Used to check whether attribute value matches a given string pattern IN –Used to check whether attribute value matches any value within a value list EXISTS –Used to check if a subquery returns any rows

29 29 More Complex Queries and SQL Functions Listing unique values –DISTINCT clause produces list of different values Aggregate functions –Mathematical summaries SELECT DISTINCT V_CODE FROM PRODUCT;



32 32 More Complex Queries and SQL Functions (Cont’d) Ordering a listing Results ascending by default –Descending order uses DESC Cascading order sequence ORDER BY ORDER BY DESC ORDER BY

33 33 More Complex Queries and SQL Functions (cont’d) Grouping data –Creates frequency distributions –Only valid when used with SQL arithmetic functions –HAVING clause operates like WHERE for grouping output SELECT P_SALECODE, MIN(P_PRICE) FROM PRODUCT_2 GROUP BY P_SALECODE; SELECT V_CODE,COUNT(DISTINCT(P_CODE)),AVG(P_PRICE) FROM PRODUCT_2 GROUP BY V_CODE HAVING AVG(P_PRICE) < 10;

34 34 SQL Exercise Write SQL code that will create the relations shown. Assume the following attribute data types: –Student_ID: integer –Student_Name: 25 characters –Faculty_ID: integer –Faculty_Name: 25 characters –Course_ID: 25 characters –Course_Name: 15 characters –Date_Qualified: date –Section_ID: integer –Semester: 7 characters

35 35 SQL Exercise (Cont’d) STUDENT (Primary key: Student_ID) Student_ ID Student_ Name 38214Letersky 54907Altvater 66324Aiken 70542Marra IS_QUALIFIED (Primary key: Faculty_ID, Course_ID) Faculty_ ID Course_I D Date_ Qualified 2143ISM31129/1988 3467ISM42129/1995 3467ISM49309/1996 4756ISM31139/1991 4756ISM31129/1991

36 36 SQL Exercise (Cont’d) FACULTY (Primary key: Faculty_ID) Faculty_I D Faculty_Name 2143Birkin 3467Berndt 4756Collins SECTION (Primary key: Section_ID) Section_IDCourse_ID 2712ISM3113 2713ISM3113 2714ISM4212 2715ISM4930

37 37 SQL Exercise (Cont’d) COURSE ((Primary key: Course_ID) Course_IDCourse_ Name ISM3113Syst Analysis ISM3112Syst Design ISM4212Database ISM4930Networking IS_REGISTERED (Primary key: Student_ID, Section_ID) Student_I D Section_I D Semester 382142714I - 2001 549072714I - 2001 549072715I - 2001 663242713I - 2001

38 38 SQL Exercise (Cont’d) Write SQL queries to answer the following questions: –Display the course ID and course name for all courses with an ISM prefix. –Is any instructor qualified to teach ISM 3113 and not qualified to teach ISM 4930? –How many students are enrolled in section 2714 during semester I – 2001? – Which students were not enrolled in any courses during semester I – 2001?

39 39 Summary SQL commands can be divided into two overall categories: –Data definition language commands –Data manipulation language commands Basic data definition commands allow you to create tables, indexes, and views Many SQL constraints can be used with columns Aggregate functions –Special functions that perform arithmetic computations over a set of rows

40 40 Summary ( Cont’d ) ORDER BY clause –Used to sort output of a SELECT statement –Can sort by one or more columns and use either an ascending or descending order Join output of multiple tables with SELECT statement Natural join uses join condition to match only rows with equal values in specified columns

41 41 Lab Instruction Before you go to the lab sessions, please use your Passport York to create Acadlab account (NOVELL account) and AML account if you don't have them yet. These two accounts will allow you to get access to the ORACLE database. How to get access to Oracle in ITEC labs –Log into the workstations using your ACADLAB account. –Choose Oracle from programs under the start menu and then choose sqlplus –When prompted for the username/password enter your_username@studb (where your_username is your AML username) at the username prompt and your AML password at the password prompt.

42 42 Lab Instruction How to get access to Oracle at home –Login to –At the prompt type the following commands: source /javainit –Start Oracle SQL*PLUS environment by typing the following command: sqlplus –When prompted for the username/password enter your_username@studb (where your_username is your AML username) at the username prompt and your AML password at the password prompt.

43 43 Lab Content Practice Question 2 of Assignment Two. –Create table structure for each table –Fill the tables with the data –Run the SQL queries –Print your queries and the answers

44 44 Lab Tips To list all tables you have in your Oracle account use the following SQL command: select table_name from user_tables; To describe a given Oracle table use the following Oracle environment command (note that this is not an SQL command): desc tablename (where tablename is the name of the table that you have in your account)

Download ppt "ITEC 3220A Using and Designing Database Systems Instructor: Gordon Turpin Course Website: Office: CSEB3020."

Similar presentations

Ads by Google