23
Structured Query Language – SQL Dr. Moustafa Elazhary

Structured Query Language – SQL · Structured Query Language (SQL) SQL (pronounced as the letters S-Q-L or as sequel) is an abbreviation for Structured Query Language. SQL is a

  • Upload
    others

  • View
    66

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Structured Query Language – SQL · Structured Query Language (SQL) SQL (pronounced as the letters S-Q-L or as sequel) is an abbreviation for Structured Query Language. SQL is a

Structured Query Language – SQL

Dr. Moustafa Elazhary

Page 2: Structured Query Language – SQL · Structured Query Language (SQL) SQL (pronounced as the letters S-Q-L or as sequel) is an abbreviation for Structured Query Language. SQL is a

Source Copyright

The material (text and images) of these slides are mostly based on the following book: Laudon, K. C., & Laudon, J. P. (2018). Management information

systems: managing the digital firm. Pearson. Chapter Six: Foundations of BI: DB and Information Management

This material is protected by copyright laws and is not meant for distribution

WS 2018/2019 Information Systems - Information Systems, Organizations, and Strategy 2

Page 3: Structured Query Language – SQL · Structured Query Language (SQL) SQL (pronounced as the letters S-Q-L or as sequel) is an abbreviation for Structured Query Language. SQL is a

Database – Schema

Database is is a collection of data stored in some organized fashion. Table is a structured list of data of a specific type. Tables have characteristics and properties that define how data is

stored in them which include information about what data may be stored, how it is broken up, how individual pieces of information are named, and much more.

This set of information that describes a table is known as a schema, Schema are used to describe specific tables within a database, as

well as entire databases (and the relationship between tables in them, if any).

WS 2018/2019 Information Systems - Structure Query Language – SQL 3

Page 4: Structured Query Language – SQL · Structured Query Language (SQL) SQL (pronounced as the letters S-Q-L or as sequel) is an abbreviation for Structured Query Language. SQL is a

Tables

Tables are made up of columns. A column contains a particular piece of information within a table. Each column in a database has an associated datatype. A datatype defines what type of data the column can contain. Datatypes restrict the type of data that can be stored in a column (for

example, preventing the entry of alphabetical characters into a numeric field).

Data in a table is stored in rows; each record saved is stored in its own row.

Every row in a table should have some column (or set of columns) that uniquely identifies it.

WS 2018/2019 Information Systems - Structure Query Language – SQL 4

Page 5: Structured Query Language – SQL · Structured Query Language (SQL) SQL (pronounced as the letters S-Q-L or as sequel) is an abbreviation for Structured Query Language. SQL is a

Primary Key

Primary Key is a column (or set of columns) whose values uniquely identify every row in a table.

The primary key is used to refer to a specific row. Without a primary key, updating or deleting specific rows in a table

becomes extremely difficult as there is no guaranteed safe way to refer to just the rows to be affected.

Any column in a table can be established as the primary key, as long as it meets the following conditions: No two rows can have the same primary key value. Every row must have a primary key value (primary key columns

may not allow NULL values). Values in primary key columns can never be modified or updated. Primary key values can never be reused. (If a row is deleted from

the table, its primary key may not be assigned to any new rows in the future.)

WS 2018/2019 Information Systems - Structure Query Language – SQL 5

Page 6: Structured Query Language – SQL · Structured Query Language (SQL) SQL (pronounced as the letters S-Q-L or as sequel) is an abbreviation for Structured Query Language. SQL is a

Structured Query Language (SQL)

SQL (pronounced as the letters S-Q-L or as sequel) is an abbreviation for Structured Query Language.

SQL is a language designed specifically for communicating with databases.

SQL is not a proprietary language used by specific database vendors, so learning this one language will enable you to interact with just about every database you'll run into.

SQL is easy to learn as the statements are all made up of descriptive English words, and there aren't that many of them.

Despite its apparent simplicity, SQL is actually a very powerful language, and by cleverly using its language elements you can perform very complex and sophisticated database operations.

WS 2018/2019 Information Systems - Structure Query Language – SQL 6

Page 7: Structured Query Language – SQL · Structured Query Language (SQL) SQL (pronounced as the letters S-Q-L or as sequel) is an abbreviation for Structured Query Language. SQL is a

Structured Query Language (SQL)

SQL Commands

DDL

CREATEDROPALTER

TRUNCATE

DML

SELECTINSERTUPDATEDELETE

DCL

GRANTREVOKE

TCL

COMMITROLLBACKSAVEPOINT

SET TRANSACTION

WS 2018/2019 Information Systems - Structure Query Language – SQL 7

