Lecture5<br /> SQL “Structure Query Language”<br /><ul><li>Definitions
SQL “Structure Query Language”
Database Schema.
Data types.
Database constraints.
DDL (Data Definition Language)
Upcoming SlideShare
Loading in …5
×

Lecture5-SQL.docx

559 views
495 views

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
559
On SlideShare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
15
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Lecture5-SQL.docx

  1. 1. Lecture5<br /> SQL “Structure Query Language”<br /><ul><li>Definitions
  2. 2. SQL “Structure Query Language”
  3. 3. Database Schema.
  4. 4. Data types.
  5. 5. Database constraints.
  6. 6. DDL (Data Definition Language)
  7. 7. Create
  8. 8. Alter
  9. 9. Drop
  10. 10. Truncate
  11. 11. DML (Data Manipulation Language)
  12. 12. Insert
  13. 13. Update
  14. 14. Delete
  15. 15. Transaction Control (Commit – Rollback)
  16. 16. Data Retrieval (SELECT)
  17. 17. Queries (SELECT statement).
  18. 18. Select from on table.
  19. 19. Distinct keyword.
  20. 20. Derived Field.
  21. 21. Column alias.
  22. 22. Where clause.
  23. 23. Order by clause.</li></ul>Definitions<br /><ul><li>SQL “Structure Query Language”
  24. 24. Data Definition Language (DDL).
  25. 25. Data Manipulation Language (DML).
  26. 26. Data Control Language (DCL).
  27. 27. Database Schema</li></ul>A schema is a group of related objects in a database. There is one owner of a schema who has access to manipulate the structure of any object in the schema. A schema does not represent a person, although the schema is associated with a user that resides in the database.<br /><ul><li>Data types</li></ul>A data type determines the type of data that can be stored in a database column. The most commonly used data types are: <br />1. Alphanumeric: Data types used to store characters, numbers, special characters, or nearly any combination.<br />2. Numeric.<br />3. Date and Time. <br /><ul><li>Database constraints
  28. 28. Not Null
  29. 29. Primary Key
  30. 30. Unique Key
  31. 31. Referential Integrity ( FK )
  32. 32. Check
  33. 33. DDL (Data Definition Language)
  34. 34. Create Table
  35. 35. CREATE TABLE customer(ID int (15) Not Null,
  36. 36. First_Name char(50),Last_Name char(50),Address char(50),City char(50),Country char(25),Birth_Date date),
  37. 37. Primary Key (ID));
  38. 38. Alter Table
  39. 39. ALTER TABLE customer
  40. 40. ADD(Telchar(7));
  41. 41. ALTER TABLE customer
  42. 42. DROP COLUMN Country;
  43. 43. Drop Table: Remove table structure and all its data.
  44. 44. DROP TABLE customer;
  45. 45. Truncate Table: Remove All Rows in the table.
  46. 46. TRUNCATE TABLE customer;
  47. 47. DML (Data Manipulation Language)
  48. 48. Insert
  49. 49. INSERT INTO Customers
  50. 50. VALUES ( 10, ‘Saleh’, ‘Ahmed', ‘Moharam bak', ‘Alex.' , ’Egypt’ , ‘01/10/1975’));
  51. 51. INSERT INTO Persons (ID, First_Name, City)
  52. 52. VALUES (20, ‘Hassan', ‘Assuit');
  53. 53. Update
  54. 54. UPDATE customer
  55. 55. SET city = ‘Cairo’; /* this statement will update all rows of the table*/
  56. 56. UPDATE customer
  57. 57. SET city = ‘Cairo’
  58. 58. WHERE id = 10;
  59. 59. UPDATE customer
  60. 60. SET city = ‘Cairo’, Country = ‘Egypt’
  61. 61. WHERE id = 10; /* Update several columns in certain row*/
  62. 62. Delete
  63. 63. DELETE FROM customer;
  64. 64. /* This statement will Delete all rows of the table*/
  65. 65. DELETE FROM customer
  66. 66. WHERE id = 10; /* Delete one record only*/
  67. 67. Data Retrieval (SELECT)
  68. 68. Select <Attribute list >
  69. 69. From < Table list>
  70. 70. Where <Condition>
  71. 71. Group By <Expression>
  72. 72. Select from one table
  73. 73. SELECT *
  74. 74. FROMcustomers;
  75. 75. SELECT id, first_name , city
  76. 76. FROMcustomers;
  77. 77. Distinct keyword
  78. 78. SELECT distinct city
  79. 79. FROMcustomers;
  80. 80. Derived Field
  81. 81. SELECT salary * 1.7
  82. 82. FROMemployees;
  83. 83. Column alias
  84. 84. SELECT salary * 1.7 “Raised salary”
  85. 85. FROMemployees;
  86. 86. Where clause</li></ul>Comparison Operator:<br />= , > , >= , < , <= , <><br />Between …… AND ….. <br />IN (set) (Match any of a list of values) <br />All (set) (Match all values in the list)<br /> Like (Match a character Pattern) <br />Logical Operator:<br />AND , OR , NOT<br />Arithmatic Operator:<br />* , / , +, -<br />Examples<br /><ul><li>SELECT *
  87. 87. FROM customers
  88. 88. WHERE city = ‘Alex’;
  89. 89. Select last_name, salary</li></ul>from employees<br />where salary between 1000 and 3000;<br /><ul><li>Select emp_id, last_name, salary, manager_id </li></ul>From employees<br />where manager_id IN (100, 101, 200);<br /><ul><li>Select first_name </li></ul>from employees<br />where first_name Like ‘_s%’;<br /><ul><li>Order by clause
  90. 90. SELECT *
  91. 91. FROM customers
  92. 92. ORDER BY First_Name ;
  93. 93. /*This means sort by first name in Ascending order*/
  94. 94. SELECT *
  95. 95. FROM customers
  96. 96. ORDER BY First_Name , Last_Name DESC ;
  97. 97. /*This means sort by first name in Ascending order and Last_name in Descending order*/

×