SQL LANGUAGE and Relational Data Model TUTORIAL

Slides:



Advertisements
Similar presentations
Concepts of Database Management Seventh Edition
Advertisements

Concepts of Database Management Sixth Edition
Copyright © by Royal Institute of Information Technology Introduction To Structured Query Language (SQL) 1.
Fundamentals, Design, and Implementation, 9/e COS 346 Day 11.
Introduction to Structured Query Language (SQL)
Using Relational Databases and SQL Steven Emory Department of Computer Science California State University, Los Angeles Lecture 2: Single-Table Selections.
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)
Microsoft Access 2010 Chapter 7 Using SQL.
ITS232 Introduction To Database Management Systems Siti Nurbaya Ismail Faculty of Computer Science & Mathematics, Universiti Teknologi MARA (UiTM), Kedah.
Concepts of Database Management, Fifth Edition
Chapter 5 Introduction to SQL. Structured Query Language = the “programming language” for relational databases SQL is a nonprocedural language = the user.
CPS120: Introduction to Computer Science Information Systems: Database Management Nell Dale John Lewis.
Relational DBs and SQL Designing Your Web Database (Ch. 8) → Creating and Working with a MySQL Database (Ch. 9, 10) 1.
Chapter 3 Single-Table Queries
CHAPTER:14 Simple Queries in SQL Prepared By Prepared By : VINAY ALEXANDER ( विनय अलेक्सजेंड़र ) PGT(CS),KV JHAGRAKHAND.
INLS 623– S QL Instructor: Jason Carter. SQL SELECT DISTINCT SELECT DISTINCT column_name, column_name FROM table_name ;
SQL: Data Manipulation Presented by Mary Choi For CS157B Dr. Sin Min Lee.
1 Single Table Queries. 2 Objectives  SELECT, WHERE  AND / OR / NOT conditions  Computed columns  LIKE, IN, BETWEEN operators  ORDER BY, GROUP BY,
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) Structured Query Language Introduction to SQL Structured Query Language.
Structure Query Language SQL. Database Terminology Employee ID 3 3 Last name Small First name Tony 5 5 Smith James
Using Special Operators (LIKE and IN)
BY SATHISH SQL Basic. Introduction The language Structured English Query Language (SEQUEL) was developed by IBM Corporation, Inc., to use Codd's model.
6 1 Lecture 8: Introduction to Structured Query Language (SQL) J. S. Chou, P.E., Ph.D.
Advanced SELECT Queries CS 146. Review: Retrieving Data From a Single Table Syntax: Limitation: Retrieves "raw" data Note the default formats… SELECT.
Database Fundamental & Design by A.Surasit Samaisut Copyrights : All Rights Reserved.
SQL LANGUAGE and Relational Data Model TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
Queries SELECT [DISTINCT] FROM ( { }| ),... [WHERE ] [GROUP BY [HAVING ]] [ORDER BY [ ],...]
DATA RETRIEVAL WITH SQL Goal: To issue a database query using the SELECT command.
AL-MAAREFA COLLEGE FOR SCIENCE AND TECHNOLOGY INFO 232: DATABASE SYSTEMS CHAPTER 7 (Part II) INTRODUCTION TO STRUCTURED QUERY LANGUAGE (SQL) Instructor.
SqlExam1Review.ppt EXAM - 1. SQL stands for -- Structured Query Language Putting a manual database on a computer ensures? Data is more current Data is.
A Guide to SQL, Eighth Edition Chapter Four Single-Table Queries.
Database: SQL, MySQL, LINQ and Java DB © by Pearson Education, Inc. All Rights Reserved.
IS2803 Developing Multimedia Applications for Business (Part 2) Lecture 5: SQL I Rob Gleasure robgleasure.com.
7 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel 7.6 Advanced Select Queries SQL provides useful functions that.
SQL LANGUAGE TUTORIAL Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha.
LM 5 Introduction to SQL MISM 4135 Instructor: Dr. Lei Li.
MySQL Tutorial. Databases A database is a container that groups together a series of tables within a single structure Each database can contain 1 or more.
Retrieving Information Pertemuan 3 Matakuliah: T0413/Current Popular IT II Tahun: 2007.
COM621: Advanced Interactive Web Development Lecture 11 MySQL – Data Manipulation Language.
SQL SQL Ayshah I. Almugahwi Maryam J. Alkhalifa
CHAPTER 7 DATABASE ACCESS THROUGH WEB
SQL Query Getting to the data ……..
More SQL: Complex Queries,
Chapter 5 Introduction to SQL.
Rob Gleasure robgleasure.com
MySQL DML Commands By Prof. B.A.Khivsara
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Lab 13 Databases and SQL.
 2012 Pearson Education, Inc. All rights reserved.
