Sql1

569 views
488 views

Published on

First presentation for Intro to SQL

Published in: Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
569
On SlideShare
0
From Embeds
0
Number of Embeds
11
Actions
Shares
0
Downloads
11
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Sql1

  1. 1. Shane Smith
  2. 3. <ul><li>Attributes (fields) </li></ul><ul><ul><li>An attribute or field is a component of a record that describes something about an item. </li></ul></ul><ul><li>Records </li></ul><ul><ul><li>A record is the representation of an individual item. </li></ul></ul><ul><li>Table </li></ul><ul><ul><li>A collection of records </li></ul></ul><ul><li>Database </li></ul><ul><ul><li>A collection of tables and rules for accessing the tables </li></ul></ul>
  3. 4. <ul><li>Originally developed by E.F. Codd in 1970 </li></ul><ul><li>Organizes data into tables where each item is a row and the attributes of the item are in columns. </li></ul><ul><li>Different from “flat file” databases because you can define “relationships” between items in different tables. </li></ul>
  4. 5. <ul><li>RDBMS stands for Relational DataBase Management System </li></ul><ul><li>RDBMS is the basis for SQL, and for all modern database systems like MS SQL Server, IBM DB2, Oracle, MySQL, and Microsoft Access. </li></ul><ul><li>The data in RDBMS is stored in database objects called tables. </li></ul><ul><li>A table is a collections of related data entries and it consists of columns and rows. </li></ul>
  5. 6. Record Attribute/Field Tables <ul><li>Records become “rows” </li></ul><ul><li>Attributes/fields become “columns” </li></ul><ul><li>Rules determine the relationship between the tables and tie the data together to form a database </li></ul>
  6. 7. <ul><li>What information are we trying to store? </li></ul><ul><li>How do we describe the information? </li></ul><ul><li>Phone Book/Contact entries </li></ul><ul><ul><li>Name </li></ul></ul><ul><ul><li>Address </li></ul></ul><ul><ul><li>Company </li></ul></ul><ul><ul><li>Phone Number </li></ul></ul><ul><ul><li>URL/Web Page </li></ul></ul><ul><ul><li>Age </li></ul></ul><ul><ul><li>Height (in meters) </li></ul></ul><ul><ul><li>Birthday </li></ul></ul><ul><ul><li>When we added the entry </li></ul></ul>
  7. 8. <ul><li>Binary </li></ul><ul><ul><li>Database specific binary objects </li></ul></ul><ul><ul><li>Pictures, digital signatures, etc. </li></ul></ul><ul><li>Boolean </li></ul><ul><ul><li>True/False values </li></ul></ul><ul><li>Character </li></ul><ul><ul><li>Fixed width or variable size </li></ul></ul><ul><li>Numeric </li></ul><ul><ul><li>Integer, Real (floating decimal point), Money </li></ul></ul><ul><li>Temporal </li></ul><ul><ul><li>Time, Date, Timestamp </li></ul></ul>
  8. 9. <ul><ul><li>Name Character </li></ul></ul><ul><ul><li>Address Character </li></ul></ul><ul><ul><li>Company Character </li></ul></ul><ul><ul><li>Phone Number Character </li></ul></ul><ul><ul><li>URL/Web Page Character </li></ul></ul><ul><ul><li>Age Integer </li></ul></ul><ul><ul><li>Height Real (float) </li></ul></ul><ul><ul><li>Birthday Date </li></ul></ul><ul><ul><li>When we added the entry Timestamp </li></ul></ul>
  9. 10. <ul><li>Creating tables with CREATE </li></ul><ul><li>Adding data with INSERT </li></ul><ul><li>Viewing data with SELECT </li></ul><ul><li>Removing data with DELETE </li></ul><ul><li>Modifying data with UPDATE </li></ul><ul><li>Destroying tables with DROP </li></ul>
  10. 11. <ul><li>Generic form </li></ul><ul><ul><li>CREATE TABLE tablename ( </li></ul></ul><ul><ul><li>column_name data_type attributes…, </li></ul></ul><ul><ul><li>column_name data_type attributes…, </li></ul></ul><ul><ul><li>… </li></ul></ul><ul><ul><li>) </li></ul></ul><ul><li>Table and column names can’t have spaces or be “reserved words” like TABLE, CREATE, etc. </li></ul>
  11. 12. <ul><ul><li>Name Character </li></ul></ul><ul><ul><li>Address Character </li></ul></ul><ul><ul><li>Company Character </li></ul></ul><ul><ul><li>Phone Number Character </li></ul></ul><ul><ul><li>URL/Web Page Character </li></ul></ul><ul><ul><li>Age Integer </li></ul></ul><ul><ul><li>Height Real (float) </li></ul></ul><ul><ul><li>Birthday Date </li></ul></ul><ul><ul><li>When we added the entry Timestamp </li></ul></ul>
  12. 13. <ul><ul><li>CREATE TABLE contacts ( </li></ul></ul><ul><ul><li>Name VARCHAR(40), </li></ul></ul><ul><ul><li>Address VARCHAR(60), </li></ul></ul><ul><ul><li>Company VARCHAR(60), </li></ul></ul><ul><ul><li>Phone VARCHAR(11), </li></ul></ul><ul><ul><li>URL VARCHAR(80), </li></ul></ul><ul><ul><li>Age INT, </li></ul></ul><ul><ul><li>Height FLOAT, </li></ul></ul><ul><ul><li>Birthday DATE, </li></ul></ul><ul><ul><li>WhenEntered TIMESTAMP </li></ul></ul><ul><ul><li>); </li></ul></ul><ul><ul><li>Plan your tables very carefully! </li></ul></ul><ul><ul><li>Once created, they are difficult to change! </li></ul></ul>
  13. 14. <ul><ul><li>CREATE TABLE contacts ( </li></ul></ul><ul><ul><li>ContactID INT PRIMARY KEY, </li></ul></ul><ul><ul><li>Name VARCHAR(40), </li></ul></ul><ul><ul><li>Address VARCHAR(60), </li></ul></ul><ul><ul><li>Company VARCHAR(60), </li></ul></ul><ul><ul><li>Phone VARCHAR(11), </li></ul></ul><ul><ul><li>URL VARCHAR(80), </li></ul></ul><ul><ul><li>Age INT, </li></ul></ul><ul><ul><li>Height FLOAT, </li></ul></ul><ul><ul><li>Birthday DATE, </li></ul></ul><ul><ul><li>WhenEntered TIMESTAMP </li></ul></ul><ul><ul><li>); </li></ul></ul><ul><ul><li>If you are going to use the relational nature of a database, don’t forget you need to have a unique way to access records! </li></ul></ul><ul><ul><li>There is a way to make the key automatically increment, so you don’t have to worry about which one is next. </li></ul></ul>
  14. 15. <ul><li>Binary </li></ul><ul><ul><li>Database specific binary objects </li></ul></ul><ul><li>Boolean </li></ul><ul><ul><li>True/False values (BOOLEAN) </li></ul></ul><ul><li>Character </li></ul><ul><ul><li>Fixed width (CHAR) or variable size (VARCHAR) </li></ul></ul><ul><li>Numeric </li></ul><ul><ul><li>Integer (INT), Real (FLOAT), Money (MONEY) </li></ul></ul><ul><li>Temporal </li></ul><ul><ul><li>Time (TIME), Date (DATE), Timestamp (TIMESTAMP) </li></ul></ul>
  15. 16. <ul><li>Generic Form </li></ul><ul><ul><li>INSERT INTO tablename ( column_name,… ) </li></ul></ul><ul><ul><li>VALUES ( value,… ) </li></ul></ul>
  16. 17. <ul><li>INSERT INTO contacts (contactid,name,address,company,phone,url,age,height,birthday,whenentered) </li></ul><ul><li>VALUES </li></ul><ul><li>(1,‘Joe’,’123 Any St.’,’ABC’, </li></ul><ul><li>’ 800-555-1212’,‘http://abc.com’,30,1.9, ’6/14/1972’, now()); </li></ul>
  17. 18. <ul><li>INSERT INTO contacts (contactid,name,phone) </li></ul><ul><li>VALUES (2,’Jane’,’212-555-1212’); </li></ul>
  18. 19. <ul><li>CREATE SEQUENCE contactidseq; </li></ul><ul><li>Change the ContactID line in the CREATE TABLE to: </li></ul><ul><ul><li>ContactID INT DEFAULT nextval(‘contactidseq’) PRIMARY KEY </li></ul></ul><ul><li>Or when inserting into a table </li></ul><ul><ul><li>INSERT contacts (contactid,name,phone) </li></ul></ul><ul><ul><li>VALUES (nextval(‘contactidseq’),’Jack’, ‘716-555-1212’); </li></ul></ul>
  19. 20. <ul><li>Generic Form </li></ul><ul><li>SELECT column,… FROM table,… WHERE condition GROUP BY group_by_expression HAVING condition ORDER BY order_expression </li></ul><ul><li>The most used command </li></ul><ul><li>Probably the most complicated also </li></ul><ul><li>If used improperly, can cause very long waits because complex computations </li></ul>
  20. 21. <ul><li>SELECT * FROM contacts; </li></ul><ul><ul><li>Display all records in the ‘contacts’ table </li></ul></ul><ul><li>SELECT contactid,name FROM contacts; </li></ul><ul><ul><li>Display only the record number and names </li></ul></ul><ul><li>SELECT DISTINCT url FROM contacts; </li></ul><ul><ul><li>Display only one entry for every value of URL. </li></ul></ul>
  21. 22. <ul><li>The WHERE “subclause” allows you to select records based on a condition. </li></ul><ul><li>SELECT * FROM contacts WHERE age<10; </li></ul><ul><ul><li>Display records from contacts where age<10 </li></ul></ul><ul><li>SELECT * FROM contacts WHERE age BETWEEN 18 AND 35; </li></ul><ul><ul><li>Display records where age is 18-35 </li></ul></ul>
  22. 23. <ul><li>The “LIKE” condition </li></ul><ul><ul><li>Allows you to look at strings that are alike </li></ul></ul><ul><li>SELECT * FROM contacts WHERE name LIKE ‘J%’; </li></ul><ul><ul><li>Display records where the name starts with ‘J’ </li></ul></ul><ul><li>SELECT * FROM contacts WHERE url LIKE ‘%.com’; </li></ul><ul><ul><li>Display records where url ends in “.com” </li></ul></ul>
  23. 24. <ul><li>Generic Form </li></ul><ul><li>DELETE FROM table WHERE condition ; </li></ul><ul><li>DELETE FROM contacts WHERE age<13; </li></ul>
  24. 25. <ul><li>Generic Form </li></ul><ul><li>UPDATE table SET column=expression </li></ul><ul><li>WHERE condition; </li></ul><ul><li>UPDATE contacts SET company=‘AOL’ </li></ul><ul><li>WHERE company=‘Time Warner’; </li></ul>
  25. 26. <ul><li>Generic Form </li></ul><ul><li>DROP TABLE tablename ; </li></ul><ul><li>DROP TABLE contacts; </li></ul>

×