Assignment 5
Assignment 5
INTRODUCTION TO SQL –
What Is SQL?
When a user wants to get some information from a database file, he
can issue a query.
A query is a user–request to retrieve data or information with a
certain condition.
SQL is a query language that allows user to specify the conditions.
(instead of algorithms)
Concept Of SQL -
The user specifies a certain condition.
The program will go through all the records in the database file and
select those records that satisfy the condition.(searching).
Statistical information of the data.
The result of the query will then be stored in form of a table.
Data Types Of SQL :
Data Manipulation
Data Definition
Data Administration
All are expressed as an SQL statement or command.
Notations To Define SQL Statements:
UPPER-CASE letters represents reserved words.
Lower-case letters represents user-defined words.
| indicates a choice among alternatives; (e.g. a | b | c).
{ } indicates a required element.
[ ] indicates an optional element.
… indicates optional repetition of an item zero or more times.
Underlined words represent default values.
SQL Requirements:
SQL Must be embedded in a programming language, or used with a
4GL like VB
SQL is a free form language so there is no limit to the the number of
words per line or fixed line break.
Syntax statements, words or phrases are always in lower case;
keywords are in uppercase.
Columns Rows
describe descr
one ibe
characterist the
ic of the Occu
entity
Types of Tables rrenc
e of
There are two types of tables which make up a relational database in
SQL.
an management
User Tables: contain information that is the database
system
Entity
System Tables: contain the database description, kept up to date by
DBMS itself
Relation Table
Tuple Row
Attribute Colum
n
Creating Tables
Empty tables are constructed using the CREATE TABLE statement.
Data must be entered later using INSERT.
CREATE TABLE S ( SNO CHAR(5),
SNAME CHAR(20),
STATUS DECIMAL(3),
CITY CHAR(15),
PRIMARY KEY (SNO)
A table name and unique column names must be specified.
Columns which are defined as primary keys will never have two
rows with the same key value.
Primary key may consist of more than one column (values unique in
combination) called composite key.
S1 SMITH 20 LONDON
S2 JONES 10 PARIS
S3 BLAKE 30 PARIS
S4 CLARK 20 LONDON
Table P
S1 P1 300
S1 P2 200
S2 P1 300
S3 P2 200
S4 P4 400
auth_name auth_city
Using SQL:
SQL statements can be embedded into a program (cgi or perl script,
Visual Basic, MS Access)
OR
SQL statements can be entered directly at the command prompt of
the SQL software being used (such as mySQL)
To begin, you must first CREATE a database using the following SQL
statement:
CREATE DATABASE data base_name
Depending on the version of SQL being used the following statement is
needed to begin using the database:
USE database_name
Using SQL:
To delete data from a table, use the DELETE statement:
DELETE from authors WHERE auth_name=‘John Smith’
auth_id auth_name auth_city auth_state
Using SQL:
To change a table in a database use ALTER TABLE. ADD adds a
Typ Initiali
characteristic.
e
ALTER TABLE authors ADD birth_date datetime null. zer
auth_id auth_name auth_city auth_state
Using SQL:
The DROP statement is also used to delete an entire database.
DROP DATABASE authors
Advantages of SQL :
1. View the data without storing the data into the object.
2. Restrict the view of a table i.e. can hide some of columns in the
tables.
3. Join two or more tables and show it as one object to user.
4. Restrict the access of a table so that nobody can insert the rows
into the table.
5. Data independence , Currency, Improved security , Reduced
complexity , Convenience ,Customization , Data integrity.
Disadvantages of SQL :
1. Can not use DML operations on this.
2. When table is dropped view becomes inactive.. it depends on the
table objects.
3. It is an object, so it occupies space
4. Update restriction, Structure restriction , Performance
Conclusion:
SQL is a versatile language that can integrate with numerous 4GL
languages and applications
SQL simplifies data manipulation by reducing the amount of code
required.
More reliable than creating a database using files with linked-list
implementation.