SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and...

31
SQL Basics

Transcript of SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and...

Page 1: SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.

SQL Basics

Page 2: SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.

What is SQL?

• SQL stands for Structured Query Language .

• SQL lets you access and manipulate databases .

Page 3: SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.

What Can SQL do?

• Can execute queries against a database.

• Can retrieve data from a database. • Can insert records in a database.• Can update records in a database.

Page 4: SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.

What Can SQL do?

• Can delete records from a database.• Can create new databases. • Can create new tables in a database.

Page 5: SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.

SQL Statements

• Most of the actions you need to perform on a database are done with SQL statements.

• Ex: select all the records in the "Person" table. SELECT * FROM Person

Page 6: SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.

Keep in Mind That...

• SQL is not case sensitive

Page 7: SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.

7

SQL

SQL

Transaction control•Commit •Rollback

DML•Select•Insert•Update•Delete

DDL•Create•Alter•Drop

DCL•Grant•Revoke

Page 8: SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.

Is used to change structure of databaseIs used to change structure of database Create TableCreate Table Drop TableDrop Table Alter TableAlter Table

8

Data Definition Language (DDL)

Page 9: SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.

Create Table

• The Create table statement is used to create a table in a database and to add constraints .– Constraints:

• NOT NULL • UNIQUE • PRIMARY KEY • FOREIGN KEY • CHECK • DEFAULT

Page 10: SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.

The data types

• Ex: some popular data types used in creating fields.

Data Type Name INT

DOUBLE

CHAR(n)

VARCHAR(n)

DATE

TIME

Page 11: SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.

CREATE TABLE person( id int,

name varchar(50) not null,

email varchar(50),

age int default 18,

Dept_ID int,

primary key(id),

check(age>17),

unique(email),

foreign key (Dept_ID) REFERENCES Department(Dept_ID))

Create Table Example

Page 12: SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.

Drop Table

• Used to remove a relation (base table) and its definition

• The relation can no longer be used in queries, updates, or any other commands since its description no longer exists

• Example:DROP TABLE DEPENDENT;

Page 13: SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.

Alter Table

• Used to modify the table design like add a column, modify a column or delete a column.

• Examples:ALTER TABLE EMPLOYEE ADD JOB VARCHAR(12)

ALTER TABLE EMPLOYEE MODIFY COLUMN JOB VARCHAR(50)

ALTER TABLE EMPLOYEE DROP COLUMN JOB

Page 14: SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.

Used to manipulate database dataUsed to manipulate database data

14

Data Manipulation Language (DML)

Page 15: SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.

SELECTSELECTo Extracts data from a database

INSERT INTOINSERT INTOo Inserts new data into a database

UPDATEUPDATEo Updates data in a database

DELETEDELETEo Deletes data from a database

15

Data Manipulation Language (DML)

Page 16: SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.

INSERT INTO Statement

• First form: It doesn't specify the column names where the data will be inserted, only their values.

– The previous form the attribute values should be listed in the same order as the attributes were specified in table.

16

INSERT INTO Employee VALUES (4,'Ahmed', ‘Ali', 112233, '1965-01-01 ', '15 Ali fahmy St.Giza‘, ‘M’,1300,223344,10)

Page 17: SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.

INSERT INTO Statement, (cont.)

• Second form: It specifies both the column names and the values to be inserted

17

INSERT INTO Employee (SSN, Lname, fname)

VALUES (5, ‘Ahmed', ‘Ali')

Page 18: SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.

• To insert a new row into table with identity value, we will not have to specify a value for the identity column (a unique value will be added automatically):

insert into Department values('DP2','56733')

insert into Department(Dname,MGRSSN) values('DP2','56733')

INSERT INTO Statement, (cont.)

Page 19: SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.

UPDATE Statement

• The UPDATE statement is used to update existing records in a table.

• SQL Syntax– UPDATE table_nameSET column1=value, column2=value2,...WHERE some_column=some_value

• Note: The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!

UPDATE Person SET Address='18 Abaas El 3akaad

St. Nasr City.Cairo', City='Cairo' WHERE Lname=‘Amr' AND Fname='Ali'

Page 20: SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.

DELETE Statement

• The DELETE statement is used to delete rows in a table.

20

delete from Employee

delete from Employee

where Lname='Brown’

Page 21: SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.

SELECT Statement

• Basic form of the SQL SELECT statement is called a mapping or a SELECT-FROM-WHERE blockSELECT <attribute list>FROM <table list>WHERE <condition>– <attribute list> is a list of attribute names whose

values are to be retrieved by the query– <table list> is a list of the relation names required

to process the query– <condition> is a conditional (Boolean) expression

that identifies the tuples to be retrieved by the query

Page 22: SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.

Or:

SELECT Statement Examples

SELECT Lname,Fname FROM Person

SELECT Person.Lname, Person.Fname FROM Person

SELECT * FROM Person WHERE City='Sandnes'

Page 23: SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.

LIKE Operator ExampleSELECT * FROM Person WHERE City LIKE 's%'

SELECT * FROM Person WHERE City LIKE '%s'

SELECT * FROM Person WHERE City LIKE '_andnes'

Used to represent one character

Page 24: SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.

SQL AND & OR Operators• AND operator displays records when all

conditions are true.

• OR operator displays records when any condition is true.

Page 25: SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.

SQL AND & OR Operators Example

Page 26: SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.

IN Operator Example

• Retrieve data of all employees whose social security numbers number is 112233, or 512463.– Query : SELECT * FROM Employee WHERE SSN IN

(112233,512463)

Page 27: SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.

IN Operator Example

• Retrieve data of all employees whose social security numbers number is not 112233, or 512463.– Query :

SELECT * FROM Employee WHERE SSN not IN (112233,512463)

Page 28: SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.

The BETWEEN Operator

• The BETWEEN operator is used in a WHERE clause to select a range of data between two values.

• Retrieve the employees with salary between 1000 and 2000 – Query : SELECT * FROM EMPLOYEE WHERE SALARY BETWEEN 1000 AND 2000

Page 29: SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.

NULLS IN SQL QUERIES

• SQL allows queries that check if a value is NULL.• SQL uses IS or IS NOT to compare Nulls.• Retrieve the names of all employees who do not

have supervisors.– Query :

SELECT FNAME, LNAMEFROM EMPLOYEEWHERE SUPERSSN IS NULL

Page 30: SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.

DISTINCT Keyword

• The keyword DISTINCT is used to eliminate duplicate tuples in a query result.

Page 31: SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases.

Thank YouThank You