Sql

2,282 views

Published on

Published in: Technology
  • Be the first to comment

Sql

  1. 1. Presented by : Ms. Nita Arora Kulachi Hansraj Model School Ashok Vihar SQL
  2. 2. SQL - History <ul><li>Structured Query Language </li></ul><ul><li>SEQUEL from IBM San Jose </li></ul><ul><li>ANSI 1992 Standard is the version used by most DBMS today (SQL92) </li></ul><ul><li>Basic language is standardized across relational DBMSs. Each system may have proprietary extensions to standard. </li></ul>
  3. 3. SQL Uses <ul><li>Database Definition and Querying </li></ul><ul><ul><li>Can be used as an interactive query language </li></ul></ul><ul><ul><li>Can be imbedded in programs </li></ul></ul><ul><li>Relational Calculus combines Select, Project and Join operations in a single command: SELECT </li></ul>
  4. 25. SQL Advanced
  5. 28. Aggregate Functions <ul><li>Count </li></ul><ul><li>Avg </li></ul><ul><li>SUM </li></ul><ul><li>MAX </li></ul><ul><li>MIN </li></ul><ul><li>Others may be available in different systems </li></ul>
  6. 30. Branch 2 3000000 Clementi Singapore Ng Wee Hiong 1000000 Jakarta_2 Jakarta George 4000000 Mona Jakarta Agus Arianto 1000000 Singapore_2 Singapore John Asset Director City Name
  7. 31. 4 3000000 Clementi Singapore Ng Wee Hiong 1000000 Jakarta_2 Jakarta George 4000000 Mona Jakarta Agus Arianto 1000000 Singapore_2 Singapore John Asset Director City Name
  8. 32. 6750000 3000000 Clementi Singapore Ng Wee Hiong 1000000 Jakarta_2 Jakarta George 4000000 Mona Jakarta Agus Arianto 1000000 Singapore_2 Singapore John Asset Director City Name
  9. 39. 3000000 Clementi Singapore Ng Wee Hiong 1000000 Jakarta_2 Jakarta George 4000000 Mona Jakarta Agus Arianto 1000000 Singapore_2 Singapore John Asset Director City Name
  10. 42. 3000000 Clementi Singapore Ng Wee Hiong 1000000 Jakarta_2 Jakarta George 4000000 Mona Jakarta Agus Arianto 1000000 Singapore_2 Singapore John Asset Director City Name
  11. 43. Views 3000000 Clementi Singapore Ng Wee Hiong 1000000 Jakarta_2 Jakarta George 4000000 Mona Jakarta Agus Arianto 1000000 Singapore_2 Singapore John Asset Director City Name 3000000 Clementi Singapore Ng Wee Hiong 1000000 Singapore_2 Singapore John Asset Director City Name
  12. 52. SQL Commands Syntax Review
  13. 53. SELECT <ul><li>Syntax: </li></ul><ul><ul><li>SELECT [DISTINCT] attr1, attr2,…, attr3 FROM rel1 r1, rel2 r2,… rel3 r3 WHERE condition1 {AND | OR} condition2 ORDER BY attr1 [DESC], attr3 [DESC] </li></ul></ul>
  14. 54. SELECT <ul><li>Syntax: </li></ul><ul><ul><li>SELECT a.author, b.title FROM authors a, bibfile b, au_bib c WHERE a.AU_ID = c.AU_ID and c.accno = b.accno ORDER BY a.author ; </li></ul></ul><ul><li>Examples in Access... </li></ul>
  15. 55. SELECT Conditions <ul><li>= equal to a particular value </li></ul><ul><li>>= greater than or equal to a particular value </li></ul><ul><li>> greater than a particular value </li></ul><ul><li><= less than or equal to a particular value </li></ul><ul><li><> not equal to a particular value </li></ul><ul><li>LIKE “*term*” (may be other wild cards in other systems) </li></ul><ul><li>IN (“opt1”, “opt2”,…,”optn”) </li></ul><ul><li>BETWEEN val1 AND val2 </li></ul><ul><li>IS NULL </li></ul>
  16. 56. Relational Algebra Selection using SELECT <ul><li>Syntax: </li></ul><ul><ul><li>SELECT * WHERE condition1 {AND | OR} condition2; </li></ul></ul>
  17. 57. Relational Algebra Projection using SELECT <ul><li>Syntax: </li></ul><ul><ul><li>SELECT [DISTINCT] attr1, attr2,…, attr3 FROM rel1 r1, rel2 r2,… rel3 r3; </li></ul></ul>
  18. 58. Relational Algebra Join using SELECT <ul><li>Syntax: </li></ul><ul><ul><li>SELECT * FROM rel1 r1, rel2 r2 WHERE r1.linkattr = r2.linkattr ; </li></ul></ul>
  19. 59. Subqueries <ul><li>SELECT SITES.[Site Name], SITES.[Destination no] </li></ul><ul><li>FROM SITES </li></ul><ul><li>WHERE sites.[Destination no] IN (SELECT [Destination no] from DEST where [avg temp (f)] >= 78); </li></ul><ul><li>Can be used as a form of JOIN. </li></ul>
  20. 60. Using Aggregate functions <ul><li>SELECT attr1, Sum(attr2) AS name FROM tab1, tab2 ... </li></ul><ul><li>GROUP BY attr1, attr3 HAVING condition; </li></ul>
  21. 61. CREATE Table <ul><li>CREATE TABLE table-name (attr1 attr-type PRIMARYKEY, attr2 attr-type,…,attrN attr-type); </li></ul><ul><li>Adds a new table with the specified attributes (and types) to the database. </li></ul>
  22. 62. Access Data Types <ul><li>Numeric (1, 2, 4, 8 bytes, fixed or float) </li></ul><ul><li>Text (255 max) </li></ul><ul><li>Memo (64000 max) </li></ul><ul><li>Date/Time (8 bytes) </li></ul><ul><li>Currency (8 bytes, 15 digits + 4 digits decimal) </li></ul><ul><li>Autonumber (4 bytes) </li></ul><ul><li>Yes/No (1 bit) </li></ul><ul><li>OLE (limited only by disk space) </li></ul><ul><li>Hyperlinks (up to 64000 chars) </li></ul>
  23. 63. Access Numeric types <ul><li>Byte </li></ul><ul><ul><ul><ul><li>Stores numbers from 0 to 255 (no fractions). 1 byte </li></ul></ul></ul></ul><ul><li>Integer </li></ul><ul><ul><ul><ul><li>Stores numbers from –32,768 to 32,767 (no fractions) 2 bytes </li></ul></ul></ul></ul><ul><li>Long Integer (Default) </li></ul><ul><ul><ul><ul><li>Stores numbers from –2,147,483,648 to 2,147,483,647 (no fractions). 4 bytes </li></ul></ul></ul></ul><ul><li>Single </li></ul><ul><ul><ul><ul><li>Stores numbers from -3.402823E38 to –1.401298E–45 for negative values and from 1.401298E–45 to 3.402823E38 for positive values. 4 bytes </li></ul></ul></ul></ul><ul><li>Double </li></ul><ul><ul><ul><ul><li>Stores numbers from –1.79769313486231E308 to –4.94065645841247E–324 for negative values and from 1.79769313486231E308 to 4.94065645841247E–324 for positive values. 15 8 bytes </li></ul></ul></ul></ul><ul><li>Replication ID </li></ul><ul><ul><ul><ul><li>Globally unique identifier (GUID) N/A 16 bytes </li></ul></ul></ul></ul>
  24. 64. Oracle Data Types <ul><li>CHAR (size) -- max 2000 </li></ul><ul><li>VARCHAR2(size) -- up to 4000 </li></ul><ul><li>DATE </li></ul><ul><li>DECIMAL, FLOAT, INTEGER, INTEGER(s), SMALLINT, NUMBER, NUMBER(size,d) </li></ul><ul><ul><li>All numbers internally in same format… </li></ul></ul><ul><li>LONG, LONG RAW, LONG VARCHAR </li></ul><ul><ul><li>up to 2 Gb -- only one per table </li></ul></ul><ul><li>BLOB, CLOB, NCLOB -- up to 4 Gb </li></ul><ul><li>BFILE -- file pointer to binary OS file </li></ul>
  25. 65. Creating a new table from existing tables <ul><li>Syntax: </li></ul><ul><ul><li>SELECT [DISTINCT] attr1, attr2,…, attr3 INTO newtablename FROM rel1 r1, rel2 r2,… rel3 r3 WHERE condition1 {AND | OR} condition2 ORDER BY attr1 [DESC], attr3 [DESC] </li></ul></ul>
  26. 66. ALTER Table <ul><li>ALTER TABLE table-name ADD COLUMN attr1 attr-type; </li></ul><ul><li>… DROP COLUMN attr1; </li></ul><ul><li>Adds a new column to an existing database table. </li></ul>
  27. 67. INSERT <ul><li>INSERT INTO table-name (attr1, attr4, attr5,…, attrK) VALUES (“val1”, val4, val5,…, “valK”); </li></ul><ul><li>Adds a new row(s) to a table. </li></ul><ul><li>INSERT INTO table-name (attr1, attr4, attr5,…, attrK) VALUES SELECT ... </li></ul>
  28. 68. DELETE <ul><li>DELETE FROM table-name WHERE <where clause>; </li></ul><ul><li>Removes rows from a table. </li></ul>
  29. 69. UPDATE <ul><li>UPDATE tablename SET attr1=newval, attr2 = newval2 WHERE <where clause>; </li></ul><ul><li>changes values in existing rows in a table (those that match the WHERE clause). </li></ul>
  30. 70. DROP Table <ul><li>DROP TABLE tablename; </li></ul><ul><li>Removes a table from the database. </li></ul>
  31. 71. 3000000 Clementi Singapore Ng Wee Hiong 1000000 Jakarta_2 Jakarta George 4000000 Mona Jakarta Agus Arianto 1000000 Singapore_2 Singapore John Asset Director City Name
  32. 73. Lecture Outline <ul><li>Review </li></ul><ul><ul><li>Relational Operations </li></ul></ul><ul><ul><li>Relational Algebra </li></ul></ul><ul><ul><li>Relational Calculus </li></ul></ul><ul><ul><li>Introduction to SQL </li></ul></ul><ul><li>Introduction to SQL (continued) </li></ul><ul><li>Application Development in Access </li></ul>

×