DML Commands

22,388 views

Published on

Adding, changing and removing entries in SQL

Published in: Education, Technology, Business
1 Comment
4 Likes
Statistics
Notes
No Downloads
Views
Total views
22,388
On SlideShare
0
From Embeds
0
Number of Embeds
580
Actions
Shares
0
Downloads
392
Comments
1
Likes
4
Embeds 0
No embeds

No notes for slide
  • Editing Data
  • DML Commands

    1. 1. DML CommandsDML Commands Editing DataEditing Data
    2. 2. DML CommandsDML Commands  Editing Commands  Insert  Update  Delete  Review Command  Select
    3. 3. InsertInsert  Insert is used to add a new row to a table or view  View is similar to a query in Access, a predefined select statement  Can add one or more rows  Syntax varies depending on number of rows
    4. 4. Insert Syntax For One RowInsert Syntax For One Row INSERT INTO tablename[(field list)] VALUES(value list)  Field list is optional  If field list is omitted, values expected for all columns (except IDENTITY)
    5. 5. Insert Single Row ExamplesInsert Single Row Examples INSERT INTO Students(StudentID) VALUES(‘875001234’) INSERT INTO Courses(Department, CourseNumber) VALUES(‘CIS’,’182’) INSERT INTO Courses VALUES(‘CIS’,’282’,’SQL Projects’,5,’Using SQL for application back-end’)
    6. 6. Using Default ValuesUsing Default Values • A default value can be entered by – Including field in field list and using DEFAULT key word in Values list – Omit field from field list and value from Values list • Assume Department defaults to CIS: INSERT INTO Courses(Department, CourseNumber) VALUES(DEFAULT,’145’) INSERT INTO Courses(CourseNumber) VALUES(‘145’)
    7. 7. Assigning Null ValuesAssigning Null Values  If Null is acceptable for a column  Omit field from field list; Or  Use NULL in Value list  Assume Course description is optional: INSERT INTO Courses VALUES(‘CIS’,’282’,’SQL Projects’,5,Null) INSERT INTO Courses(Department, CourseNumber, Title, Credits) VALUES(‘CIS’,’282’,’SQL Projects’,5)
    8. 8. UpdateUpdate  Update is used to change an existing row  Can change one, some or all rows in a table or view  A WHERE clause is used to specify specific rows to change  WHERE represents a true/false description of a row  Multiple conditions require a logical operator
    9. 9. Update SyntaxUpdate Syntax UPDATE tablename SET field1 = value1, field2 = value2 [WHERE condition] • Each field to change is listed with the value to store in that field – Comma separates each field/value pair • WHERE condition is same as criteria in Access
    10. 10. Update ExamplesUpdate Examples  Assign value to zip code for 2nd publisher: UPDATE publishers SET pub_zip = 20006 WHERE pub_id = 0877  Change publisher name to add ‘Ltd.’ for any Massachusetts publisher: UPDATE publishers SET pub_name = pub_name + ‘Ltd.’ WHERE pub_state = ‘ma’
    11. 11. DeleteDelete  Delete removes one or more rows from the table  No field list is included  May specify which rows to remove by adding WHERE clause
    12. 12. Delete SyntaxDelete Syntax DELETE FROM tablename [WHERE condition]  Not including a where clause removes all rows from a table
    13. 13. Delete ExamplesDelete Examples DELETE FROM Publishers WHERE pub_state <>’ca’ DELETE FROM Publishers WHERE pub_zip IS NULL OR pub_state = ‘wa’ DELETE FROM Publishers

    ×