Page 8: Structured Query Language – SQL · Structured Query Language (SQL) SQL (pronounced as the letters S-Q-L or as sequel) is an abbreviation for Structured Query Language. SQL is a

DML (Data Manipulation Language)

It is the SQL commands that deals with the manipulation of data present in database and includes most of the SQL statements.

Examples of DML: SELECT

It is used to retrieve data from the a database. INSERT

It is used to insert data into a table. UPDATE

It is used to update existing data within a table. DELETE

It is used to delete records from a database table.

WS 2018/2019 Information Systems - Structure Query Language – SQL 8

Page 9: Structured Query Language – SQL · Structured Query Language (SQL) SQL (pronounced as the letters S-Q-L or as sequel) is an abbreviation for Structured Query Language. SQL is a

SQL – databases

MS SQL Server using T-SQL (Transact SQL) Oracle using PL/SQL (Procedural Language SQL) MS Access version of SQL is called JET SQL

SQL is case insensitive which means SELECT and select have same meaning in SQL statements but MySQL make difference in table names.

If you are working with MySQL, you need to use table names as they exist in the database.

WS 2018/2019 Information Systems - Structure Query Language – SQL 9

Page 10: Structured Query Language – SQL · Structured Query Language (SQL) SQL (pronounced as the letters S-Q-L or as sequel) is an abbreviation for Structured Query Language. SQL is a

RDBMS – fields and records

RDBMS stands for Relational Database Management System. RDBMS is the basis for SQL and for all modern database systems like

MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access. The data in RDBMS is stored in database objects called tables. The table is a collection of related data entries and it consists of

columns and rows. Every table is broken up into smaller entities called fields. The fields in the CUSTOMERS table consist of ID, NAME, AGE and

ADDRESS. A field is a column in a table that is designed to maintain specific

information about every record in the table. A record, also called a row of data, is each individual entry that exists

in a table and is a horizontal entity in a table.

WS 2018/2019 Information Systems - Structure Query Language – SQL 10

Page 11: Structured Query Language – SQL · Structured Query Language (SQL) SQL (pronounced as the letters S-Q-L or as sequel) is an abbreviation for Structured Query Language. SQL is a

SQL SELECT Statement – select and where

SELECT column1 FROM table_name;

SELECT column1, column2....columnN FROM table_name;

SELECT column1, column2....columnN FROM table_name WHERE CONDITION;

WS 2018/2019 Information Systems - Structure Query Language – SQL 11

Page 12: Structured Query Language – SQL · Structured Query Language (SQL) SQL (pronounced as the letters S-Q-L or as sequel) is an abbreviation for Structured Query Language. SQL is a

SQL SELECT Statement – condition, like and order by

SELECT column1, column2....columnN FROM table_name WHERE CONDITION-1 {AND|OR} CONDITION-2

SELECT column1, column2....columnN FROM table_name WHERE column_name LIKE {PATTERN};

SELECT column1, column2....columnN FROM table_name WHERE CONDITION ORDER BY column_name {ASC|DESC};

WS 2018/2019 Information Systems - Structure Query Language – SQL 12

Page 13: Structured Query Language – SQL · Structured Query Language (SQL) SQL (pronounced as the letters S-Q-L or as sequel) is an abbreviation for Structured Query Language. SQL is a

SQL CREATE TABLE and DROP TABLE

CREATE TABLE table_name( column1 datatype, column2 datatype, column3 datatype, ..... columnN datatype, PRIMARY KEY(one or more columns) );

DROP TABLE table_name;

WS 2018/2019 Information Systems - Structure Query Language – SQL 13

Page 14: Structured Query Language – SQL · Structured Query Language (SQL) SQL (pronounced as the letters S-Q-L or as sequel) is an abbreviation for Structured Query Language. SQL is a

Retrieving Individual Columns

SELECT tbl_students.student_name FROM tbl_students;

qry_students_single

The statement uses the SELECT statement to retrieve a single column called student_name from tbl_students.

The desired column name is specified right after the SELECT keyword, and the FROM keyword specifies the name of the table from which to retrieve the data.

WS 2018/2019 Information Systems - Structure Query Language – SQL 14

Page 15: Structured Query Language – SQL · Structured Query Language (SQL) SQL (pronounced as the letters S-Q-L or as sequel) is an abbreviation for Structured Query Language. SQL is a

Retrieving Multiple Columns

SELECT tbl_students.student_name, tbl_students.student_email FROM tbl_students;

qry_students_multiple

To retrieve multiple columns from a table, the same SELECT statement is used.

The only difference is that multiple column names must be specified after the SELECT keyword, and each column must be separated by a comma.

WS 2018/2019 Information Systems - Structure Query Language – SQL 15

