0% found this document useful (0 votes)
50 views

SQL Introduction: Pamantasan NG Cabuyao

Uploaded by

Bien Medina
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

SQL Introduction: Pamantasan NG Cabuyao

Uploaded by

Bien Medina
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Pamantasan ng Cabuyao

College of Computing and Engineering


1ST SEMESTER 2020 – 2021

MODULE

3 SQL INTRODUCTION

OVERVIEW

The purpose of this module introduces the Structured Query Language or SQL to students. SQL (pronounced
"ess-que-el") stands for Structured Query Language. SQL is used to communicate with a database. According
to ANSI (American National Standards Institute), it is the standard language for relational database
management systems. SQL statements are used to perform tasks such as update data on a database, or
retrieve data from a database. Some common relational database management systems that use SQL are:
Oracle, Sybase, Microsoft SQL Server, Access, Ingres, etc.

OBJECTIVES

By the end of this module you should be able to:


1. Understand the use and components of SQL.
2. Explain how to utilize scripting language that can be used in DBMS
3. Define different commands that is used in DBMS thru SQL

1.1. SQL

Types of Structured Query Language(SQL)

In the above section, we learned what we do with the database using SQL. SQL is basically combination of
four different languages, they are –

DQL (Data Query Language)


DQL is used to fetch the information from the database which is already stored there.

DDL (Data Definition Language)


DDL is used to define table schemas.

DCL (Data Control Language)


DCL is used for user & permission management. It controls the access to the database.

DML (Data Manipulation Language)


DML is used for inserting, updating and deleting data from the database.

Copyright Disclaimer

Fair use of a Copyrighted Work as defined in section 185 of RA 8293, which states, “The fair use of copying, criticism, comment, news reporting, teaching including multiple copies
for classroom use, research and similar purposes is not infringement of copyright.” These lecture handouts are prepared and compiled solely for students of Pamantasan ng
Cabuyao (PnC) under the College of Computing and Engineering enrolled in Database Management System for AY 2020-2021.
Page 1
Pamantasan ng Cabuyao
College of Computing and Engineering
1ST SEMESTER 2020 – 2021

What is a Query

A Query is a set of instruction given to the database management system, which tells RDBMS what
information you would like to get from the database.

Note: Do not worry about learning the queries now, this is just an introduction. We will learn how to write
queries in detail in the next part.

For e.g. to fetch the employee name from the database table EMPLOYEE, we write the SQL Query like this:

SELECT employee_name from EMPLOYEE;

TABLE

A relational database system contains one or more objects called tables. The data or information for the
database are stored in these tables. Tables are uniquely identified by their names and are comprised of
columns and rows. Columns contain the column name, data type, and any other attributes for the column.
Rows contain the records or data for the columns. Here is a sample table called "weather".

city, state, high, and low are the columns. The rows contain the data for this table:

Weather

city state high low

Phoenix Arizona 105 90

Tucson Arizona 101 92

Flagstaff Arizona 88 69

San Diego California 77 60

New
Albuquerque 80 72
Mexico

Copyright Disclaimer

Fair use of a Copyrighted Work as defined in section 185 of RA 8293, which states, “The fair use of copying, criticism, comment, news reporting, teaching including multiple copies
for classroom use, research and similar purposes is not infringement of copyright.” These lecture handouts are prepared and compiled solely for students of Pamantasan ng
Cabuyao (PnC) under the College of Computing and Engineering enrolled in Database Management System for AY 2020-2021.
Page 2
Pamantasan ng Cabuyao
College of Computing and Engineering
1ST SEMESTER 2020 – 2021

Since a single server can support many databases, each containing many tables, with each table having a
variety of columns, it’s easy to get lost when you’re working with databases. These commands will help figure
out what’s available:

 SHOW DATABASES;
 SHOW TABLES IN database;
 SHOW COLUMNS IN table;
 DESCRIBE table; - shows the columns and their types

Variable Types

SQL supports a very large number of different formats for internal storage of information.

Numeric

 INTEGER, SMALLINT, BIGINT


 NUMERIC(w,d), DECIMAL(w,d) - numbers with width w and d decimal places
 REAL, DOUBLE PRECISION - machine and database dependent
 FLOAT(p) - floating point number with p binary digits of precision

