Insert:: DML (Data Manipulation Language)
Insert:: DML (Data Manipulation Language)
Insert:: DML (Data Manipulation Language)
DML is abbreviation of Data Manipulation Language. It is used to retrieve, store, modify, delete, insert and update data in database. Following are the DML Statements: Editing Commands Insert Update Delete
Insert :
Insert is used to add a new row to a table or view
INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)
Now we want to insert a new row in the "Persons" table. We use the following SQL statement: INSERT INTO Persons VALUES (4,'Nilsen', 'Johan', 'Bakken 2', 'Stavanger') The "Persons" table will now look like this:
P_Id 1 2 3 4 LastName Hansen Svendson Pettersen Nilsen FirstName Ola Tove Kari Johan Address Timoteivn 10 Borgvn 23 Storgt 20 Bakken 2 City Sandnes Sandnes Stavanger Stavanger
Update:
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 UPDATE Syntax :
UPDATE table_name SET column1=value, column2=value2,... WHERE some_column=some_value
UPDATE Example :
The "Persons" table: P_Id 1 2 3 4 5 LastName Hansen Svendson Pettersen Nilsen Tjessem FirstName Ola Tove Kari Johan Jakob Address Timoteivn 10 Borgvn 23 Storgt 20 Bakken 2 City Sandnes Sandnes Stavanger Stavanger
Now we want to update the person "Tjessem, Jakob" in the "Persons" table. We use the following SQL statement: UPDATE Persons SET Address='Nissestien 67', City='Sandnes' WHERE LastName='Tjessem' AND FirstName='Jakob' The "Persons" table will now look like this: P_Id 1 2 3 4 5 LastName Hansen Svendson Pettersen Nilsen Tjessem FirstName Ola Tove Kari Johan Jakob Address Timoteivn 10 Borgvn 23 Storgt 20 Bakken 2 Nissestien 67 City Sandnes Sandnes Stavanger Stavanger Sandnes
UPDATE Warning Be careful when updating records. If we had omitted the WHERE clause in the example above, like this: UPDATE Persons SET Address='Nissestien 67', City='Sandnes' The "Persons" table would have looked like this: P_Id 1 2 3 4 5 LastName Hansen Svendson Pettersen Nilsen Tjessem FirstName Ola Tove Kari Johan Jakob Address Nissestien 67 Nissestien 67 Nissestien 67 Nissestien 67 Nissestien 67 City Sandnes Sandnes Sandnes Sandnes Sandnes
Delete:
Delete removes one or more rows from the table No field list is included May specify which rows to remove by adding WHERE clause
Delete Syntax:
DELETE FROM tablename [WHERE condition] Not including a where clause removes all rows from a table
DELETE Example
DELETE FROM Persons WHERE LastName='Tjessem' AND FirstName='Jakob'
The "Persons" table will now look like this: P_Id 1 2 3 4 LastName Hansen Svendson Pettersen Nilsen FirstName Ola Tove Kari Johan Address Timoteivn 10 Borgvn 23 Storgt 20 Bakken 2 City Sandnes Sandnes Stavanger Stavanger
Select :
The SELECT statement is used to select data from a database. The result is stored in a result table, called the result-set. SELECT Syntax : SELECT column_name(s) FROM table_name And SELECT * FROM table_name
An SELECT Example :
SELECT * Example
Now we want to select all the columns from the "Persons" table. We use the following SELECT statement: SELECT * FROM Persons Tip: The asterisk (*) is a quick way of selecting all columns! The result-set will look like this: P_Id 1 2 3 LastName Hansen Svendson Pettersen FirstName Ola Tove Kari Address Timoteivn 10 Borgvn 23 Storgt 20 City Sandnes Sandnes Stavanger
CREATE
CREATE DATABASE Statement
The CREATE DATABASE statement is used to create a database.
CREATE TABLE Example Now we want to create a table called "Persons" that contains five columns: P_Id, LastName, FirstName, Address, and City. We use the following CREATE TABLE statement: CREATE TABLE Persons ( P_Id int, LastName varchar(255), FirstName varchar(255), Address varchar(255), City varchar(255) )
The "Persons" table will now look like this: P_Id LastName FirstName Address City
ALTER
ALTER TABLE Statement
The ALTER TABLE statement is used to add, delete, or modify columns in an existing table.
ALTER TABLE Syntax : To add a column in a table, use the following syntax: ALTER TABLE table_name ADD column_name datatype To change the data type of a column in a table, use the following syntax: ALTER TABLE table_name ALTER COLUMN column_name datatype
ALTER TABLE Example : Now we want to add a column named "DateOfBirth" in the "Persons" table. We use the following SQL statement: ALTER TABLE Persons ADD DateOfBirth date The "Persons" table will now like this: P_Id 1 2 3 LastName Hansen Svendson Pettersen FirstName Ola Tove Kari Address Timoteivn 10 Borgvn 23 Storgt 20 City Sandnes Sandnes Stavanger DateOfBirth
DROP
The DROP TABLE Statement
The DROP TABLE statement is used to delete a table.
TRUNCATE
The TRUNCATE TABLE Statement
What if we only want to delete the data inside the table, and not the table itself? Then, use the TRUNCATE TABLE statement:
Example of TRUNCATE
The "Persons" table will now look like this: P_Id LastName FirstName Address City