Published on

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. SQL<br />INTRODUCTION TO SQL<br />What is SQL?<br /><ul><li>SQL stands for Structured Query Language
  2. 2. SQL lets you access and manipulate databases
  3. 3. SQL is an ANSI (American National Standards Institute) standard
  4. 4. It is the most commonly used relational database language today.
  5. 5. SQL works with a variety of different fourth-generation (4GL) programming languages, such as Visual Basic.</li></li></ul><li>What Can SQL do?<br /><ul><li>SQL can execute queries against a database
  6. 6. SQL can retrieve data from a database
  7. 7. SQL can insert records in a database
  8. 8. SQL can update records in a database
  9. 9. SQL can delete records from a database
  10. 10. SQL can create new databases
  11. 11. SQL can create new tables in a database
  12. 12. SQL can create stored procedures in a database
  13. 13. SQL can create views in a database
  14. 14. SQL can set permissions on tables, procedures, and views</li></li></ul><li>SQL is used for:<br /><ul><li>Data Manipulation
  15. 15. Data Definition
  16. 16. Data Administration
  17. 17. 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 />
  18. 18. 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)
  19. 19. A server-side scripting language, like PHP or ASP
  20. 20. SQL
  21. 21. HTML / CSS
  22. 22. 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 />
  23. 23. 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 />
  24. 24. SQL<br />Design<br /><ul><li>SQL represents all information in the form of tables
  25. 25. Supports three relational operations: selection, projection, and join. These are for specifying exactly what data you want to display or use
  26. 26. SQL is used for data manipulation, definition and administration</li></ul>6<br />
  27. 27. 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 />
  28. 28. 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 />
  29. 29. <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.
  30. 30. Data Manipulation Language (DML) - These SQL commands are used for storing, retrieving, modifying, and deleting data. These commands are SELECT, INSERT, UPDATE, and DELETE.
  31. 31. Transaction Control Language (TCL) - These SQL commands are used for managing changes affecting the data. These commands are COMMIT, ROLLBACK, and SAVEPOINT.
  32. 32. 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.
  33. 33. 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.
  34. 34. 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 />
  35. 35. <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.
  36. 36. 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 />
  37. 37. <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
  38. 38. 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 />
  39. 39. 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.
  40. 40. 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
  41. 41. 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
  42. 42. 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>