0% found this document useful (0 votes)
23 views73 pages

PCIT 02 Chapter 4 of Fundamental of Database System

Uploaded by

kaye layan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views73 pages

PCIT 02 Chapter 4 of Fundamental of Database System

Uploaded by

kaye layan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 73

Fundamental of Database

System
Chapter 4
SQL
EMILVERCHRISTIAN V. ARQUERO

EMILVERCHRISTIAN V. ARQUERO, MIT


Chapter 4:
SQL
• SQL Syntax, Data Types, and Operators
• SQL Database
• CREATE, DROP, SELECT
• SQL Table
• CREATE, DROP, RENAME, TRUNCATE, COPY,
ALTER
• SQL INSERT, SELECT, DELETE, and UPDATE

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


What is SQL?

SQL stands for Structured Query Language. It is used for storing and
managing data in relational database management system (RDMS).

It is a standard language for Relational Database System. It enables a


user to create, read, update and delete relational databases and
tables.

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL?
SQL is a comprehensive database language: It has statements for data
definitions, queries, and updates. Hence, it is both a DDL and a DML. In
addition, it has facilities for defining views on the database, for specifying
security and authorization, for defining integrity constraints, and for
specifying transaction controls.

It also has rules for embedding SQL statements into a general-purpose


programming language such as Java or C/C++.1

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


Data Types
Data types take into consideration the length allocated by the database for
every column in the table and what values it could contain - whether it is
alphanumeric, just numbers, graphics, date or time.

Since database is a collection of information, it can store names, numbers,


images, calculations, financial amounts, characters and so on. This stored
information is what you call data, which you can change or manipulate
anytime you want.

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


Data Types
By defining what data is stored in each database field, you are
preventing the occurrence of data entry errors. This form of validation
that controls incorrect data to be entered into the database is also
called field definition.

the field’s value is considered null - meaning the value is not known.
This null value is different from the numeric zero value or the blank
character value, since zeroes and blanks are definite values

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


Data Types
The following are scenarios when you may have a null value:

 You don’t know what the value is yet even if it possibly exists.
 The value does not exist yet.
 The value is out of range.
 The field is not applicable for a particular row.

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


Data Types
The following are the general data types predefined in the SQL
language (that are further categorized into subtypes):

1. Numeric - The value defined by the numeric data type is some kind
of a number, which could either be expressed with an exact or just
an approximate value.
1.1) INTEGER – This consists only of whole numbers that are both positive
and negative. It does not contain a decimal nor a fractional part. The value
ranges from -2,147,483,648 to 2,147,483,647, with an allocated 4 bytes of
storage size.

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


Data Types
1.2) SMALLINT – This is used in replacement of integers to save storage
space, but with a precision that cannot be larger than that of an integer.
Precision in computer programming is the maximum total of significant
digits a number can have. The value ranges from -32,768 to +32,767, with
an allocated 2 bytes of storage size.

1.3) BIGINT – This is the reverse of the SMALLINT, where its minimum
precision is the same as the INTEGER data type or greater. The value ranges
from -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807, with an
allocated 8 bytes of storage size.

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


Data Types
1.4) NUMERIC (p, s) – this data type also contains a fractional component
that indicates the precision and scale of the value. Scale is the number of
digits or places reserved in a fractional part of the data, located at the right
side of the decimal point. For example, NUMERIC (6, 3) means that the
number’s absolute value will only be up to 999.999 (6 total significant digits
with 3 digits following the decimal point).
1.5) DECIMAL (p, s) – Like the NUMERIC data type, this has a fractional
component where you can specify both the value precision and scale. For
example, DECIMAL (6, 3) can contain values up to 999.999 but the database
will still accept values larger than 999.999. Let us say you entered the
number 123.4564, this will be rounded off to 123.456.

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


Data Types
2.) String – The string data type stores alphanumeric information and is also
considered as one of the most commonly used data types.

