Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Module For Midterm

Download as pdf or txt
Download as pdf or txt
You are on page 1of 31

Introduction to Structured

Query Language (SQL)


Chapter 4
Why SQL?
• SQL is a database query language used for storing and managing data in
Relational DBMS. SQL was the first commercial language introduced for E.F
Codd's Relational model of database. Today almost all RDBMS (MySql,
Oracle, Infomix, Sybase, MS Access) use SQL as the standard database
query language. SQL is used to perform all types of data operations in
RDBMS.

• SQL Statements are understood by MS Access, MS SQL Server, DB2, Oracle,


Sybase, MySQL and others.

• SQL is a Sublanguage that:


• Uses high level, easy to understand statements
• Has a relational Database orientation
• Is portable across wide range of systems
Structures Query Language (SQL)
• Is a standard language for accessing and manipulating databases
• Graphical user interface allows point-and-click queries
• SQL lets you access and manipulate databases
• SQL is an ANSI (American National Standards Institute) standards
What can SQL do?
• SQL can execute queries against a database
• SQL can retrieve data from database
• SQL can insert records in a database
• SQL can delete records from the database
• SQL can create new databases
• SQL can create new tables in a database
• SQL can create stored procedures in a database
• SQL can create views in a database
• SQL can set permissions on tables, and procedures
SQL DML and DDL
• SQL can be divided into parts;

1. The Data Manipulation Language (DML)


2. The Data Definition Language (DDL)

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;

• CREATE DATABASE - creates a new database


• ALTER DATABASE - modifies a database
• CREATE TABLE - creates a new table
• ALTER TABLE - modifies a table
• DROP DATABASE - deletes a database
• DROP TABLE - deletes a table
• CREATE INDEX - creates an index (search key)
• DROP INDEX – deletes an index
Database tables
• A database most often contains one or more tables. Each table is
identified by a name (e.g. “Customer” or “Orders”). Tables contains
records (rows) with data.
• Below is an example of a table called “tblStudents”
Basic Data Types
1. INT --- Whole Numbers
2. DECIMAL (10,4) --- Decimal Number – Exact Value
3. VARCHAR(30) --- String of Text of length 1
4. BLOB --- Binary Large Object, Stores large
data
5. DATE --- ‘YYYY-MM-DD’
6. TIMESTAMP --- ‘YYYY-MM-DD HH:MM:SS’ – use for
recording time and date
SQL Queries/Command
• A query is a set of instructions given to the RDBMS (written in SQL)
that tells the RDBMS what information you want it to retrieve for you.

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.)

We will only focus on the following constraints:


• NOT NULL – a constraint that Primary enforces a column to NOT accept Null values.
• UNIQUE – constraints that uniquely identify each record in a database table column
• PRIMARY KEY – uniquely identify each record in a database table.
• FOREIGN KEY – a key that points to a primary key in another table
• CHECK - constraint is used to limit the value range that can be placed in a column.
• DEFAULT – displays wanted default value if the input is null
MySQL Data Types
MySQL Data Types
MySQL Data Types
Basic SQL Commands
• DROP TABLE statement – use to delete the table

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)
);

INSERT INTO bands(bname, origin) VALUES('Parokya ni Edgar', 'Quezon City');


INSERT INTO albums(aname,release_yr,band_id) VALUES(101,'kami na muna','1990-08-
08',001);
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;

NOTE: SQL in not case sensitive SELECT is the same as select.


Basic SQL Commands
• SELECT DISTINCT statement – use to display different (distinct) values
in a table.

Syntax:
SELECT DISTINCT column1, column2 FROM tbl_name;

Example:
SELECT DISTINCT IDNo, Lname, FName FROM tblstudents

NOTE: SQL is not case sensitive SELECT is the same as select.


Basic SQL Commands
• SELECT DISTINCT statement
Determine the output of SELECT DISTINCT statement;
SELECT DISTINCT City, Country FROM tblcustomer;
Basic SQL Commands
• WHERE clause – use to extract only the records that fulfill a specified
criterion.

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 !=

You might also like