Digital recordkeeping and preservation I Databases : DML ARK2100 Digital recordkeeping and preservation I 2017 Thomas Sødring thomas.sodring@hioa.no P48-R407 67238287
Data Manipulation Language (DML) DML is a database language designed for manipulating data in relational databases DML is used to insert, update or delete data in a table INSERT UPDATE DELETE
DML INSERT Data is inserted into a relation one row at a time The minimum you need is the primary key Unless there are restrictions on other attributes e.g. NOT NULL Even if you are inserting many rows (batch import), data is inserted one tuple at a time Data that is across two rows in different tables can be inserted together There is no requirement for transaction Remember referential integrity
INSERT INTO TABLE (attributtelist) VALUES (valuelist);
INSERT INTO TABLE VALUES ();
INSERT Car # Example 1 INSERT INTO Car (registrationNr, chassisNr, colour, manufacturer, model) VALUES ("ZQ10000", "111592315", "black", "Audi", "A3"); # Example 2 INSERT INTO Car VALUES
INSERT Car
INSERT Car ??
INSERT Car
After INSERT Car
INSERT Car (multiple rows) INSERT INTO Car (registrationNr, chassisNr, colour, manufacturer, model) VALUES ("ZQ10001", "516592316", "red", "Audi", "A5"), ("ZQ10002", "136695919", "blue", "VW", "Golf");
INSERT Car (multiple rows)
After INSERT Car (multiple rows)
Take a look at Car
SQL UPDATE A tuple can be updated/changed with the UPDATE command You can change the values of fields Many tuples can be updated simultaneously with one command Updated one at a time Remember, there is usually no [CTRL] + [Z] (undo) button You can do a lot of harm to a database if you are not careful
UPDATE SET WHERE;
UPDATE TABLE SET Attribute='value' WHERE;
UPDATE Car SET registrationNr = "ZQ10099" WHERE
UPDATE Car
After UPDATE Car
UPDATE Car SET colour = "pink" WHERE registrationNr = "ZQ10099" ;
UPDATE Bil
After UPDATE Car
Take a look at Car
UPDATE Car UPDATE Car SET colour = "red"; What will happen here?
Update Car
Take a look at Car
DELETE FROM WHERE;
DELETE FROM TABLE WHERE;
DELETE Car DELETE FROM Car WHERE registrationNr = "ZQ10099" ;
DELETE Car
After DELETE Car
Take a look at Car
DELETE Car DELETE FROM Car; What will happen here?
Fill up the Car relation The following link will allow you to insert a lot of data into the Car relation https://bibin.hioa.no/~tsodring/php/make_cars.html The username is your studentId Use the password you created for mysql/phpmyadmin The database must exist and contain a table called Car
Car with many records
TRUNCATE Car; Truncate We can easily empty the table using the truncate command You need to run the script again to fill up the table TRUNCATE Car;
Summary Manipulation of data is done using insert, update or delete It's very easy to create problems with your data Always use the WHERE clause when you create an UPDATE or DELETE command Care must be taken especially with these as there is usually no undo button The application on top of the database might break if you aren't careful