PCIT 02 Chapter 4 of Fundamental of Database System
PCIT 02 Chapter 4 of Fundamental of Database System
System
Chapter 4
SQL
EMILVERCHRISTIAN V. ARQUERO
SQL stands for Structured Query Language. It is used for storing and
managing data in relational database management system (RDMS).
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
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.
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.
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.
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).
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
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.
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.
Note: Always the database name should be unique within the RDBMS.
Once the database is created, this will return "Query OK, 1 row affected."
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.
Check it in the list of tables by typing the SQL statement "SHOW TABLES;“
To check the data definition for table tbl_customer," use the "DESC"
statement.
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.
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.
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.
Once the statement successfully, this will return "Query OK, 0 rows affected.“
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.
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.
To verify the table's data, use the select statement to retrieve the data.
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.
The above syntax only allows you to add a single column to the existing
table.
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.
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.
This syntax only allows you to modify a single column of the existing table.
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.
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.
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
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. 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.
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.
If you want to access all rows from all fields of the table, use the following SQL
SELECT syntax with * asterisk sign:
tbl_cstmr
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.
To check if the customer name is successfully modified, use the SELECT statement to
return the data from the table;