Basic select statement
Instructor: Craig Duckett Lecture 09: Tuesday, April 25th, 2017
ATS Application Programming: Java Programming
PL/SQL LANGUAGE MULITPLE CHOICE QUESTION SET-1
Quiz Questions Q.1 An entity set that does not have sufficient attributes to form a primary key is a (A) strong entity set. (B) weak entity set. (C) simple.
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
SQL LANGUAGE and Relational Data Model TUTORIAL
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Prof: Dr. Shu-Ching Chen TA: Yimin Yang
Prof: Dr. Shu-Ching Chen TA: Hsin-Yu Ha
Chapter 4 Summary Query.
Prof: Dr. Shu-Ching Chen TA: Haiman Tian
Introduction To Structured Query Language (SQL)
Copyright © 2012 Pearson Education, Inc. Publishing as Prentice Hall
CS4222 Principles of Database System
Structured Query Language – The Fundamentals
Introduction To Structured Query Language (SQL)
Contents Preface I Introduction Lesson Objectives I-2
Shelly Cashman: Microsoft Access 2016
Presentation transcript:

SQL LANGUAGE and Relational Data Model TUTORIAL Prof: Dr. Shu-Ching Chen TA: Sheng Guan

Outline Relational Data Model SQL Language Step1 Define the purpose Step2 Gather data, define constraints Step3 Create Relationship SQL Language http://www3.ntu.edu.sg/home/ehchua/programming/sql/relational_database_design.html

Relational Data Model (1) Relation : Organizing data into collections of two- dimensional table called “Relations” ERD  Relational Data Model  Database

Relational Data Model (2) Fields (Attributes, Columns) S_id name Email Age gpa 5000 Dave Dave@cs 19 3.3 53666 Jones Jone@cs 18 3.4 53888 Smith Smith@cs 3.2 Tuples (Records /Rows )

RD- Step1 Define the purpose Gather the requirements and define the objective of your database Drafting out the sample input forms, queries and reports, often helps.

RD Step2 Gather data, define constraints Primary Key Unique and not Null Simple Should not change Often uses Integer Other constraints Not null Positive values

RD - Step3 Create Relationship (1) Identify the relationships among tables: One-to-Many Many-to-Many One-to-One

RD - Step3 Create Relationship (2) One-to-Many The column teacherID in the child table Classes is known as the foreign key. A foreign key of a child table is a primary key of a parent table, used to reference the parent table. Take note that for every value in the parent table, there could be zero, one, or more rows in the child table. For every value in the child table, there is one and only one row in the parent table.

RD - Step3 Create Relationship (3) Many-to-Many The many-to-many relationship is, in fact, implemented as two one-to-many relationships, with the introduction of the junction table. - An order has many items in OrderDetails. An OrderDetails item belongs to one particular order. - A product may appears in many OrderDetails. Each OrderDetails item specified one product.

RD - Step3 Create Relationship (4) One-to-One

Basic Syntax of SQL Language SELECT attribute name(s) FROM table name WHERE comparison predicate (Boolean expression) GROUP BY attribute name(s) HAVING comparison predicate ORDER BY attribute name(s) SQL (Structured Query Language) is a computer language aimed to store, manipulate, and query data stored in relational databases HAVING is used when you are using an aggregate such as GROUP BY. It is used to check conditions after the aggregation takes place. WHERE is used before the aggregation takes place.