2.1) CHARACTER (n) or CHAR (n) – This data type is also known as a fixed-
length string or a constant character. This means that all the strings stored
in that particular column have the same length, which is represented by ‘n’
(the number of characters or the maximum allocated length for the defined
field).

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


Data Types
2.2) CHARACTER VARYING (n) or VARCHAR (n) – This is used when the data
entries are of different lengths, or not constant, but you don’t want SQL to
fill the remaining spaces with blanks. Thus, the exact number of characters
you enter will be stored in the database - further saving storage space. This
data type has no default value and its maximum length is 32,672 characters.

2.3) CHARACTER LARGE OBJECT (CLOB) – Introduced in SQL:1999, this


variable-length data type is used to contain unicode character-based
information that is too big to be stored as a CHARACTER type, such as large
documents. The maximum value of a CLOB is up to 2,147,483,647
characters long.

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


Data Types
3.) Date and Time – This data type manages any information concerning
dates and times.

3.1) DATE – This data type provides storage for the year, month and day
values of a date, in that particular order. The year value is expressed using
four digits, which can be represented by any value ranging from 0001 up to
9999. As for the month and day values, they are both expressed using two
digits. The format for the date data type is yyyy-mm-dd

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


Data Types
3.2) TIME – This data type stores and displays time values with an hour-
minute-second format (“HH:MM:SS”).

3.3) DATETIME – When the value contains both date and time information
then you use the DATETIME data type, which is displayed using the “YYYY-
MM-DD HH:MM:SS” format. The valid range of values for this type is from
“1000-01-01 00:00:00” to “9999-12-31 23:59:59”.

3.4) TIMESTAMP – This is similar to the DATETIME data type but the range
of values is from “1970-01-01 00:00:01” UTC to “2038-01-19 03:14:07” UTC.

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


Data Types
4) Boolean – This data type consists of values that are used for data
comparison: TRUE, FALSE, or NULL. For data to be returned, all the
conditions of the specified criteria for a given query should be met –
meaning the Boolean value is TRUE. If data is not returned, then the value is
either FALSE or NULL

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Operator
There are various types of SQL operator

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Operator
SQL Arithmetic
Operators
Let's assume 'variable a'
and 'variable b'. Here, 'a'
contains 20 and 'b'
contains 10.

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Operator
SQL Comparison
Operators
Let's assume 'variable a'
and 'variable b'. Here, 'a'
contains 20 and 'b'
contains 10.

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Operator
SQL Logical
Operators

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Definition
SQL uses the terms table, row, and column for the formal relational model
terms relation, tuple, and attribute, respectively. We will use the
corresponding terms interchangeably.

The main SQL command for data definition is the CREATE statement, which
can be used to create schemas, tables (relations), types, and domains, as
well as other constructs such as views, assertions, and triggers.

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Definition (Create Database)
The SQL CREATE DATABASE statement is used to create a new SQL
database.

The basic syntax of this CREATE DATABASE statement is as follows


CREATE DATABASE DatabaseName;

Note: Always the database name should be unique within the RDBMS.

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Definition (Create Database)
For example: If you want to create a new database testdb, then the CREATE
DATABASE statement would be as shown below −

Once the database is created, this will return "Query OK, 1 row affected."

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Definition (Show Databases)
You can check it in the list of databases by typing the SQL statement
"SHOW DATABASES;“

And this will return a


list of all databases.

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Definition (Drop Database)
The SQL DROP DATABASE statement is used to drop or delete an existing
database.

The basic syntax of DROP DATABASE statement is as follows


DROP DATABASE DatabaseName;

NOTE − Be careful before using this operation because by deleting an existing


database would result in loss of complete information stored in the database.

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Definition (Drop Database)
For example: If you want to delete an existing database “testdb”, then the
DROP DATABASE statement would be as shown below

Once the database is deleted, this will return "Query OK, 0 rows affected.“

