Lab # 3 Hands-On DML Basic SQL Statements Institute of Computer Science, University of Tartu, Estonia DML: Data manipulation language statements access and manipulate data in existing schema objects. These statements do not implicitly commit the current transaction. The data manipulation language statements are: SELECT, INSERT, UPDATE, DELETE, CALL, EXPLAIN, PLAN, LOCK TABLE, MERGE. Part A: Demo by Instructor in Lab a. SELECT select records from a table b. INSERT insert new records c. UPDATE update/modify existing records d. DELETE delete existing records Part B: Hands-on by Students in Lab Page 1 of 7
Part A: Demo by Instructor in Lab a. SELECT Statements: The SELECT statement is a limited form of DML statement in that it can only access data in the database. It cannot manipulate data in the database, although it can operate on the accessed data before returning the results of the query. Following are its general syntaxes: SELECT * FROM table_name SELECT column_name(s) FROM table_name SELECT * FROM countrylanguage; SELECT CountryCode, Language FROM countrylanguage; WHERE clause: To select specific rows from a table we include WHERE clause in the select command. It can appear only after the FROM clause. Syntax: SELECT column_name1,..,column_namen FROM tablename WHERE condition; Example: SELECT * FROM countrylanguage WHERE CountryCode='EST'; SELECT Language, Percentage FROM countrylanguage WHERE percentage > 99; b. INSERT Statements: INSERT statement is used to insert data into a table. Following is its general syntax, INSERT INTO table-name VALUES (data1,data2,..); INSERT INTO CustomersTable (CustomerName, Country) SELECT SupplierName, Country FROM Suppliers WHERE Country='Germany'; INSERT INTO countrylanguage VALUES ('EST', 'Swedish', 'F', '0.2'); Page 2 of 7
c. UPDATE Statements: Update command is used to update a row of a table. Following is its general syntax: UPDATE table-name SET column-name = value WHERE condition; UPDATE countrylanguage SET Language='English' WHERE Language = 'Swedish' and CountryCode='EST'; UPDATE countrylanguage SET Language='French' WHERE Percentage = 0.7 and CountryCode = 'EST'; d. DELET Statements: Delete command is used to delete data from a table. Delete command can also be used with condition to delete a particular row. Following is its general syntax: DELETE from table-name; DELETE from table-name WHERE condition; DELETE from countrylanguage; DELETE from countrylanguage WHERE Language='English' and Percentage= 0.2; Page 3 of 7
Part B: Hands-on by Students in Lab In the last lab # 2, the EmployeeTable in UT_Database database had been created with following structure or use the script from additional material (at last page) to create the EmployeeTable : Columns of EmployeeTable : Page 4 of 7
a. Enter the following 10 records in the EmployeeTable : b. Display those records from EmployeeTable, those work in DeptNo 10. Like records are display below: c. Display those records from EmployeeTable, those work since 2008. Like records are display below: Page 5 of 7
e. Change the Marko, Min and Magi records as PostalAddress Tallinn in the EmployeeTable : a. Remove those records, those work in DeptNo 40. Only Paul works in DeptNo 40. Paul record is removed: Page 6 of 7
Additional Material: Scripts to create EmployeeTable create table EmployeeTable ( EmpNo INT NOT NULL auto_increment, EmpName VARCHAR(40) NOT NULL, Job CHAR(10) NOT NULL, DeptNo INT NOT NULL, Phone_No INT, YearOfEnrollment YEAR, Percentage float(2, 2), EmailAddress VARCHAR(20) NOT NULL, PostalAddress VARCHAR(100), PRIMARY KEY (EmpNo) ); Page 7 of 7