3. Inserting values into a table

Once an empty table has been created, the next thing is to load some records into it. This is done using the sql INSERT command

Carrying on with the example of the clubs table, the command below will insert one record into the table

INSERT INTO `test`.`clubs` (`ClubID`, `ClubName`) VALUES ('1', 'The Pullman Club');

The figure below shows a nicely formatted view of inserting a row into MySQL

SQL insert command

Breaking it down, the first command is

INSERT INTO test.clubs .....

This identifies the database and the table to be used with the insert command. (This is a 'boots and braces' approach - if there is no other database open then you can just use the table name rather than the full database definition)

Next comes

INSERT INTO `test`.`clubs` (`ClubID`, `ClubName`)....

The items within the brackets are the fields that will be loaded with new data

Next the sql term VALUES appears making way for the actual values

INSERT INTO `test`.`clubs` (`ClubID`, `ClubName`) VALUES ...

along come the values themselves ...

INSERT INTO `test`.`clubs` (`ClubID`, `ClubName`) VALUES ('1', 'The Pullman Club');

In practice you have to be very careful that all the data types are correct and in the right order with nothing missing, most RDBMS are very unforgiving if you do not have it completely correct!


Challenge see if you can find out one extra fact on this topic that we haven't already told you

Click on this link: SQL INSERT command






Copyright © www.teach-ict.com