Character

 CHARACTER(L) - a fixed-length character of length L


 CHARACTER VARYING(L) or VARCHAR(L) – supports maximum length of L

Binary

 BIT(L), BIT VARYING(L) - like corresponding characters


 BINARY LARGE OBJECT(L) or BLOB(L)

Temporal

 DATE
 TIME
 TIMESTAMP

Copyright Disclaimer

Fair use of a Copyrighted Work as defined in section 185 of RA 8293, which states, “The fair use of copying, criticism, comment, news reporting, teaching including multiple copies
for classroom use, research and similar purposes is not infringement of copyright.” These lecture handouts are prepared and compiled solely for students of Pamantasan ng
Cabuyao (PnC) under the College of Computing and Engineering enrolled in Database Management System for AY 2020-2021.
Page 3
Pamantasan ng Cabuyao
College of Computing and Engineering
1ST SEMESTER 2020 – 2021

CREATE TABLE statement

Suppose we have data measured on the height and weight of children over a range of ages. The first step is
deciding on the appropriate variable types, and creating the table with the CREATE TABLE command.

CREATE TABLE kids(id CHAR(6),


race SMALLINT, age DECIMAL(6,3),
height DECIMAL(7,3), weight DECIMAL(7,3),
sex SMALLINT);

Selecting Data

The select statement is used to query the database and retrieve selected data that match the criteria that you
specify. Here is the format of a simple select statement:

select "column1"
[,"column2",etc]
from "tablename"
[where "condition"];
[] = optional

The column names that follow the select keyword determine which columns will be returned in the results.
You can select as many column names that you'd like, or you can use a "*" to select all columns.

The table name that follows the keyword from specifies the table that will be queried to retrieve the desired
results. The where clause (optional) specifies which data values or rows will be returned or displayed, based
on the criteria described after the keyword where.

Conditional selections used in the where clause:

= Equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
<> Not equal to
LIKE *See note below

The LIKE pattern matching operator can also be used in the conditional selection of the where clause. Like is
a very powerful operator that allows you to select only rows that are "like" what you specify.

The percent sign "%" can be used as a wild card to match any possible character that might appear before or
after the characters specified. For example:
Copyright Disclaimer

Fair use of a Copyrighted Work as defined in section 185 of RA 8293, which states, “The fair use of copying, criticism, comment, news reporting, teaching including multiple copies
for classroom use, research and similar purposes is not infringement of copyright.” These lecture handouts are prepared and compiled solely for students of Pamantasan ng
Cabuyao (PnC) under the College of Computing and Engineering enrolled in Database Management System for AY 2020-2021.
Page 4
Pamantasan ng Cabuyao
College of Computing and Engineering
1ST SEMESTER 2020 – 2021

select first, last, city


from empinfo
where first LIKE 'Er%';

This SQL statement will match any first names that start with 'Er'. Strings must be in single quotes. Or you
can specify,

select first, last


from empinfo
where last LIKE '%s';

This statement will match any last names that end in a 's'.

select * from empinfo


where first = 'Eric';

This will only select rows where the first name equals 'Eric' exactly.

Sample Table: empinfo

first last id age city state

John Jones 99980 45 Payson Arizona

Mary Jones 99982 25 Payson Arizona

Eric Edwards 88232 32 San Diego California

Mary Ann Edwards 88233 32 Phoenix Arizona

Ginger Howell 98002 42 Cottonwood Arizona

Sebastian Smith 92001 23 Gila Bend Arizona

Gus Gray 22322 35 Bagdad Arizona

Mary Ann May 32326 52 Tucson Arizona

Erica Williams 32327 60 Show Low Arizona

Leroy Brown 32380 22 Pinetop Arizona

Copyright Disclaimer

Fair use of a Copyrighted Work as defined in section 185 of RA 8293, which states, “The fair use of copying, criticism, comment, news reporting, teaching including multiple copies
for classroom use, research and similar purposes is not infringement of copyright.” These lecture handouts are prepared and compiled solely for students of Pamantasan ng
Cabuyao (PnC) under the College of Computing and Engineering enrolled in Database Management System for AY 2020-2021.
Page 5
Pamantasan ng Cabuyao
College of Computing and Engineering
1ST SEMESTER 2020 – 2021

