SQL Structured Query Language 1 Başar Öztayşi 2017
SQL SQL is an ANSI (American National Standards Institute) standard computer language for accessing and manipulating database systems. SQL works with database programs like MS Access, DB2, Informix, MS SQL Server, Oracle, Sybase, etc. 2 Başar Öztayşi 2017
There are different versions of the SQL language, but they must support the same major keywords in a similar manner (such as SELECT, UPDATE, DELETE, INSERT, WHERE, and others) with SQL standards Most of the SQL database programs also have their own extensions in addition to the SQL standard! 3 Başar Öztayşi 2017
SQL Database Tables Person LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit Bohçacı Berke Çeşme Sokak 4/1 İzmit Sungur Ayşegül Tepe Sokak 8/23 Balıkesir 4 Başar Öztayşi 2017
SQL Queries With SQL, we can query a database and have a result set returned. A query like this: SELECT LastName FROM Person Gives a result set like this: LastName Kiraz Bohçacı Sungur 5 Başar Öztayşi 2017
SQL can be divided into two parts: The Data Manipulation Language (DML) The Data Definition Language (DDL). 6 Başar Öztayşi 2017
Data Manipulation Language SQL (Structured Query Language) is a syntax for executing queries. But the SQL language also includes a syntax to update, insert, and delete records. SELECT - extracts data from a database table UPDATE - updates data in a database table DELETE - deletes data from a database table INSERT INTO - inserts new data into a database table 7 Başar Öztayşi 2017
Data Definition Language The Data Definition Language (DDL) part of SQL permits database tables to be created or deleted. We can also define indexes (keys), specify links between tables, and impose constraints between database tables. The most important DDL statements in SQL are: CREATE TABLE - creates a new database table ALTER TABLE - alters (changes) a database table DROP TABLE - deletes a database table CREATE INDEX - creates an index (search key) DROP INDEX - deletes an index 8 Başar Öztayşi 2017
SQL The SELECT Statement 9 Başar Öztayşi 2017
SQL The SELECT Statement The SELECT statement is used to select data from a table. The tabular result is stored in a result table (called the result-set). Syntax SELECT column_name(s) FROM table_name 10 Başar Öztayşi 2017
LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit Bohçacı Berke Çeşme Sokak 4/1 İzmit Sungur Ayşegül Tepe Sokak 8/23 Balıkesir To select the columns named "LastName" and "FirstName", use a SELECT statement like this: SELECT LastName, FirstName FROM Person LastName Kiraz Bohçacı Sungur FirstName Meltem Berke Ayşegül 11 Başar Öztayşi 2017
Select All Columns To select all columns from the "Person" table, use a * symbol instead of column names, like this: SELECT * FROM Person LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit Bohçacı Berke Çeşme Sokak 4/1 İzmit Sungur Ayşegül Tepe Sokak 8/23 Balıkesir 12 Başar Öztayşi 2017
Select Distinct Statement The DISTINCT keyword is used to return only distinct (different) values. The SELECT statement returns information from table columns. But what if we only want to select distinct elements? With SQL, all we need to do is to add a DISTINCT keyword to the SELECT statement: Syntax SELECT DISTINCT column_name(s) FROM table_name 13 Başar Öztayşi 2017
Using Distinct Keyword To select ALL values from the column named "Company" we use a SELECT statement like this: SELECT Company FROM Orders Orders Company OrderNumber SONY 3412 ACME 5464 VESTEL 5876 ACME 3425 Company SONY ACME VESTEL ACME 14 Başar Öztayşi 2017
Note that "W3Schools" is listed twice in the result-set. To select only DIFFERENT values from the column named "Company" we use a SELECT DISTINCT statement like this: SELECT DISTINCT Company FROM Orders Orders Company OrderNumber SONY 3412 ACME 5464 VESTEL 5876 ACME 3425 Company SONY ACME VESTEL 15 Başar Öztayşi 2017
Select All Columns The WHERE clause is used to specify a selection criterion. The WHERE Clause To conditionally select data from a table, a WHERE clause can be added to the SELECT statement. Syntax SELECT column FROM table WHERE column operator value 16 Başar Öztayşi 2017
Operators With the WHERE clause, the following operators can be used: Operator Description = Equal <> Not equal > Greater than < Less than >= Greater than or equal <= Less than or equal BETWEEN LIKE IN Between an inclusive range Search for a pattern If you know the exact value you want to return for at least one of the columns 17 Başar Öztayşi 2017
Using the WHERE Clause To select only the persons living in the city "İzmit", we add a WHERE clause to the SELECT statement: SELECT * FROM Person WHERE City= İzmit' LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit Bohçacı Berke Çeşme Sokak 4/1 İzmit Sungur Ayşegül Tepe Sokak 8/23 Balıkesir Bohçacı Nilgün Kahya Sokak 3/65 İzmit LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit Bohçacı Berke Çeşme Sokak 4/1 İzmit Bohçacı Nilgün Kahya Sokak 3/65 İzmit 18 Başar Öztayşi 2017
Using Quotes SQL uses single quotes around text values Numeric values should not be enclosed in quotes. For Text Valuıes This is correct: SELECT * FROM Person WHERE FirstName= Ayşe This is wrong: SELECT * FROM Person WHERE FirstName=Ayşe For Numeric Values This is correct: SELECT * FROM Person WHERE Year=1985 This is wrong: SELECT * FROM Person WHERE Year= 1985 19 Başar Öztayşi 2017
The LIKE Condition The LIKE condition is used to specify a search for a pattern in a column. Syntax SELECT column FROM table WHERE column LIKE pattern A "%" sign can be used to define wildcards (missing letters in the pattern) both before and after the pattern. 20 Başar Öztayşi 2017
Using LIKE The following SQL statement will return persons with first names that start with an 'O': SELECT * FROM Person WHERE FirstName LIKE 'O%' The following SQL statement will return persons with first names that end with an 'a': SELECT * FROM Person WHERE FirstName LIKE '%a' 21 Başar Öztayşi 2017
Using LIKE 2 The following SQL statement will return persons with first names that contain the pattern 'la': SELECT * FROM Person WHERE FirstName LIKE '%la%' 22 Başar Öztayşi 2017
SQL The INSERT INTO Statement 23 Başar Öztayşi 2017
The INSERT INTO Statement The INSERT INTO statement is used to insert new rows into a table. Syntax INSERT INTO table_name VALUES (value1, value2,...) You can also specify the columns for which you want to insert data: INSERT INTO table_name (column1, column2,...) VALUES (value1, value2,...) 24 Başar Öztayşi 2017
Insert a New Row LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit INSERT INTO Person VALUES ( Arslan', Hüseyin', Karga Sok 3/23', Balıkesir') LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit Arslan Hüseyin Karga Sok 3/23 Balıkesir 25 Başar Öztayşi 2017
Insert Data in Specified Columns LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit Arslan Hüseyin Karga Sok 3/23 Balıkesir And This SQL statement: INSERT INTO Person (LastName, Address) VALUES ( Kibar', 'Terzi Sok 1/3') LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit Arslan Hüseyin Karga Sok 3/23 Balıkesir Kibar Terzi Sok 1/3 26 Başar Öztayşi 2017
SQL The UPDATE Statement 27 Başar Öztayşi 2017
The Update Statement The UPDATE statement is used to modify the data in a table. Syntax UPDATE table_name SET column_name = new_value WHERE column_name = some_value 28 Başar Öztayşi 2017
Update one Column in a Row LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit Arslan Hüseyin Karga Sok 3/23 Balıkesir Kibar Terzi Sok 1/3 We want to add a first name to the person with a last name of "Kibar": UPDATE Person SET FirstName = Nehar' WHERE LastName = Kibar' LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit Arslan Hüseyin Karga Sok 3/23 Balıkesir Kibar Nehar Terzi Sok 1/3 29 Başar Öztayşi 2017
Update several Columns in a Row LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit Arslan Hüseyin Karga Sok 3/23 Balıkesir Kibar Nehar Terzi Sok 1/3 We want to change the address and add the name of the city: UPDATE Person SET Address = Keresteci Sok 1/7', City = İzmit WHERE LastName = Kibar' LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit Arslan Hüseyin Karga Sok 3/23 Balıkesir Kibar Nehar Keresteci Sok 1/7 İzmit 30 Başar Öztayşi 2017
SQL The Delete Statement 31 Başar Öztayşi 2017
The Delete Statement The DELETE statement is used to delete rows in a table. Syntax DELETE FROM table_name WHERE column_name = some_value 32 Başar Öztayşi 2017
Delete a Row LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit Arslan Hüseyin Karga Sok 3/23 Balıkesir Kibar Nehar Terzi Sok 1/3 İzmit "Nehar Kibar" is going to be deleted: DELETE FROM Person WHERE LastName = Kibar' LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit Arslan Hüseyin Karga Sok 3/23 Balıkesir 33 Başar Öztayşi 2017
LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit Arslan Hüseyin Karga Sok 3/23 Balıkesir Kibar Nehar Terzi Sok 1/3 İzmit LastName FirstName Address City Kiraz Meltem Paşa Sokak 1/23 İzmit Arslan Hüseyin Karga Sok 3/23 Balıkesir Kibar Nehar Terzi Sok 1/3 34 Başar Öztayşi 2017
Create Table Creating a basic table involves naming the table and defining its columns and each column's data type CREATE TABLE table_name( column1 datatype, column2 datatype, column3 datatype,... columnn datatype, PRIMARY KEY( one or more columns ) ); 35 Başar Öztayşi 2017
Create Table CREATE TABLE Student ( ST_ID int PRIMARY KEY, Name CHAR(255), Course CHAR(255), Marks INTEGER, Grade CHAR(255), Phone INTEGER, Present YESNO ) 36 Başar Öztayşi 2017
Drop Table The SQL DROP TABLE statement is used to remove a table definition and all the data, indexes, triggers, constraints and permission specifications for that table. DROP TABLE table_name; 37 Başar Öztayşi 2017
Alter Table The SQL ALTER TABLE command is used to add, delete or modify columns in an existing table. You should also use the ALTER TABLE command to add and drop various constraints on an existing table. 38 Başar Öztayşi 2017
Add Column The basic syntax of an ALTER TABLE command to add a New Column in an existing table is as follows. ALTER TABLE table_name ADD column_name datatype; ALTER TABLE Student ADD SurName CHAR(255); 39 Başar Öztayşi 2017
Drop Column The basic syntax of an ALTER TABLE command to DROP COLUMN in an existing table is as follows. ALTER TABLE table_name DROP COLUMN column_name; ALTER TABLE Student Drop Column SurName; 40 Başar Öztayşi 2017
Modify Column The basic syntax of an ALTER TABLE command to change the DATA TYPE of a column in a table is as follows. ALTER TABLE table_name MODIFY COLUMN column_name datatype; 41 Başar Öztayşi 2017
Not Null Constraint The basic syntax of an ALTER TABLE command to add a NOT NULL constraint to a column in a table is as follows. ALTER TABLE table_name MODIFY column_name datatype NOT NULL; 42 Başar Öztayşi 2017
Inner Join SELECT table1.column1, table2.column2... FROM table1 INNER JOIN table2 ON table1.common_field = table2.common_field The INNER JOIN creates a new result table by combining column values of two tables (table1 and table2) based upon the joinpredicate. The query compares each row of table1 with each row of table2 to find all pairs of rows which satisfy the join-predicate. When the join-predicate is satisfied, column values for each matched pair of rows of A and B are combined into a result row. 43 Başar Öztayşi 2017
Left Join The SQL LEFT JOIN returns all rows from the left table, even if there are no matches in the right table. This means that if the ON clause matches 0 (zero) records in the right table; the join will still return a row in the result, but with NULL in each column from the right table. This means that a left join returns all the values from the left table, plus matched values from the right table or NULL in case of no matching join predicate SELECT table1.column1, table2.column2... FROM table1 LEFT JOIN table2 ON table1.common_field = table2.common_field; 44 Başar Öztayşi 2017
Right Join SELECT table1.column1, table2.column2... FROM table1 RIGHT JOIN table2 ON table1.common_field = table2.common_field; The SQL RIGHT JOIN returns all rows from the right table, even if there are no matches in the left table. This means that if the ON clause matches 0 (zero) records in the left table; the join will still return a row in the result, but with NULL in each column from the left table. This means that a right join returns all the values from the right table, plus matched values from the left table or NULL in case of no matching join predicate. 45 Başar Öztayşi 2017