Chapter 4 (1)
Chapter 4 (1)
The Query and Update commands from the DML part of SQL
• SELECT – extracts data from the database
• UPDATE – updates data from the database
• DELETE – deletes data from the database
• INSERT INTO – insert new data into a database
• The DDL part of SQL permits database tables to be created or deleted.
It also defines indexes (keys), specifies links between tables, and
imposes constraints between tables. The most importand DDL
statements in SQL are;
Example
SELECT tblstudent.Fname, tblstudent.Lname
FROM tblstudents
WHERE tblstudent.Address = Echague;
Basic SQL Commands
• CREATE DATABASE statement – is use to create a database
Syntax:
CREATE DATABASE database_name;
Example:
CREATE DATABASE dbEnrollmentSystem;
Basic SQL Commands
• CREATE TABLE statement – is use to create a tables in a database.
Syntax:
CREATE TABLE table_name
(
column_name1 datatype,
column_name2 datatype,
column_name3 datatype,
column_name4 datatype
);
Basic SQL Commands
Example:
CREATE TABLE tblStudents
(
IDNo int (6) primary key not null,
Lname varchar (30) default null,
Fname varchar (30) unique default null,
Mname varchar (30) default null,
Bdate date default null,
Address varchar (70) default null
);
Basic SQL Commands
Output:
SQL Constraints
• Constraints are use to limit the type of data that can go into a table.
• Constraints can be specified when table is created (with the CREATE TABLE
statement) or after the table is created (with the ALTER TABLE statement.)
Syntax:
DROP TABLE tbl_name;
Example:
DROP TABLE tblStudents;
Basic SQL Commands
• ALTER TABLE statement – use to add a new column in a table
Syntax:
ALTER TABLE ADD age int(5);
Example:
ALTER TABLE tblstudents ADD age INT(5);
Basic SQL Commands
How to delete Column?
Syntax:
ALTER TABLE table_name DROP COLUMN column_name;
Example:
ALTER TABLE tblstudents DROP COLUMN age;
Inserting Data in the Database Table
• INSERT INTO statement can be also use in inserting data that lacks
some column content.
Syntax:
INSERT INTO tbl_name VALUES(‘value1’,’value2’,’value3’);
Example:
INSERT INTO tblStudent VALUES (10101, ‘Desiray’,’Nayga’);
Using Select statement to view Data in the
Database Table
Syntax:
SELECT * FROM table_name;
Example:
SELECT * FROM tblstudents;
Inserting Data in the Database Table
• INSERT INTO statement – use to insert a new row in a table
Syntax:
INSERT INTO tbl_name (column1,column2,column2)
VALUES(‘value1’,’value2’,’value3’);
Example:
INSERT INTO tblStudents VALUES (10101, ‘Desiray’,’Nayga’);
Basic SQL Commands
• SELECT statement – use to select data from the database
Syntax:
SELECT column_name FROM tbl_name;
OR
SELECT * FROM tbl_name;
Example:
SELECT IDNo, Lname, FName FROM tblStudent;
OR
SELECT * FROM tblStudents;
Syntax:
SELECT DISTINCT column1, column2 FROM tbl_name;
Example:
SELECT DISTINCT IDNo, Lname, FName FROM tblStudents
Syntax:
SELECT columnName FROM tbl_name WHERE columnName operator
value;
Example:
SELECT Fname, LName FROM tblStudents WHERE IDNo = 10101;
OR
SELECT * FROM tblStudents WHERE Address = ‘Echague’;
Note: You can use single quote on textfield and none for numeric values.
Operators Allowed in WHERE clause
Operators
• AND operator – displays a record if both the first and second condition is
true.
Syntax:
SELECT columnName, columnName FROM tbl_name WHERE
columnName operator value AND columnName operator value;
Example:
SELECT FName, LName FROM tblStudents WHERE Address = Echague
AND IDNo =10101;
Operators
• OR operator – displays a record if either the first condition or the second
condition is true.
Syntax:
SELECT columnName FROM tbl_name WHERE columnName operator
value OR columnName operator value;
Example:
SELECT Fname, LName FROM tblStudents WHERE Address = ‘Echague’
OR IDNo=10102 ;
Operators
• ORDER BY keyword – used to sort the result-set by specified column.
Sorts record by ascending order by default.
Syntax:
SELECT columnName FROM tbl_name ORDER BY columnName;
Example:
SELECT Fname, LName FROM tblStudents ORDER BY LName
DESC;
Syntax:
UPDATE tbl_name SET columnName = value, columnName = value2
WHERE columnName =value;
Example:
UPDATE tblStudents SET Address = ‘Alicia’ WHERE IDNo =10102;
OR
UPDATE tblStudents SET Address = ‘Alicia’ WHERE IDNo = 10102 AND
Fname=‘Desiray’;
Note: UPDATE statement can be use to fill-up Null data or change the current value of a column.
Basic SQL Commands
• DELETE statement – used to delete rows on the table.
Syntax:
DELETE FROM tbl_name WHERE columnName =value;
Example:
DELETE FROM tblStudents WHERE IDNo =10102;
OR
DELETE FROM tblStudents WHERE FName=‘Desiray’ AND LName
=‘Nayga’;
Note: The WHERE clause specifies the record or records that should be deleted. If WHERE clause is omitted then all
records will be deleted.
SQL Advance Commands
• LIKE operator – is used to search for a specified pattern in a column.
Syntax:
SELECT columnName FROM tbl_name WHERE columnName LIKE
pattern;
Example:
SELECT * FROM tblStudents WHERE Address LIKE ‘a%’;
The example above illustrates that we want to select the tblStudents having an Address that starts with “a” from
the table
SQL Advance Commands
SELECT * FROM tblStudents WHERE Address LIKE ‘s%’;
The example above illustrates that we want to select the tblStudents having an Address that starts with “a” from
the table. The “%” sign can be use to define wildcards (missing letter in the pattern) both before and after the
pattern.
IDNo LName FName MName BDate Address
10101 Castillo Wendell Mercado 1989-04-26 Echague
10102 Nayga Desiray Domael 1990-08-08 Echague
10103 Medrano Jun Michael Nayga 1988-06-12 Alicia
RESULT
Note: If we want to search for an Address that ends with an “e” then we’ll just have to change our pattern from
‘a%’ to ‘%e’.
SQL Advance Commands
SELECT * FROM tblStudents WHERE Address NOT LIKE ‘%ag%’;
If we are going to search for a particular address that does not have “ag” in the Pattern we’ll just have to add NOT
keyword in the statement.
RESULT