(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:
table_name
(column1,column2,…) VALUES (value1,value2,…);Here the column1, column2,…, etc. represents the name of the table columns, whereas the value1, value2,…, 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.
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
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
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.