SQL is an ANSI (American National Standards Institute) standard
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.
What Can SQL do?
SQL can execute queries against a database
SQL can retrieve data from a database
SQL can insert records in a database
SQL can update records in a database
SQL can delete records from a 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, procedures, and views
SQL is used for:
All are expressed as an SQL statement or command.
SQL REQUIREMENTS: SQL Must be embedded in a programming language, or used with a 4GL like VB 2. SQL is a free form language so there is no limit to the the number of words per line or fixed line break. 3. Syntax statements, words or phrases are always in lower case; keywords are in uppercase.
Using SQL in Your Web Site To build a web site that shows some data from a database, you will need the following:
An RDBMS database program (i.e. MS Access, SQL Server, MySQL)
A server-side scripting language, like PHP or ASP
HTML / CSS
RDBMS stands for Relational Database Management System. RDBMS is the basis for SQL, and for all modern database systems like MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access. The data in RDBMS is stored in database objects called tables. A table is a collection of related data entries and it consists of columns and rows.
SQL is a Relational Database A Fully Relational Database Management System must: Represent all info in database as tables Keep logical representation of data independent from its physical storage characteristics Use one high-level language for structuring, querying, and changing info in the database Support the main relational operations Support alternate ways of looking at data in tables Provide a method for differentiating between unknown values and nulls (zero or blank) Support Mechanisms for integrity, authorization, transactions, and recovery
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
SQL is used for data manipulation, definition and administration
SQL SYNTAX SQL Database Tables A database most often contains one or more tables. Each table is identified by a name (e.g. "Customers" or "Orders"). Tables contain records (rows) with data. Below is an example of a table called "Persons": INSERT TABLE
SQL Commands: SQL commands are instructions used to communicate with the database to perform specific task that work with data. SQL commands can be used not only for searching the database but also to perform various other functions like, for example, you can create tables, add data to tables, or modify data, drop the table, set permissions for users. SQL commands are grouped into four major categories depending on their functionality:
Data Definition Language (DDL) - These SQL commands are used for creating, modifying, and dropping the structure of database objects. The commands are CREATE, USE, ALTER, DROP, RENAME, and TRUNCATE.
Data Manipulation Language (DML) - These SQL commands are used for storing, retrieving, modifying, and deleting data. These commands are SELECT, INSERT, UPDATE, and DELETE.
Transaction Control Language (TCL) - These SQL commands are used for managing changes affecting the data. These commands are COMMIT, ROLLBACK, and SAVEPOINT.
Data Control Language (DCL) - These SQL commands are used for providing security to database objects. These commands are GRANT and REVOKE.
The data definition language (DDL)
used o create and destroy databases and data objects. These commands will primarily be used by database administrators during the set up and removal phases of a database project.
CREATE TABLE personal_info (first_name char (20) not null, last_name char(20)not null, employee_id not null)
Establishes a table titled “personal_info” in the current database. The table contains three attributes: first_name, last_name and employee_id.
Issue commands that will affect the employees database.
!!!It is important to always be conscious of the database you are working in before issuing SQL commands that manipulate data.
ALTER TABLE personal_info ADD salary money null;
adds new attribute to the personal_info table – an employees' salary. The money argument specifies that an employee’s salary will be stored using dollars and cents formats. The “null” keyword tell the database that it’s ok for this field to contain no value for any given argument.
DROP TABLE personal_info;
removing the personal_info table permanently
DROP DATABASE employees;
remove the entire employees database
!!! Remember that the DROP command removes the entire data structures from your database. If you want to remove individual records, use the DELETE command of the DML.
RENAME dpll To dept
You can execute the above statement only if you are the owner of the object you are renaming
Another DDL statement is the TRUNCATE TABLE statement, which removes all rows from the table and releases the storage space used by that table. You can truncate a table in the following manner: TRUNCATE TABLE dept
The TRUNCATE TABLE statement removes data and releases storage space but does not remove the structure of the table, while the DELETE statement removes data, does not release storage space, and does not remove the structure
Being a DDL statement, you cannot roll back the TRUNCATE TABLE
statement. You can execute the TRUNCATE TABLE statement only if you are the owner of the table you are truncating or you have been granted the DELETE TABLE privilege. The TRUNCATE TABLE statement is faster than the DELETE statement because no rollback information is generated for the TRUNCATE TABLE statement unlike for the DELETE statement.
The DATA MANIPULATION LANGUAGE (DML)
is used to retrieve, insert and modify the database information. These commands will be used by all database users during the routing operation of the database.
INSERT INTO personal_info values(‘bart’,’simpson’,1234,$45000) !! Note there are four values specified for the record. These corresponds with to the table attribute in the order they are defined: first_name, last_name, employee_id and salary.
SELECT * FROM personal_info
retrieves all of the information contained within the personal_info table. The asterisk (*) is used as a wildcard in SQL. It means “select everything from the personal_info table
SELECT last_name FROM personal_info
requires a list of the last names of all employees in the company.
SELECT * FROM personal_info WHERE salary > $50000
reviewing the personnel records of all highly paid employees. The above command retrieves all of the data contained within personal_info for records that have a salary value greater than $5000
UPDATE personal_info WHERE salary = salary *1.03
each year, the company gives all employees a 3% cost- of-living increase in their salary.
UPDATE personal_info SET salary = salary + $5000 WHERE employee_id = 1234
raising the salary of a CERTAIN employee by $5,000
DELETE FROM personal_info WHERE employee_id = 1234
removing record of a certain employee from the personal_info