Beginners Guide to SQL – SQL INSERT Statement

Hits: 9

(SQL Tutorials for Citizen Data Scientist)

SQL INSERT Statement

In this tutorial you will learn how to insert records in a database table using SQL.

Inserting Data in Table

In the preceding chapter we’ve created a table named persons in our demo database. Now it’s time to insert some data inside our newly created database table.

The INSERT INTO statement is used to insert new rows in a database table.

Syntax

The basic syntax for inserting data into a table can be given with:

INSERT INTO table_name (column1,column2,…) VALUES (value1,value2,…);

Here the column1column2,…, etc. represents the name of the table columns, whereas the value1value2,…, and so on represents the corresponding values for these columns.

Let’s insert some records into the persons table.

Step 1: View Table Structure

Before adding record it’s a good idea to obtain the information about the table structure. Execute the following command on MySQL command-line. It will display the information about the columns in the persons table i.e. column name, data type, constraints etc.

mysql> DESCRIBE persons;

You can see the column information or structure of any table in MySQL and Oracle database using the command DESCRIBE table_name;, whereas EXEC sp_columns table_name; in SQL Server (replace the table_name with actual table name).

Step 2: Adding Records to a Table

The following statement inserts a new row in persons table.

Example

INSERT INTO persons (name, birth_date, phone)
VALUES ('Peter Wilson', '1990-07-15', '0711-020361');

Did you notice, we didn’t insert any value for id field? Because, if you remember from the create table chapter, the id field was marked with AUTO_INCREMENT flag, which tells MySQL to automatically assign a value to this field if it is left unspecified.

Note: Non-numeric values like strings and dates must always be surrounded by quotes, whereas numeric values should never be enclosed within quotes. Also, if your string itself contains quotes you should escape it with backslash like 'Let's go'.

Similarly, insert another row into the persons table, as follow:

Example

INSERT INTO persons (name, birth_date, phone)
VALUES ('Carrie Simpson', '1995-05-01', '0251-031259');

Insert one more row into the persons table, in a similar manner:

Example

INSERT INTO persons (name, birth_date, phone)
VALUES ('Victoria Ashworth', '1996-10-17', '0695-346721');

Now if you select the records from persons table, the output will now look like this:

+----+--------------------+------------+-------------+
| id | name               | birth_date | phone       |
+----+--------------------+------------+-------------+
|  1 | Peter Wilson       | 1990-07-15 | 0711-020361 |
|  2 | Carrie Simpson     | 1995-05-01 | 0251-031259 |
|  3 | Victoria Ashworth  | 1996-10-17 | 0695-346721 |
+----+--------------------+------------+-------------+

 

Beginners Guide to SQL – SQL INSERT Statement

 

Personal Career & Learning Guide for Data Analyst, Data Engineer and Data Scientist

Applied Machine Learning & Data Science Projects and Coding Recipes for Beginners

A list of FREE programming examples together with eTutorials & eBooks @ SETScholars

95% Discount on “Projects & Recipes, tutorials, ebooks”

Projects and Coding Recipes, eTutorials and eBooks: The best All-in-One resources for Data Analyst, Data Scientist, Machine Learning Engineer and Software Developer

Topics included: Classification, Clustering, Regression, Forecasting, Algorithms, Data Structures, Data Analytics & Data Science, Deep Learning, Machine Learning, Programming Languages and Software Tools & Packages.
(Discount is valid for limited time only)

Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.

Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners

Please do not waste your valuable time by watching videos, rather use end-to-end (Python and R) recipes from Professional Data Scientists to practice coding, and land the most demandable jobs in the fields of Predictive analytics & AI (Machine Learning and Data Science).

The objective is to guide the developers & analysts to “Learn how to Code” for Applied AI using end-to-end coding solutions, and unlock the world of opportunities!