Page 16: Structured Query Language – SQL · Structured Query Language (SQL) SQL (pronounced as the letters S-Q-L or as sequel) is an abbreviation for Structured Query Language. SQL is a

Retrieving All Columns

SELECT * FROM tbl_orders;

qry_orders_all

This is done using the asterisk (*) wildcard character in place of the actual column names.

When a wildcard (*) is specified, all the columns in the table are returned.

The column order will typically, but not always, be the physical order in which the columns appear in the table definition.

Retrieving unnecessary columns usually slows down the performance of your query and accordingly your application.

WS 2018/2019 Information Systems - Structure Query Language – SQL 16

Page 17: Structured Query Language – SQL · Structured Query Language (SQL) SQL (pronounced as the letters S-Q-L or as sequel) is an abbreviation for Structured Query Language. SQL is a

Filtering Data – WHERE CONDITION

SELECT tbl_students.student_name, tbl_students.student_zip FROM tbl_students WHERE tbl_students.student_zip = 75177;

qry_students_zip

Within a SELECT statement, data is filtered by specifying search criteria in the WHERE clause.

The WHERE clause is specified right after the table name (the FROM clause)

This example uses a simple equality test: It checks to see if a column has a specified value, and it filters the data

accordingly. However, SQL has more than just test for equality – see next slide

WS 2018/2019 Information Systems - Structure Query Language – SQL 17

Page 18: Structured Query Language – SQL · Structured Query Language (SQL) SQL (pronounced as the letters S-Q-L or as sequel) is an abbreviation for Structured Query Language. SQL is a

WHERE Clause Operators

WS 2018/2019 Information Systems - Structure Query Language – SQL 18

Page 19: Structured Query Language – SQL · Structured Query Language (SQL) SQL (pronounced as the letters S-Q-L or as sequel) is an abbreviation for Structured Query Language. SQL is a

Advanced Data Filtering – AND

SELECT tbl_courses.course_name, tbl_courses.course_category, tbl_courses.course_fee

FROM tbl_courses WHERE course_category= ‘IS' AND course_fee = 66;

In Microsoft Access, the syntax is as follows: SELECT tbl_courses.course_name,

tbl_courses.course_category, tbl_courses.course_fee FROM tbl_courses WHERE (((tbl_courses.course_category)='IS') AND

((tbl_courses.course_fee)=66));

qry_courses_and

You can specify multiple WHERE clauses in two ways: as AND clauses or as OR clauses. The OR operator instructs the database to retrieve rows that match either condition.

WS 2018/2019 Information Systems - Structure Query Language – SQL 19

Page 20: Structured Query Language – SQL · Structured Query Language (SQL) SQL (pronounced as the letters S-Q-L or as sequel) is an abbreviation for Structured Query Language. SQL is a

Using LIKE

SELECT tbl_students.student_name FROM tbl_students WHERE (((tbl_students.student_name) Like "*Wilde*"));

qry_students_like

LIKE instructs the DBMS that the following search pattern is to be compared using a wildcard match rather than a straight equality match.

Like "*Wilde*"

WS 2018/2019 Information Systems - Structure Query Language – SQL 20

Page 21: Structured Query Language – SQL · Structured Query Language (SQL) SQL (pronounced as the letters S-Q-L or as sequel) is an abbreviation for Structured Query Language. SQL is a

Sorting Data – ascendingly

SELECT tbl_students.student_name FROM tbl_students ORDER BY tbl_students.student_name;

qry_students_sort_az

To explicitly sort data retrieved using a SELECT statement, the ORDER BY clause is used.

WS 2018/2019 Information Systems - Structure Query Language – SQL 21

Page 22: Structured Query Language – SQL · Structured Query Language (SQL) SQL (pronounced as the letters S-Q-L or as sequel) is an abbreviation for Structured Query Language. SQL is a

Sorting Data – descendingly

SELECT tbl_students.student_name FROM tbl_students ORDER BY tbl_students.student_name DESC;

qry_students_sort_za

To sort by descending order, the keyword DESC must be specified. Be sure that ORDER BY clause is the last clause in your SELECT

statement. Using clauses out of order will generate an error message.

WS 2018/2019 Information Systems - Structure Query Language – SQL 22

Page 23: Structured Query Language – SQL · Structured Query Language (SQL) SQL (pronounced as the letters S-Q-L or as sequel) is an abbreviation for Structured Query Language. SQL is a

References

Laudon, K. C., & Laudon, J. P. (2018). Management information systems: managing the digital firm. Pearson.

Chapter Six: Foundations of BI: DB and Information Management

WS 2018/2019 Information Systems - IS in Global Business Today 23