Again you can check it in the list of databases by typing the SQL statement
"SHOW DATABASES;“ to verify if the database is deleted.

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Definition (Select Database)
When you have multiple databases in your SQL Schema, then before
starting your operation, you would need to select a database where all the
operations would be performed.

The SQL USE statement is used to select any existing database.

The basic syntax of the USE statement is as shown below


USE DatabaseName;

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Definition (Select Database)
For example, if you want to select an existing database called "testdb",
then the USE statement would be as shown below.

Once the statement is executed successfully, this will return "Database


Changed“ and the result would be as shown below:

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Definition (Create Table)
Creating a basic table involves naming the table and defining its columns
and each column's data type.
The SQL CREATE TABLE statement is used to create a new table.

The basic syntax of the CREATE TABLE statement is as follows −


CREATE TABLE tableName (
columnName1 datatype(length) ,
columnName2 datatype(length)
);

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Definition (Create Table)
Here are some of the factors to take into consideration through when
creating tables:
 Type of data the table will contain
 Table and column names
 Primary key (the column that makes each row of data unique to avoid
duplicate records in a table)
 Column length
 Columns containing null values

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Definition (Create Table)
For example: The following code block is an example, which creates a
tbl_customer table with an cus_id as a primary key and NOT NULL are the
constraints showing that these fields cannot be NULL while creating
records in this table

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Definition (Show Table)
Once the table is successfully created, this will return the message "Query
OK, 0 rows affected.“

Check it in the list of tables by typing the SQL statement "SHOW TABLES;“

And this will return a


list of all tables.

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Definition (Describe Table)

To check the data definition for table tbl_customer," use the "DESC"
statement.

The basic syntax of the DESC statement is as follows


DESC tbl_customer;

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Definition (Describe Table)
For example, if you want to describe an existing table called
"tbl_customer," then the statement would be as shown below.

This will return the


following rows
of table:

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Definition (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.

The basic syntax of this DROP TABLE statement is as follows


DROP TABLE table_name;

NOTE − Be very careful while using this command, because once a table is
deleted, all the information available in that table will also be lost forever.

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Definition (Drop Table)
For example, if you want to delete an existing table "tbl_customer", then
the DROP TABLE statement would be as shown below.

Once the table is deleted, this will return "Query OK, 0 rows affected.“

Again you can check it in the list of tables by typing the SQL statement
"SHOW TABLES;“ to verify if the table is deleted.

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Definition (RENAME TABLE)
Database administrators and users want to change the name of the table
in the SQL database because they want to give a more relevant name to
the table.

Any database user can easily change the name by using the RENAME
TABLE and ALTER TABLE statement in Structured Query Language.

The RENAME TABLE and ALTER TABLE syntax help in changing the name of
the table.

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Definition (RENAME TABLE)
Option 1: The syntax for renaming tables using the "RENAME TABLE"
statement is shown below.
RENAME TABLE old_table _name TO new_table_name ;

For example, if you want to rename an existing table "tbl_customer" to


"tbl_cstmr," then the statement would be as shown below.

Once the statement successfully, this will return "Query OK, 0 rows affected.“

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Definition (RENAME TABLE)
Option 2: The syntax for renaming tables using the “ALTER TABLE" statement
is shown below.
ALTER TABLE old_table_name RENAME TO new_table_name;

For example:

Once the statement successfully, this will return "Query OK, 0 rows affected.“

Again you can check it in the list of tables by typing the SQL statement
"SHOW TABLES;“ to verify if the table is renamed.

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Definition (TRUNCATE TABLE)
A truncate SQL statement is used to remove all rows (complete data) from a
table.

The syntax for deleting all the data from tables using the "TRUNCATE TABLE"
statement is shown below.
TRUNCATE TABLE tableName;

Note: The rollback process is not possible after truncate table statement. Once you
truncate a table you cannot use a flashback table statement to retrieve the content
of the table.

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Definition (TRUNCATE TABLE)
For example, First, select all the data from "tbl_customer" using the SELECT
statement. This will return the table's data.

In this case, we have 3 rows of data in the table.


EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)
SQL Data Definition (TRUNCATE TABLE)
Now let's truncate or delete all the data in table customer. The syntax will be
shown as follows:

