Structured Query Language
Structured Query Language
An Overview of SQL
SQL stands for Structured Query Language. It is the most commonly used relational database language today. SQL works with a variety of different fourth-generation (4GL) programming languages, such as Visual Basic. When a user wants to get some information from a database file, he can issue a query A query is a userrequest 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.
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.
Provide a method for differentiating between unknown values and nulls (zero or blank)
Support Mechanisms for integrity, authorization, transactions, and recovery
SQL
Design
SQL represents all information in the form of tables
Supports three relational operations: selection, projection, and join. These are for specifying exactly what data you want to display or use
Table Design
Columns describe one characteristic of the entity Rows describe the Occurrence of an Entity
Address 123 Main Street 456 Second Street 789 Third Ave
pub_name
New Age Books Binnet & Hardley Algodata Infosys
address
1 1st Street 2 2nd Street 3 3rd Street
state
MA DC CA
pub_name
New Age Books Binnet & Hardley Algodata Infosys
address
1 1st Street 2 2nd Street 3 3rd Street
state
MA DC CA
Data Input
Putting data into a table is accomplished using the keyword INSERT
INSERT INTO publishers VALUES (0010, pragmatics, 4 4th Ln, chicago, il) Keyword
Variable
pub_name New Age Books Pragmatics Binnet & Hardley New Age Books Algodata Hardley Binnet & Infosys Algodata Infosys
address 1 1st Street 4 4th Ln 2 2nd Street 1 1st Street 3 3rd Street 2 2nd Street 3 3rd Street
state MA IL DC MA CA DC CA
Types of Tables
There are two types of tables which make up a relational database in SQL
User Tables: contain information that is the database management system System Tables: contain the database description, kept up to date by DBMS itself Relation Table
Tuple Attribute Row Column
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)
Using SQL
To begin, you must first CREATE a database using the following SQL statement:
USE database_name
Using SQL
To create a table in the current database, use the CREATE TABLE keyword CREATE TABLE authors (auth_id int(9) not null, auth_name char(40) not null)
auth_id (9 digit int) auth_name (40 char string)
Using SQL
To insert data in the current table, use the keyword INSERT INTO
INSERT INTO authors values(000000001, John Smith)
Using SQL
If you only want to display the authors name and city from the following table:
auth_id auth_name auth_city Dearborn Taylor auth_state MI MI 123456789 Jane Doe 000000001 John Smith
auth_city Dearborn
Taylor
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 Dearborn Taylor auth_state MI MI
Using SQL
To Update information in a database use the UPDATE keyword UPDATE authors SET auth_name=hello
auth_id auth_name auth_city Dearborn Taylor auth_state MI MI
Using SQL
To change a table in a database use ALTER TABLE. ADD adds a characteristic.
Type ALTER TABLE authors ADD birth_date datetime null Initializer
auth_id
auth_name
auth_state MI MI
birth_date . .
Using SQL
To delete a column or row, use the keyword DROP ALTER TABLE authors DROP birth_date
auth_id auth_name auth_city Dearborn Taylor auth_state MI MI auth_state . .
Using SQL
The DROP statement is also used to delete an entire database. DROP DATABASE authors
auth_id auth_name auth_city Dearborn Taylor auth_state MI MI
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