75
EC601 DATABASE SYSTEM 4.1 Do database design using SQL commands 4.2 Learn SQL Server/Open source Software TOPIC 4 Structured Query Language

EC601 DATABASE SYSTEM TOPIC 4.pptx

Embed Size (px)

Citation preview

EC601 DATABASE SYSTEM

EC601 DATABASE SYSTEM4.1 Do database design using SQL commands 4.2 Learn SQL Server/Open source Software

TOPIC 4 Structured Query Language

STRUCTURED QUERY LANGUAGEIntroduction to SQL

What is SQL?SQL stands for Structured Query LanguageSQL lets you access and manipulate databasesSQL is an ANSI (American National Standards Institute) standard4.1 Do database design using SQL commands

STRUCTURED QUERY LANGUAGESQL Syntax

SQL can be divided into two parts: Data Manipulation Language (DML)Data Definition Language (DDL).4.1 Do database design using SQL commands

STRUCTURED QUERY LANGUAGESQL Syntax

The query and update commands form the DML part of SQL:

SELECT - extracts data from a databaseUPDATE - updates data in a databaseDELETE - deletes data from a databaseINSERT INTO - inserts new data into a database4.1 Do database design using SQL commands

STRUCTURED QUERY LANGUAGESQL Syntax

The DDL part of SQL permits database tables to be created or deleted.

CREATE DATABASE - creates a new databaseCREATE TABLE - creates a new tableALTER TABLE - modifies a tableDROP TABLE - deletes a tableDROP DATABASE - deletes a database

4.1 Do database design using SQL commands

STRUCTURED QUERY LANGUAGE1.0 DDL statements in SQL

CREATE DATABASE - creates a new databaseCREATE TABLE - creates a new tableALTER TABLE - modifies a tableDROP TABLE - deletes a tableDROP DATABASE - deletes a database

4.1 Do database design using SQL commands

STRUCTURED QUERY LANGUAGECREATE DATABASE Statement

The CREATE DATABASE statement is used to create a database.

SQL CREATE DATABASE Syntax

CREATE DATABASE dbname;4.1 Do database design using SQL commands

STRUCTURED QUERY LANGUAGECREATE DATABASE Example

The following SQL statement creates a database called "my_db":

CREATE DATABASE my_db;

4.1 Do database design using SQL commands

STRUCTURED QUERY LANGUAGECREATE TABLE Statement

The CREATE TABLE statement is used to create a table in a database.

Tables are organized into rows and columns; and each table must have a name.4.1 Do database design using SQL commands

STRUCTURED QUERY LANGUAGECREATE TABLE Syntax

CREATE TABLE table_name(column_name1 data_type(size),column_name2 data_type(size),column_name3 data_type(size),....);4.1 Do database design using SQL commands

STRUCTURED QUERY LANGUAGECREATE TABLE Syntax

The column_name parameters specify the names of the columns of the table.

The data_type parameter specifies what type of data the column can hold (e.g. varchar, integer, decimal, date, etc.).

The size parameter specifies the maximum length of the column of the table.4.1 Do database design using SQL commands

STRUCTURED QUERY LANGUAGECREATE TABLE Example

To create a table called "Persons" that contains five columns: PersonIDLastNameFirstNameAddressCity

4.1 Do database design using SQL commands

STRUCTURED QUERY LANGUAGECREATE TABLE Example

We use the following CREATE TABLE statement:ExampleCREATE TABLE Persons(PersonID int,LastName varchar(255),FirstName varchar(255),Address varchar(255),City varchar(255));

4.1 Do database design using SQL commands

STRUCTURED QUERY LANGUAGECREATE TABLE ExampleCREATE TABLE Persons(PersonID int,LastName varchar(255),FirstName varchar(255),Address varchar(255),City varchar(255));

4.1 Do database design using SQL commands P_IdLastNameFirstNameAddressCity

STRUCTURED QUERY LANGUAGEDisember 2012Section A : Question 8Write an SQL command to create relation for figure below:

Student_Data

(4 marks)

