Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases."— Presentation transcript:

1 SQL Basics

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

3 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.

4 What Can SQL do? Can delete records from a database. Can create new databases. Can create new tables in a database.

5 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

6 Keep in Mind That... SQL is not case sensitive

7 7 SQL Transaction control Commit Rollback DML Select Insert Update Delete DDL Create Alter Drop DCL Grant Revoke

8  Is used to change structure of database  Create Table  Drop Table  Alter Table 8 Data Definition Language (DDL)

9 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

10 The data types Ex: some popular data types used in creating fields. Data Type Name INT DOUBLE CHAR(n) VARCHAR(n) DATE TIME

11 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

12 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;

13 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

14  Used to manipulate database data 14 Data Manipulation Language (DML)

15  SELECT o Extracts data from a database  INSERT INTO o Inserts new data into a database  UPDATE o Updates data in a database  DELETE o Deletes data from a database 15 Data Manipulation Language (DML)

16 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 )

17 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')

18 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.)

19 UPDATE Statement The UPDATE statement is used to update existing records in a table. SQL Syntax –UPDATE table_name SET 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'

20 DELETE Statement The DELETE statement is used to delete rows in a table. 20 delete from Employee where Lname='Brown’

21 SELECT Statement Basic form of the SQL SELECT statement is called a mapping or a SELECT-FROM-WHERE block SELECT FROM WHERE – is a list of attribute names whose values are to be retrieved by the query – is a list of the relation names required to process the query – is a conditional (Boolean) expression that identifies the tuples to be retrieved by the query

22 Or: SELECT Statement Examples SELECT Lname,Fname FROM Person SELECT Person.Lname, Person.Fname FROM Person SELECT * FROM Person WHERE City='Sandnes'

23 LIKE Operator Example SELECT * 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

24 SQL AND & OR Operators AND operator displays records when all conditions are true. OR operator displays records when any condition is true.

25 SQL AND & OR Operators Example

26 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)

27 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)

28 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

29 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, LNAME FROM EMPLOYEE WHERESUPERSSN IS NULL

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

31 Thank You


Download ppt "SQL Basics. What is SQL? SQL stands for Structured Query Language. SQL lets you access and manipulate databases."

Similar presentations


Ads by Google