this will return "Query OK, 0 rows affected.“

To verify the table's data, use the select statement to retrieve the data.

This will returned the Empty set


EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)
SQL Data Definition (COPY TABLE)
There may be a situation when you just want to create an exact copy or
clone of an existing table to test or perform something without affecting the
original table.

Syntax: CREATE TABLE new_table SELECT * FROM original_table;

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Definition (COPY TABLE)
for example, We are going to backup the table of tbl_cstmr. In this case we
are to copy all the data in new table called tbl_cstmrbkp.

After executing the statement with no error this will return the “Query Ok, 1
row affected.” means that the tbl_cstmr table is successfully cloned .

Again you can use DESC statement to describe the cloned table.
EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)
SQL Data Definition (ALTER TABLE)
The ALTER TABLE statement in Structured Query Language allows you to add,
modify, and delete columns of an existing table. This statement also allows
database users to add and remove various SQL constraints on the existing
tables.

1. ALTER TABLE ADD COLUMN statement


2. ALTER TABLE MODIFY COLUMN statement
3. ALTER TABLE DROP COLUMN statement
4. ALTER TABLE CHANGE COULMN statement

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Definition (ALTER TABLE)
1. ALTER TABLE ADD Column statement
This statement will add the columns in the existing table. Instead of creating
a whole table or database. It can easily add single and multiple columns
using the ADD keyword.

Syntax: ALTER TABLE table_name ADD column_name column-definition;

The above syntax only allows you to add a single column to the existing
table.

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Definition (ALTER TABLE)
If you want to add more than one column to the table in a single SQL
statement, then use the following:
ALTER TABLE table_name
ADD (
column_Name1 column-definition,
column_Name2 column-definition,
column_Name3 column-definition
);

Note: All column name and definition should enclosed inside the parenthesis.
EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)
SQL Data Definition (ALTER TABLE)
For example, We want add the username and password in tbl_cstmr table.

The statement above will created a column username with datatype of


varchar and 255 length, and password with datatype of varchar and 550
length. This statement will returned the “Query OK, 0 rows affected”.

To check if the column is added, we can use DESC statement to describe the table
definition this will returned the columns.
EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)
SQL Data Definition (ALTER TABLE)
2. ALTER TABLE MODIFY COLUMN statement
The MODIFY keyword is used for changing the column definition of the
existing table.

Syntax: ALTER TABLE table_name MODIFY column_name column-definition;

This syntax only allows you to modify a single column of the existing table.

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Definition (ALTER TABLE)
If you want to modify more than one column of the table in a single SQL
statement, then use the following syntax:
ALTER TABLE table_name
MODIFY (
column_Name1 column-definition,
column_Name2 column-definition,
column_Name3 column-definition
);

Note: All column name and definition should enclosed inside the parenthesis.
EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)
SQL Data Definition (ALTER TABLE)
For example, First, we describe the table tbl_cstmr.

Here we can see that the


cus_contact column has a
Datatype of char, we wish to
Change the datatype into
VARCHAR with the length of
20;

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Definition (ALTER TABLE)
Now let’s modify the cus_contact column. The syntax will be shown as
follows:

The statement will returned the “Query OK, 1 row affected.”;

To check if the column modified


use the DESC statement to describe
the tbl_cstmr table.

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Definition (ALTER TABLE)
3. ALTER TABLE DROP COLUMN statement
The DROP keyword is used for delete the columns from the existing table.
Instead of deleting the whole table or database. DROP keyword can use for
deleting the columns.
Syntax: ALTER TABLE table_name DROP COLUMN column_name ;
Note: The ALTER TABLE DROP COLUMN will only drop one column in one statement.
If you wish to drop more column, then use the following syntax:

Syntax: ALTER TABLE table_name DROP COLUMN column_name1 ;