Final Exam QuestionNAMEAGESTATUSSALARY

STRUCTURED QUERY LANGUAGEALTER TABLE Statement

The ALTER TABLE statement is used to add, delete, or modify COLUMNS in an existing table.4.1 Do database design using SQL commands

STRUCTURED QUERY LANGUAGEALTER TABLE Syntax

To add a column in a table, use the following syntax:ALTER TABLE table_nameADD column_name datatype

To delete a column in a table, use the following syntax (notice that some database systems don't allow deleting a column):ALTER TABLE table_nameDROP COLUMN column_name

4.1 Do database design using SQL commands

STRUCTURED QUERY LANGUAGEALTER TABLE Syntax

To change the data type of a column in a table, use the following syntax:ALTER TABLE table_nameMODIFY COLUMN column_name datatype

4.1 Do database design using SQL commands

STRUCTURED QUERY LANGUAGEADD COLUMN Example

To add a column named "DateOfBirth" in the "Persons" table.We use the following SQL statement:

ALTER TABLE PersonsADD DateOfBirth date;

4.1 Do database design using SQL commands P_IdLastNameFirstNameAddressCityDateOfBirth 1HasanOthmanTmn TimahIpoh2SurayaTaminTmn CempakaIpoh3PuteriKarimTmn JatiKota Bharu

STRUCTURED QUERY LANGUAGEDROP COLUMN Example

To delete the column named "DateOfBirth" in the "Persons" table.We use the following SQL statement:

ALTER TABLE PersonsDROP COLUMN DateOfBirth

4.1 Do database design using SQL commands P_IdLastNameFirstNameAddressCity1HasanOthmanTmn TimahIpoh2SurayaTaminTmn CempakaIpoh3PuteriKarimTmn JatiKota Bharu

STRUCTURED QUERY LANGUAGECHANGE DATA TYPE Example

To change the data type of the column named "DateOfBirth" in the "Persons" table.We use the following SQL statement:

ALTER TABLE PersonsALTER COLUMN DateOfBirth year

Notice that the "DateOfBirth" column is now of type year and is going to hold a year in a two-digit or four-digit format.

4.1 Do database design using SQL commands

STRUCTURED QUERY LANGUAGEDROP TABLE Statement

The DROP TABLE statement is used to delete a table.DROP TABLE table_name

DROP DATABASE Statement

The DROP DATABASE statement is used to delete a database.DROP DATABASE database_name

4.1 Do database design using SQL commands

STRUCTURED QUERY LANGUAGE2.0 DML statements in SQL

SELECT - extracts data from a databaseUPDATE - updates data in a databaseDELETE - deletes data from a databaseINSERT INTO - inserts new data into a database4.1 Do database design using SQL commands

STRUCTURED QUERY LANGUAGESELECT Statement

The SQL SELECT StatementThe SELECT statement is used to select data from a database.The result is stored in a result table, called the result-set.SQL SELECT SyntaxSELECT column_name(s)FROM table_nameandSELECT * FROM table_name4.1 Do database design using SQL commands

STRUCTURED QUERY LANGUAGESELECT StatementAn SQL SELECT Example

Persons

The table above contains three records (one for each person) and five columns (P_Id, LastName, FirstName, Address, and City).

4.1 Do database design using SQL commands P_IdLastNameFirstNameAddressCity1HasanOthmanTmn TimahIpoh2SurayaTaminTmn CempakaIpoh3PuteriKarimTmn JatiKota Bharu

STRUCTURED QUERY LANGUAGESELECT StatementSELECT LastName,FirstName FROM Persons;

The result-set will look like this:

4.1 Do database design using SQL commands P_IdLastNameFirstNameAddressCity1HasanOthmanTmn TimahIpoh2SurayaTaminTmn CempakaIpoh3PuteriKarimTmn JatiKota BharuLastNameFirstNameHasanOthmanSurayaTaminPuteriKarim

STRUCTURED QUERY LANGUAGESELECT StatementSELECT * FROM Persons

The result-set will look like this:

4.1 Do database design using SQL commands P_IdLastNameFirstNameAddressCity1HasanOthmanTmn TimahIpoh2SurayaTaminTmn CempakaIpoh3PuteriKarimTmn JatiKota BharuP_IdLastNameFirstNameAddressCity1HasanOthmanTmn TimahIpoh2SurayaTaminTmn CempakaIpoh3PuteriKarimTmn JatiKota Bharu

STRUCTURED QUERY LANGUAGESELECT DISTINCT Statement

The DISTINCT keyword can be used to return only distinct (different) values

SQL SELECT DISTINCT SyntaxSELECT DISTINCT column_name(s)FROM table_name

4.1 Do database design using SQL commands

STRUCTURED QUERY LANGUAGESELECT DISTINCT StatementSELECT DISTINCT City FROM Persons

The result-set will look like this:

4.1 Do database design using SQL commands P_IdLastNameFirstNameAddressCity1HasanOthmanTmn TimahIpoh2SurayaTaminTmn CempakaIpoh3PuteriKarimTmn JatiKota BharuCityIpohKota Bharu

STRUCTURED QUERY LANGUAGEUPDATE Statement

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

SQL UPDATE SyntaxUPDATE table_nameSET column1=value, column2=value2,...WHERE some_column=some_valueNote: Notice the WHERE clause in the UPDATE syntax. The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!

4.1 Do database design using SQL commands

STRUCTURED QUERY LANGUAGEUPDATE ExampleUPDATE PersonsSET Address=Tmn Perdana', City=Ipoh'WHERE LastName=Sani' AND FirstName=Jusoh

The result-set will look like this:

4.1 Do database design using SQL commands P_IdLastNameFirstNameAddressCity1HasanOthmanTmn TimahIpoh4NaemahTalibTmn RiaKota Bharu5SaniJusohP_IdLastNameFirstNameAddressCity1HasanOthmanTmn TimahIpoh2SurayaTaminTmn CempakaIpoh3PuteriKarimTmn JatiKota Bharu4NaemahTalibTmn RiaKota Bharu5SaniJusohTmn PerdanaIpoh

STRUCTURED QUERY LANGUAGEUPDATE WarningBe careful when updating records. If we had omitted the WHERE clause in the example above, like this:UPDATE PersonsSET Address=' Tmn Timah', City=' Ipoh '

The result-set will look like this:

4.1 Do database design using SQL commands P_IdLastNameFirstNameAddressCity1HasanOthmanTmn TimahIpoh2SurayaTaminTmn TimahIpoh3PuteriKarimTmn TimahIpoh4NaemahTalibTmn TimahIpoh5SaniJusohTmn TimahIpoh

STRUCTURED QUERY LANGUAGEDELETE Statement

The DELETE statement is used to delete records in a table.The DELETE StatementThe DELETE statement is used to delete rows in a table.SQL DELETE SyntaxDELETE FROM table_nameWHERE some_column=some_valueNote: Notice the WHERE clause in the DELETE syntax. The WHERE clause specifies which record or records that should be deleted. If you omit the WHERE clause, all records will be deleted!

4.1 Do database design using SQL commands

STRUCTURED QUERY LANGUAGEDELETE ExampleDELETE FROM PersonsWHERE LastName=Sani' AND FirstName=Jusoh

The result-set will look like this:

4.1 Do database design using SQL commands P_IdLastNameFirstNameAddressCity1HasanOthmanTmn TimahIpoh2SurayaTaminTmn CempakaIpoh3PuteriKarimTmn JatiKota Bharu4NaemahTalibTmn RiaKota Bharu5SaniJusohTmn PerdanaIpohP_IdLastNameFirstNameAddressCity1HasanOthmanTmn TimahIpoh2SurayaTaminTmn CempakaIpoh3PuteriKarimTmn JatiKota Bharu4NaemahTalibTmn RiaKota Bharu

STRUCTURED QUERY LANGUAGEDelete All Rows

It is possible to delete all rows in a table without deleting the table. This means that the table structure, attributes, and indexes will be intact:

DELETE FROM table_nameorDELETE * FROM table_name

Note: Be very careful when deleting records. You cannot undo this statement

4.1 Do database design using SQL commands

STRUCTURED QUERY LANGUAGEINSERT INTO StatementThe INSERT INTO statement is used to insert new records/row in a table.

INSERT INTO SyntaxIt is possible to write the INSERT INTO statement in TWO forms. The first form doesn't specify the column names where the data will be inserted, only their values:INSERT INTO table_nameVALUES (value1, value2, value3,...)The second form specifies both the column names and the values to be inserted:INSERT INTO table_name (column1, column2, column3,...)VALUES (value1, value2, value3,...)

4.1 Do database design using SQL commands

STRUCTURED QUERY LANGUAGEINSERT INTO Statement (First Form) INSERT INTO PersonsVALUES (4,'Naemah', 'Talib', Tmn Ria', 'Kota Bharu')

The result-set will look like this:

4.1 Do database design using SQL commands P_IdLastNameFirstNameAddressCity1HasanOthmanTmn TimahIpoh2SurayaTaminTmn CempakaIpoh3PuteriKarimTmn JatiKota BharuP_IdLastNameFirstNameAddressCity1HasanOthmanTmn TimahIpoh2SurayaTaminTmn CempakaIpoh3PuteriKarimTmn JatiKota Bharu4NaemahTalibTmn RiaKota Bharu

STRUCTURED QUERY LANGUAGEINSERT INTO Statement (Second Form) INSERT INTO Persons (P_Id, LastName, FirstName)VALUES (5, Sani', 'Jusoh')

The result-set will look like this:

4.1 Do database design using SQL commands P_IdLastNameFirstNameAddressCity1HasanOthmanTmn TimahIpoh2SurayaTaminTmn CempakaIpoh3PuteriKarimTmn JatiKota Bharu4NaemahTalibTmn RiaKota Bharu5SaniJusohP_IdLastNameFirstNameAddressCity1HasanOthmanTmn TimahIpoh2SurayaTaminTmn CempakaIpoh3PuteriKarimTmn JatiKota Bharu4NaemahTalibTmn RiaKota Bharu

STRUCTURED QUERY LANGUAGEDisember 2012Section A : Question 7State the function and write the SQL syntax of the term below:INSERTDELETE(4 marks)Jun 2013Section A : Question 10Build the SQL command to insert a new record containing information which is EC601,Database System,2 to COURSE table as in table below.:COURSE

Final Exam QuestionCourseCodeCourseNameCredit

STRUCTURED QUERY LANGUAGEWHERE Clause

The WHERE clause is used to filter records. The WHERE clause is used to extract only those records that fulfill a specified criterion.

SQL WHERE Syntax

SELECT column_name(s)FROM table_nameWHERE column_name operator value

4.1 Do database design using SQL commands

STRUCTURED QUERY LANGUAGEWHERE ClauseSELECT * FROM PersonsWHERE City=Ipoh'

The result-set will look like this:

4.1 Do database design using SQL commands P_IdLastNameFirstNameAddressCity1HasanOthmanTmn TimahIpoh2SurayaTaminTmn CempakaIpoh3PuteriKarimTmn JatiKota BharuP_IdLastNameFirstNameAddressCity1HasanOthmanTmn TimahIpoh2SurayaTaminTmn CempakaIpoh

STRUCTURED QUERY LANGUAGEQuotes Around Text FieldsSQL uses single quotes around text values (most database systems will also accept double quotes).However, numeric values should not be enclosed in quotes.For text values:This is correct:SELECT * FROM Persons WHERE FirstName='Tove'This is wrong:SELECT * FROM Persons WHERE FirstName=ToveFor numeric values:This is correct:SELECT * FROM Persons WHERE Year=1965This is wrong:SELECT * FROM Persons WHERE Year='1965'

4.1 Do database design using SQL commands

STRUCTURED QUERY LANGUAGEOperators Allowed in the WHERE Clause

4.1 Do database design using SQL commands OperatorDescription=EqualNot equal>Greater than=Greater than or equal