All are expressed as an SQL statement or command.</li></ul>SQL REQUIREMENTS:<br />SQL Must be embedded in a programming language, or used with a 4GL like VB<br />2. SQL is a free form language so there is no limit to the the number of words per line or fixed line break.<br />3. Syntax statements, words or phrases are always in lower case; keywords are in uppercase.<br />
Using SQL in Your Web Site<br />To build a web site that shows some data from a database, you will need the following:<br /><ul><li>An RDBMS database program (i.e. MS Access, SQL Server, MySQL)
A server-side scripting language, like PHP or ASP
RDBMS</li></ul>RDBMS stands for Relational Database Management System.<br />RDBMS is the basis for SQL, and for all modern database systems like MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access.<br />The data in RDBMS is stored in database objects called tables.<br />A table is a collection of related data entries and it consists of columns and rows.<br />
SQL is a Relational Database<br />A Fully Relational Database Management System must:<br />Represent all info in database as tables<br />Keep logical representation of data independent from its physical storage characteristics<br />Use one high-level language for structuring, querying, and changing info in the database<br />Support the main relational operations<br />Support alternate ways of looking at data in tables<br />Provide a method for differentiating between unknown values and nulls (zero or blank)<br />Support Mechanisms for integrity, authorization, transactions, and recovery<br />
SQL<br />Design<br /><ul><li>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</li></ul>6<br />
SQL SYNTAX<br />SQL<br />Database Tables<br />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.<br />Below is an example of a table called "Persons":<br />INSERT TABLE<br />
SQL Commands:<br />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:<br />
<ul><li>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.</li></li></ul><li>The data definition language (DDL) <br /><ul><li>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 </li></ul>CREATE TABLE personal_info (first_name char (20) not null, last_name char(20)not null, employee_id not null)<br /><ul><li>Establishes a table titled “personal_info” in the current database. The table contains three attributes: first_name, last_name and employee_id.
USE</li></ul>USE employees;<br /><ul><li> Issue commands that will affect the employees database.</li></ul>!!!It is important to always be conscious of the database you are working in before issuing SQL commands that manipulate data.<br />
<ul><li> ALTER </li></ul>ALTER TABLE personal_info<br />ADD salary money null;<br /><ul><li> 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</li></ul>DROP TABLE personal_info;<br /><ul><li> removing the personal_info table permanently</li></ul>DROP DATABASE employees;<br /><ul><li> remove the entire employees database</li></ul>!!! 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.<br />
<ul><li> RENAME</li></ul>RENAME dpll To dept<br /><ul><li>You can execute the above statement only if you are the owner of the object you are renaming
TRUNCATE</li></ul>Another DDL statement is the TRUNCATE TABLE statement, which removes all rows from the table and<br />releases the storage space used by that table. You can truncate a table in the following manner:<br />TRUNCATE TABLE dept<br /><ul><li>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</li></li></ul><li><ul><li>Being a DDL statement, you cannot roll back the TRUNCATE TABLE</li></ul>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.<br />
The DATA MANIPULATION LANGUAGE (DML)<br /><ul><li> 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</li></ul>INSERT INTO personal_info<br />values(‘bart’,’simpson’,1234,$45000)<br />!! 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.<br /><ul><li> SELECT</li></ul>SELECT *<br />FROM personal_info<br /><ul><li> 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</li></li></ul><li>SELECT last_name<br />FROM personal_info<br /><ul><li> requires a list of the last names of all employees in the company.</li></ul>SELECT *<br />FROM personal_info<br />WHERE salary > $50000<br /><ul><li> 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</li></ul>UPDATE personal_info<br />WHERE salary = salary *1.03<br /><ul><li> each year, the company gives all employees a 3% cost- of-living increase in their salary.</li></li></ul><li>UPDATE personal_info<br />SET salary = salary + $5000<br />WHERE employee_id = 1234<br /><ul><li> raising the salary of a CERTAIN employee by $5,000
DELETE</li></ul>DELETE FROM personal_info<br />WHERE employee_id = 1234<br /><ul><li> removing record of a certain employee from the personal_info</li></li></ul><li>