Module For Midterm
Module For Midterm
Module For Midterm
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’);
Foreign Key
CREATE DATABASE records;
USE records;
CREATE TABLE bands
(
id INT PRIMARY KEY AUTO_INCREMENT,
bname VARCHAR(255) NOT NULL,
origin VARCHAR(255) NOT NULL
);
Foreign Key
CREATE TABLE albums
(
id INT PRIMARY KEY AUTO_INCREMENT,
aname VARCHAR(255) NOT NULL,
release_yr DATE NOT NULL,
band_id INT NOT NULL,
FOREIGN KEY (band_id) REFERENCES bands(id)
);
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
The following can be used;
Operator Description
= Equal
<> Not equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between and inclusive range
LIKE Search for a pattern
IN If you know the exact value you want to
return for at least one of the column
NOTE: In some version of SQL the <> operator may be written as !=