ALTER TABLE table_name DROP COLUMN column_name2 ;

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Definition (ALTER TABLE)

For example, we want to delete the cus_bday column from tbl_cstmr table.
The following syntax will be shown below.

The statement will returned the “Query OK, 0 rows affected.”; check using
the following statement that the cus_bday column is deleted from the table
or not use the DESC statement.
EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)
SQL Data Definition (ALTER TABLE)
4. ALTER TABLE CHANGE COULMN statement
The CHANGE keyword is used for changing the name of columns or fields of the
existing table.

Syntax: ALTER TABLE table_name CHANGE old_name new_name field-definition;

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Definition (ALTER TABLE)

For example, we want to rename the column username to cus_user. The syntax
will shown below:

In this case, we can see that if we rename the column, we need to define its
definition, meaning we cannot only change the column name but we can also
change the definition itself at the same time. To check if the column was
renamed, use the DESC statement.
EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)
SQL Data Manipulation

DML is short name of Data Manipulation Language which deals with data
manipulation and includes most common SQL statements such SELECT, INSERT,
UPDATE, DELETE, and it is used to store, modify, retrieve, delete and update
data in a database.

1. INSERT STATEMENT
2. SELECT STATEMENT
3. UPDATE STATEMENT
4. DELETE STATEMENT

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Manipulation (INSERT STATEMENT)
1. INSERT STATEMENT
SQL INSERT statement is a SQL query. It is used to insert a single or a multiple
records in a table.

There are two ways to insert data in a table:


1.1 By SQL Insert data directly into a table
1.1.1 By specifying column names
1.1.2 Without specifying column names
1.2 By SQL insert into select statement

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Manipulation (INSERT STATEMENT)
1.1 By SQL Insert data directly into a table
There are two ways to insert values in a table:
1.1.1 In the first method there is no need to specify the column name where
the data will be inserted, you need only their values.
Syntax: INSERT INTO table_name VALUES (value1, value2, value3);

1.1.2 The second method specifies both the column name and values which
you want to insert.
Syntax: INSERT INTO table_name (column1, column2, column3) VALUES
(value1, value2, value3)
EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)
SQL Data Manipulation (INSERT STATEMENT)
Example #1. The following statements would create 1 records in the tbl_cstmr
table using first method .

Note: the order of the values is in the


same order as the columns in the
table. In this case, the customer ID
will be defined first in the values since
the cus_id column is first, and the
password will be defined last in the values.
EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)
SQL Data Manipulation (INSERT STATEMENT)
Example #2. The following statements would create 1 records in the tbl_cstmr
table using second method .

Note: If the value is a string, this should be enclosed in a single quotation.

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Manipulation (INSERT STATEMENT)
Example #3. The following statements would create 3 records in the tbl_cstmr
table using second method .

Note. Note: The Insert statement above will insert data into a table in a
different statement, and each statement will return "Query OK, 1 row
affected."
EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)
SQL Data Manipulation (INSERT STATEMENT)
Example #4. The following statements would create three records in the
tbl_cstmr table in a single statement using the second method.

Note: Each of the values is enclosed in parenthesis and separated by a


comma. This statement will return "Query OK, 3 rows affected".

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Manipulation (INSERT STATEMENT)
1.2 By SQL insert into select statement
You can populate or copy the data into a table through the select statement
over another table; provided the other table has a set of fields, which are
required to populate the first table.

Syntax: INSERT INTO table_name1 (column1, column2, column) SELECT


column1, column2, ColumnN FROM table_name2 [WHERE condition];

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Manipulation (INSERT STATEMENT)
For example, we want to populate or copy the data from the tbl_cstmr table
to the tbl_cstmrbkp table. The syntax will be shown below.

The statement above will returned the “Query OK, 5 rows affected.”;