Create Tables Note that all data types use rather obvious input formats. Constants that are not simple numeric values must usually be surrounded by single quotes ('), as in the example. The date column is actually quite flexible in what it accepts, but for this tutorial we will stick to the format shown here. The syntax used so far requires you to remember the order of the columns. An alternative syntax allows you to list the columns explicitly: You can list the columns in a different order if you wish or you can even omit some columns, for example, if the precipitation is unknown: Many developers consider explicitly listing the columns better style than relying on the order implicitly.

SELECT ALL records SQL is not case sensitive. SELECT is the same as select. Tip: The asterisk (*) is a quick way of selecting all columns!

Formula Like & DISTINCT The LIKE operator is used to search for a specified pattern in a column. The "%" sign can be used to define wildcards (missing letters in the pattern) both before and after the pattern. It is also possible to select the city that does NOT contain the pattern “Sa" from the “weather" table, by using the NOT keyword. SELECT * FROM Persons WHERE City NOT LIKE '%tav%‘ % => A substitute for zero or more characters _ => A substitute for exactly one character

SELECT specific records with conditions

SQL ORDER BY 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. SELECT * FROM Persons ORDER BY LastName DESC

SQL UPDATE UPDATE table name SET column1 = value, column2 = value2,… WHERE comparison predicate (Boolean expression)

SQL IN OPERATOR AVG() - Returns the average value COUNT() - Returns the number of rows FIRST() - Returns the first value LAST() - Returns the last value MAX() - Returns the largest value MIN() - Returns the smallest value SUM() - Returns the sum The IN operator allows you to specify multiple values in a WHERE clause The MAX() function returns the largest value of the selected column SQL has many built-in functions for performing calculations on data

SQL Alias This can be a good thing to do if you have very long or complex table names or column names An alias name could be anything, but usually it is short. As you'll see from the two SELECT statements above; aliases can make queries easier to both write and to read.

SQL Joins SELECT column_name(s) FROM table_name1 JOIN_TYPES table_name2 ON table_name1.column_name = table_name2.column_name INNER JOIN: Return rows when there is at least one match in both tables LEFT JOIN: Return all rows from the left table, even if there are no matches in the right table RIGHT JOIN: Return all rows from the right table, even if there are no matches in the left table The JOIN keyword is used in an SQL statement to query data from two or more tables, based on a relationship between certain columns in these tables Tables in a database are often related to each other with keys. A primary key is a column (or a combination of columns) with a unique value for each row. Each primary key value must be unique within the table. The purpose is to bind data together, across tables, without repeating all of the data in every table

SQL INNER JOIN

SQL LEFT JOIN

SQL RIGHT JOIN

SQL GROUP BY Statement The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns. Now we want to find the total sum (total order) of each customer. We will have to use the GROUP BY statement to group the customers

SQL HAVING Clause

PosgreSQL Arrays (1) PostgreSQL allows columns of a table to be defined as variable-length multidimensional arrays. Arrays of any built-in or user-defined base type, enum type, or composite type can be created. Arrays of domains are not yet supported As shown, an array data type is named by appending square brackets ([]) to the data type name of the array elements. The above command will create a table named sal_emp with a column of type text (name), a one-dimensional array of type integer(pay_by_quarter), which represents the employee's salary by quarter, and a two-dimensional array of text (schedule), which represents the employee's weekly schedule

PosgreSQL Arrays (2) The array subscript numbers are written within square brackets. By default PostgreSQL uses a one-based numbering convention for arrays, that is, an array of n elements starts with array[1] and ends with array[n] We can also access arbitrary rectangular slices of an array, or subarrays. An array slice is denoted by writing lower-bound:upper-bound for one or more array dimensions. For example, this query retrieves the first item on Bill's schedule for the first two days of the week: If any dimension is written as a slice, i.e., contains a colon, then all dimensions are treated as slices. Any dimension that has only a single number (no colon) is treated as being from 1 to the number specified. For example, [2] is treated as [1:2]

PosgreSQL Arrays (3) An array value can be replaced completely: or using the ARRAY expression syntax: An array can also be updated at a single element or updated in a slice:

PosgreSQL Composite Types