Inserting Data into a Table
Now that we have an empty table, let’s look at two ways to add new data. Here’s the first form:
USE sysadm
INSERT hosts
VALUES (
'shimmer',
'192.168.1.11',
'shim shimmy shimmydoodles',
'David Davis',
'Software',
'Main',
'309',
'Sun',
'Ultra60'
)The first line tells the server we are going to work with objects in
the sysadm database. The second line selects the
hosts table and adds a row, one column at a
time. This version of the INSERT command is used
to add a complete row to the table (i.e., one with all columns filled
in). To create a new row with a partial record we can specify the
columns to fill, like so:
USE sysadm
INSERT hosts (name,ipaddr,owner)
VALUES (
'bendir',
'192.168.1.3',
'Cindy Coltrane'
)The INSERT command will fail if we try to insert a
row does not have all of the required (NOT NULL)
columns.
INSERT can also be used to add data from one table
to another; we’ll see this usage later. For the rest of our
examples, assume that we’ve fully populated the
hosts table using the first form of
INSERT.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access