Note: The number of affected rows is determined by the number of rows from
the table that were successfully copied. And in this case, the tbl_cstmrbkp was
already created before the "insert into select statement" was executed.
EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)
SQL Data Manipulation (SELECT STATEMENT)
The SELECT statement is the most commonly used command in Structured
Query Language. It is used to access the records from one or more database
tables and views. It also retrieves the selected data that follow the conditions
we want.

By using this command, we can also access the particular record from the
particular column of the table. The table which stores the record returned by
the SELECT statement is called a result-set table.

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Manipulation (SELECT STATEMENT)
Syntax: SELECT ColumnName1, ColumnName2,ColumnNameN FROM
Table_Name;

In this SELECT syntax, ColumnName1, ColumnName2,ColumnNameN are the


name of those columns in the table whose data we want to return.

If you want to access all rows from all fields of the table, use the following SQL
SELECT syntax with * asterisk sign:

Syntax: SELECT * FROM table_name;

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Manipulation (SELECT STATEMENT)
Example #1. We want to return the columns cus_id and cus_name from tbl_cstmr.
Then this will be the syntax.
SELECT cus_id, cus_name FROM tbl_cstmr;

tbl_cstmr

result-set table of SELECT cus_id,


cus_name FROM tbl_cstmr;

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Manipulation (SELECT STATEMENT)
Example #2. We want to return all the columns from tbl_cstmr. Then this will be the
syntax.
SELECT * FROM tbl_cstmr;

result-set table of SELECT * FROM tbl_cstmr statement


EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)
SQL Data Manipulation (UPDATE STATEMENT)
The SQL commands UPDATE is used to modify the data that is already in the
database.

SQL UPDATE statement is used to change the data of the records held by tables.
Which rows is to be update, it is decided by a condition. To specify condition, we use
WHERE clause.

Syntax: UPDATE table_name SET column_name1= value1, column_nameN = valueN


WHERE condition

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)


SQL Data Manipulation (UPDATE STATEMENT)
Example #1, refer to the previous tbl_cstmr table. if we want to modify the name of
the customer with the customer ID of 00001, then this will be the syntax.
UPDATE tbl_cstmr SET cus_name = 'Layla Uranus' WHERE cus_id = '00001’;

To check if the customer name is successfully modified, use the SELECT statement to
return the data from the table;

result-set table of SELECT * FROM tbl_cstmr statement


EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)
SQL Data Manipulation (UPDATE STATEMENT)
Example #2, if we want to modify the name of the customer and address with the
customer ID of 00001, then this will be the syntax.
UPDATE tbl_cstmr SET cus_name = 'Layla Uranus’ , cus_addrs = ‘Indonesia’ WHERE
cus_id = '00001’;
Here we can see that each of the column separated by comma together it’s
value.

Note. The String value always


enclosed by the parenthesis.

The result of update statement above.


EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)
SQL Data Manipulation (DELETE STATEMENT)
The SQL DELETE statement is used to delete rows from a table. Generally DELETE
statement removes one or more records from a table.

Syntax #1: DELETE FROM table_name WHERE condition;


• This will delete the specific rows from the table using where Clause.
• WHERE clause is used to prevent the deletion of all the rows in the table, If you
don't use the WHERE clause you might loss all the rows.
• The WHERE clause in the SQL DELETE statement is optional and it identifies the
rows in the column that gets deleted.

Syntax #2: DELETE FROM table_name;


It will delete all the records of table.
EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)
SQL Data Manipulation (DELETE STATEMENT)
Example #1. We want to delete the records from the tbl_cstmr table using where
clause, then this will be the syntax.
DELETE FROM tbl_cstmr WHERE cus_id = ‘00001’;
It will delete the records of customer table where customer ID is 00001.

Example #2. DELETE FROM tbl_cstmr WHERE cus_addrs = ‘Philippines’;


It will delete the all the records of customer table where address ID is Philippines.

EMILVERCHRISTIAN V. ARQUERO, MIT FUNDAMENTALS OF DATABASE SYSTEM (PCIT 02)

You might also like