Elroy Cleaver 32382 22 Globe Arizona

Comparison Operators

In SQL, the WHERE clause allows you to operate on subsets of a table. The following comparison operators
are

available:

 Usual logical operators: < > <= >= = <>


 BETWEEN used to test for a range
 IN used to test group membership
 Keyword NOT used for negation
 LIKE operator allows wildcards
o _ means single character, % means anything
o SELECT salary WHERE name LIKE ’Fred %’;
 RLIKE operator allows regular expressions
 Use AND(&&) and OR(||) to combine conditions

Updating a Table

To change some of the values of columns of a table, you can use the UPDATE command. Changes are provided
as a comma-separated list of column/value pairs. For example, to add one to the weight of an observation in
the kids table where id is 101311 and age is between 9 and 10, we could use:

UPDATE kids SET weight = weight + 1 WHERE id=’101311’ AND age BETWEEN 9 and 10;

Be careful with UPDATE, because if you don’t provide a WHERE clause, all the rows of the table will be
changed.

Create Database

The CREATE DATABASE statement is used to create a new SQL database.

Syntax

CREATE DATABASE databasename;

CREATE DATABASE Example

The following SQL statement creates a database called "5CPEDB":


Copyright Disclaimer

Fair use of a Copyrighted Work as defined in section 185 of RA 8293, which states, “The fair use of copying, criticism, comment, news reporting, teaching including multiple copies
for classroom use, research and similar purposes is not infringement of copyright.” These lecture handouts are prepared and compiled solely for students of Pamantasan ng
Cabuyao (PnC) under the College of Computing and Engineering enrolled in Database Management System for AY 2020-2021.
Page 6
Pamantasan ng Cabuyao
College of Computing and Engineering
1ST SEMESTER 2020 – 2021

Example : CREATE DATABASE 5CPEDB;

Tip: Make sure you have admin privilege before creating any database. Once a database is created, you can
check it in the list of databases with the following SQL command: SHOW DATABASES;

The SQL DROP DATABASE Statement

The DROP DATABASE statement is used to drop an existing SQL database.

Syntax : DROP DATABASE databasename;

Note: Be careful before dropping a database. Deleting a database will result in loss of complete information
stored in the database!

The following SQL statement drops the existing database "testDB":

Example :DROP DATABASE testDB;

Tip: Make sure you have admin privilege before dropping any database. Once a database is dropped, you can
check it in the list of databases with the following SQL command: SHOW DATABASES;

The SQL DROP TABLE Statement

The DROP TABLE statement is used to drop an existing table in a database.

Syntax : DROP TABLE table_name;

Note: Be careful before dropping a table. Deleting a table will result in loss of complete information stored in
the table!

SQL ALTER TABLE Statement

The ALTER TABLE statement is used to add, delete, or modify columns in an existing table. The ALTER
TABLE statement is also used to add and drop various constraints on an existing table.

ALTER TABLE - ADD Column

To add a column in a table, use the following syntax:

Copyright Disclaimer

Fair use of a Copyrighted Work as defined in section 185 of RA 8293, which states, “The fair use of copying, criticism, comment, news reporting, teaching including multiple copies
for classroom use, research and similar purposes is not infringement of copyright.” These lecture handouts are prepared and compiled solely for students of Pamantasan ng
Cabuyao (PnC) under the College of Computing and Engineering enrolled in Database Management System for AY 2020-2021.
Page 7
Pamantasan ng Cabuyao
College of Computing and Engineering
1ST SEMESTER 2020 – 2021

ALTER TABLE table_name


ADD column_name datatype;

The following SQL adds an "Email" column to the "Customers" table:

Example :

ALTER TABLE Customers


ADD Email varchar(255);

To delete a column in a table, use the following syntax (notice that some database systems don't allow
deleting a column):

ALTER TABLE table_name


DROP COLUMN column_name;

The following SQL deletes the "Email" column from the "Customers" table:

Example

ALTER TABLE Customers


