Upload
mariel-asunsion
View
214
Download
0
Embed Size (px)
Citation preview
7/23/2019 ComE 223 (Slide 2).pdf
http://slidepdf.com/reader/full/come-223-slide-2pdf 1/20
SQL
Structure Query Language
7/23/2019 ComE 223 (Slide 2).pdf
http://slidepdf.com/reader/full/come-223-slide-2pdf 2/20
is mainly classified into Data Definition
Language (DDL) and Data Manipulation
Language (DML).
ANSI: “The standard language for relational database management systems”
7/23/2019 ComE 223 (Slide 2).pdf
http://slidepdf.com/reader/full/come-223-slide-2pdf 3/20
• Common RDBMS that uses SQL
– Oracle
– Sybase – Microsoft SQL Server
– Access
–Ingres
– MySQL
Although they
used SQL, they
have their own
proprietary
extensions that
are only used
w/in their
systems
But common are
the standard SQL
Commands
7/23/2019 ComE 223 (Slide 2).pdf
http://slidepdf.com/reader/full/come-223-slide-2pdf 4/20
Using SQL as a DDL (data definition
language)
• means creating a database, creating tables and adding them to a
database, updating the design of existing tables, and removing
tables from a database.
7/23/2019 ComE 223 (Slide 2).pdf
http://slidepdf.com/reader/full/come-223-slide-2pdf 5/20
Database tables• A RDBMS contains : one or more objects
tables (data or information is stored)
Course Code Course Description Units Co-Requisite
ComE 223 Advance Programming 3 ComE 223L
Chem 4 Chemistry 3 Chem4L
EM 111 Algebra 5 none
Are uniquely identified by
their names.
Comprises of rows And columns
7/23/2019 ComE 223 (Slide 2).pdf
http://slidepdf.com/reader/full/come-223-slide-2pdf 6/20
CREATE DATABASE
• Statement can be used to create a database
• Syntax: CREATE DATABASE <databaseName>
Example:
The ff. statement created a database named myDB
Create Database myDB
7/23/2019 ComE 223 (Slide 2).pdf
http://slidepdf.com/reader/full/come-223-slide-2pdf 7/20
CREATE TABLE• statement creates a table and adds it to the database
• Syntax:
CREATE TABLE <tableName> (colDefinition,…, colDefinition)
– Each columnDefinition is of the form ColumnName columnType
• Common Data types:
– Char(n) - An n character text string
– Integer - An integer value
– Float - A floating point value
– Bit - A boolean (1 or 0) value
– Date - A date value
–
Time - A time value
7/23/2019 ComE 223 (Slide 2).pdf
http://slidepdf.com/reader/full/come-223-slide-2pdf 8/20
Example of create table
CREATE TABLE Friends(
name char(100),
address char(50),bday date
)
7/23/2019 ComE 223 (Slide 2).pdf
http://slidepdf.com/reader/full/come-223-slide-2pdf 9/20
ALTER TABLE (mysql syntax)
• Rename column
• Add column
•
Change column location• Delete column
7/23/2019 ComE 223 (Slide 2).pdf
http://slidepdf.com/reader/full/come-223-slide-2pdf 10/20
The DROP TABLE Statement
• DROP TABLE tableName
– deletes a table permanently from the database
7/23/2019 ComE 223 (Slide 2).pdf
http://slidepdf.com/reader/full/come-223-slide-2pdf 11/20
Using SQL as a Data Manipulation
Language
• One of SQL Primary use
– Update data
• inserting new rows into a database,
• deleting rows from a database, and
• modifying/updating existing rows.
7/23/2019 ComE 223 (Slide 2).pdf
http://slidepdf.com/reader/full/come-223-slide-2pdf 12/20
INSERT
• INSERT INTO tableName VALUES ('values 1 ' ,…, 'valuen' )
• Values should be surrounded by single quotes.
• Add a complete row
• Alternative or Add a partial row
– INSERT INTO tableName(columnName1,….,columnNamem) VALUE
('value1',…,'valuem')
7/23/2019 ComE 223 (Slide 2).pdf
http://slidepdf.com/reader/full/come-223-slide-2pdf 13/20
Delete
• deletes a row from a table
• DELETE FROM tableName [WHERE condition]
•
All rows of the table that meet the conditionof the WHERE clause are deleted from the
table.
• If the WHERE clause is omitted, all rows of the
table are deleted
7/23/2019 ComE 223 (Slide 2).pdf
http://slidepdf.com/reader/full/come-223-slide-2pdf 14/20
Update
• used to update an existing row of a table
• UPDATE tableName SET columnName1 ='value1", …...,columnName = 'value' [WHERE
condition]• All the rows of the table that satisfy the
condition of the WHERE clause are updated by
setting the value of the specified values.• If the WHERE clause is omitted, all rows of the
table are updated.
7/23/2019 ComE 223 (Slide 2).pdf
http://slidepdf.com/reader/full/come-223-slide-2pdf 15/20
Using SQL as a Data Query Language
• retrieving data contained in a database
• SELECT columnList1 FROM table1 ,…, tablem [WHEREcondition]
• Select/retrieve data for – Comparison
– Range
– Set membership
– Pattern matching
– Null value test
– Compound search condition
7/23/2019 ComE 223 (Slide 2).pdf
http://slidepdf.com/reader/full/come-223-slide-2pdf 16/20
Relational and Logical Comparisons
• Comparison Test- used to compare equal to, not equal to, less
than, less than or equal to, greater than, greater than or equal
– Use of relational operators
– =, <>,<,<=,>=,>
• Range Test- test whether the value lies within the range or not
– Between and not between
• Set membership test-test whether the value is present in the
list of values
– IN, NOT IN
7/23/2019 ComE 223 (Slide 2).pdf
http://slidepdf.com/reader/full/come-223-slide-2pdf 17/20
• Pattern Matching Test- test is employed to find a string
starting with a character/or several characters
– LIKE (%, _ )
– E.G: WHERE NAME LIKE ‘A%BC_’
• NULL VALUE TEST-test whether the column contains null value.
– IS NULL
• COMPOUND SEARCH CONDITON- test for more than
one condition
– AND/OR/NOT is used
• ORDER BY-retrieve data in some order
– asc,desc
7/23/2019 ComE 223 (Slide 2).pdf
http://slidepdf.com/reader/full/come-223-slide-2pdf 18/20
• Union-is used to combine two or more tables
• Criteria
– The tables must contain same number of columns.
– Data type of each should match that of the other
– Neither can be stored but combined can be
stored.
7/23/2019 ComE 223 (Slide 2).pdf
http://slidepdf.com/reader/full/come-223-slide-2pdf 19/20
Functions
• used to act upon single column or
multicolumn for a specified job.
7/23/2019 ComE 223 (Slide 2).pdf
http://slidepdf.com/reader/full/come-223-slide-2pdf 20/20
Assignment (1 whole yellow paper)
• Describe, explain, and give example of the ff.concepts. Handwritten, write legibly. – Alias table
– Distinct
– Union
– Join (Equi join, non equi join, outer join)
– Query on queries
– Rules for Multi Table Query Processing
• Essay1. How SQL can be used as a Data Query Language?
2. How SQL can be used for Data Manipulation?