INSERT
The INSERT
DML statement allows users to write data into a database. A simplified example of the syntax follows:
INSERT <table_name> [(column list)]
VALUES( { DEFAULT | NULL | <expression> } [ ,...n] )
where the parameters are defined as follows:
<table_name>
is the table that you want to insert data into.<column list>
is a list of specific columns you want to insert data into. This is optional.<expression>
is the actual data that you want to
insert. The type of data depends on the data type that is defined for
the particular column.
Basic INSERT Queries
The following sample script will add more pets to the Pets
table:
INSERT INTO Pets (pet_id, pet_name, pet_weight, MicroChipID)
VALUES (10,'Roxy',7,'1A8AF59'),
(11,'Champ',95,'81CB910'),
(12,'Penny',80,'C710A6B')
Alternatively, you could have omitted the
specific columns you wanted to insert. If you do this, SQL Server will
assume that the first value will be for the first column definition,
the second value entered will be for the second columns definition, and
so on. For example, since you are inserting information into all the
columns in the Owners
table, you could issue this statement and omit owner_id
, pet_id
, and owner_name
from the statement:
INSERT INTO Owners VALUES (4,5,'Frank')
Identity and Sequences
When the Pets
table was defined, a primary key column for pet_id
was created. Usually, a table has a primary key column that uniquely
identifies the information for that specific row of data. Up until now,
when an INSERT
statement was given, we supplied this
primary key. In a real-world application, the next available primary
key might not be known, or it may require some T-SQL execution to
obtain the next value. This is one of the scenarios where the IDENTITY
property of the CREATE TABLE
statement can help with automatically generating a value for the new row. Since we already have the Pets
table, to use the INDENTITY
property, we would have to drop the table and create it. To help illustrate the property create a new table called PetFood
as follows:
CREATE TABLE PetFood
(pet_food_id int IDENTITY(1,1),
food_name varchar (20));
Now, as data is inserted into the PetFood
table, you do not need to specify the pet_food
identity, as shown by the following T-SQL statement:
INSERT INTO PetFood VALUES('Lamb and Rice'),('Chicken'),('Corn')
To view the table issue a SELECT * FROM PetFood
statement. This will yield the following result:
pet_food_id food_name
1 Lamb and Rice
2 Chicken
3 Corn
Notice that you didn’t have to figure out what the next available pet_food_id
was when you inserted the data into the table.
IDENTITY
is a powerful and useful property, but it is also limited in some areas. Perhaps the biggest issue with IDENTITY
comes with its restriction to a single table. It is hard to share a
common identity value across multiple tables in a single transaction.
This is because the identity value is created after
the insert. There are some ways to get around this, but a much better solution would be to use a SEQUENCE
object. A SEQUENCE
object can easily be used by more than one table because it’s not bound
to any particular table; rather, it’s a separate independent object. To
create a SEQUENCE
object, use the CREATE SEQUENCE
statement as follows:
CREATE SEQUENCE PetFoodIDSequence
AS INT
START WITH 1
INCREMENT BY 1
Let’s drop and re-create our PetFood
table so we can use the new PetFoodIDSequence
object:
DROP TABLE PetFood
GO
CREATE TABLE PetFood
(pet_food int DEFAULT (NEXT VALUE FOR PetFoodIDSequence),
food_name VARCHAR (20));
GO
Now, we can insert our pet food as before using the INSERT
statement:
INSERT INTO PetFood (food_name) VALUES('Lamb and Rice'),('Chicken'),('Corn')
Running a SELECT
statement on the PetFood
table will yield the same results as when we used the IDENTITY
property.
UPDATE
The UPDATE
DML statement allows the user to modify existing data within the database. A simplified definition follows:
UPDATE <object>
SET { column_name = { <expression> | DEFAULT | NULL }
[ WHERE <search_condition> ]
where the parameters are defined as follows:
<object>
is the table or view that is being updated.column_name
is the name of the column of data that will be updated.<search_condition>
is used to filter or scope the list of rows to update.
As an example, let’s update the Owners
table and change the ownership of dogs owned by Frank to Courtney. You can do this with the following UPDATE
statement:
UPDATE Owners SET owner_name='Courtney' WHERE owner_name='Frank'
The WHERE
clause is optional.
However, if you do not specify a search condition, you will update
every row. So, unless updating all rows is what you intend to do, you
should always specify a WHERE
clause.
DELETE
The DELETE
DML statement allows users to remove data from the database. A simplified definition follows:
DELETE FROM <table_source> [ WHERE <search_condition> ]
where the parameters are defined as follows:
<table_source>
specifies the table that you want to remove items from.<search_condition>
is used to scope the amount of rows that will be deleted.
The following example uses the DELETE
statement to remove the pet named Penny:
DELETE FROM Pets WHERE pet_name = 'Penny'
The WHERE
clause is optional. If
you omit it, you will delete all rows in the specified table. Be very
careful about omitting it. Omitting the WHERE
clause and
inadvertently deleting all data from a table is a common mistake. It’s
a mistake that you truly want to avoid making.