DROP COLUMN Email;

ALTER TABLE - ALTER/MODIFY COLUMN

To change the data type of a column in a table, use the following syntax:

SQL Server / MS Access:

ALTER TABLE table_name


ALTER COLUMN column_name datatype;

My SQL / Oracle (prior version 10G):

ALTER TABLE table_name


MODIFY COLUMN column_name datatype;

Name Score

Copyright Disclaimer

Fair use of a Copyrighted Work as defined in section 185 of RA 8293, which states, “The fair use of copying, criticism, comment, news reporting, teaching including multiple copies
for classroom use, research and similar purposes is not infringement of copyright.” These lecture handouts are prepared and compiled solely for students of Pamantasan ng
Cabuyao (PnC) under the College of Computing and Engineering enrolled in Database Management System for AY 2020-2021.
Page 8
Pamantasan ng Cabuyao
College of Computing and Engineering
1ST SEMESTER 2020 – 2021

Section Instructor Date

ACTIVITY 1

I. Perform the following statements .

Select statement exercises “select statements to”:

1) Display the first name and age for everyone that's in the table.
2) Display the first name, last name, and city for everyone that's not from Payson.
3) Display all columns for everyone that is over 40 years old.
4) Display the first and last names for everyone whose last name ends in an "ay".
5) Display all columns for everyone whose first name equals "Mary".
6) Display all columns for everyone whose first name contains "Mary".

II. Create Table Exercise


You have just started a new company. It is time to hire some employees. You will need to create a
table that will contain the following information about your new employees: firstname, lastname,
title, age, and salary. After you create the table, you should receive a small form on the screen
with the appropriate column names. If you are missing any columns, you need to double check
your SQL statement and recreate the table. Once it's created successfully, go to the "Insert" lesson.

IMPORTANT: When selecting a table name, it is important to select a unique name that no one
else will use or guess. Your table names should have an underscore followed by your initials and
the digits of your birth day and month. For example, Tom Smith, who was born on November 2nd,
would name his table myemployees_ts0211 Use this convention for all of the tables you create.
Your tables will remain on a shared database until you drop them, or they will be cleaned up if
they aren't accessed in 4-5 days. If "support" is good, I hope to eventually extend this to at least
one week. When you are finished with your table, it is important to drop your table

ANSWER KEY

Copyright Disclaimer

Fair use of a Copyrighted Work as defined in section 185 of RA 8293, which states, “The fair use of copying, criticism, comment, news reporting, teaching including multiple copies
for classroom use, research and similar purposes is not infringement of copyright.” These lecture handouts are prepared and compiled solely for students of Pamantasan ng
Cabuyao (PnC) under the College of Computing and Engineering enrolled in Database Management System for AY 2020-2021.
Page 9
Pamantasan ng Cabuyao
College of Computing and Engineering
1ST SEMESTER 2020 – 2021

1.Selecting Data Answers


Display everyone's first name and their age for everyone that's in table.
select first,
age
from empinfo;

2.Display the first name, last name, and city for everyone that's not from Payson.
select first,
last,
city
from empinfo
where city <>
'Payson';

3.Display all columns for everyone that is over 40 years old.


select * from empinfo
where age > 40;

4. Display the first and last names for everyone whose last name ends in an "ay".

select first, last from empinfo


where last LIKE '%ay';
a. Display all columns for everyone whose first name equals "Mary".
select * from empinfo
where first = 'Mary';
b. Display all columns for everyone whose first name contains "Mary".

select * from empinfo


where first LIKE '%Mary%';

II.
Your create statement should resemble:
create table myemployees_ts0211 (firstname varchar(30),
lastname varchar(30), title varchar(30),
age number(2), salary number(8,2) );

Copyright Disclaimer

Fair use of a Copyrighted Work as defined in section 185 of RA 8293, which states, “The fair use of copying, criticism, comment, news reporting, teaching including multiple copies
for classroom use, research and similar purposes is not infringement of copyright.” These lecture handouts are prepared and compiled solely for students of Pamantasan ng
Cabuyao (PnC) under the College of Computing and Engineering enrolled in Database Management System for AY 2020-2021.
Page 10

You might also like