Insert some data into a table

Question

The club is adding a new facility - a spa. We need to add it into the facilities table. Use the following values:

  • facid: 9, Name: 'Spa', membercost: 20, guestcost: 30, initialoutlay: 100000, monthlymaintenance: 800.

Expected Results

facidnamemembercostguestcostinitialoutlaymonthlymaintenance
0Tennis Court 152510000200
1Tennis Court 25258000200
2Badminton Court015.5400050
3Table Tennis0532010
4Massage Room 1358040003000
5Massage Room 2358040003000
6Squash Court3.517.5500080
7Snooker Table0545015
8Pool Table0540015
9Spa2030100000800

Your Answer

Your Results

Loading database...

Answers and Discussion

insert into cd.facilities
    (facid, name, membercost, guestcost, initialoutlay, monthlymaintenance)
    values (9, 'Spa', 20, 30, 100000, 800);

INSERT INTO ... VALUES is the simplest way to insert data into a table. There's not a whole lot to discuss here: VALUES is used to construct a row of data, which the INSERT statement inserts into the table. It's a simple as that.

You can see that there's two sections in parentheses. The first is part of the INSERT statement, and specifies the columns that we're providing data for. The second is part of VALUES, and specifies the actual data we want to insert into each column.

If we're inserting data into every column of the table, as in this example, explicitly specifying the column names is optional. As long as you fill in data for all columns of the table, in the order they were defined when you created the table, you can do something like the following:

insert into cd.facilities values (9, 'Spa', 20, 30, 100000, 800);

Generally speaking, for SQL that's going to be reused I tend to prefer being explicit and specifying the column names.