TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

MySQL

Insert Data into a MySQL Database Table

Overview

We can use the INSERT statement to insert data into MySQL database table. Steps to create a table can be found here:

https://www.testingdocs.com/create-a-table-in-mysql-database/

INSERT Statement Syntax

https://www.testingdocs.com/mysql-insert-statement/

Populate Data

To insert a single row into the table, we can run the INSERT statement.

Example

In this example, we will insert a row into the supplier table. String values are enclosed with ‘ ‘ characters. We can execute the statement at the MySQL prompt, SQL Editor in MySQL workbench, etc.

mysql> INSERT INTO supplier VALUES(10,’FooSupplier’);

Insert Data MySQL Database Table

Statements

mysql> DESC supplier;
+—————+————-+——+—–+———+——-+
| Field | Type | Null | Key | Default | Extra |
+—————+————-+——+—–+———+——-+
| supplier_id | int | NO | PRI | NULL | |
| supplier_name | varchar(40) | NO | | NULL | |
+—————+————-+——+—–+———+——-+
2 rows in set (0.20 sec)

mysql> INSERT INTO supplier VALUES(10,’FooSupplier’);
Query OK, 1 row affected (0.41 sec)

mysql> SELECT * FROM supplier;
+————-+—————+
| supplier_id | supplier_name |
+————-+—————+
| 10                | FooSupplier |
+————-+—————+
1 row in set (0.00 sec)

mysql>

supplier_id is the PRIMARY KEY for the table. This column value should be unique and should not contain duplicate values. When running multiple INSERT statements we have to make sure that this CONSTRAINT is met. So, another INSERT statement with the same supplier_id 10 would throw an error. For example, the below statement would run without violating the constraint.

INSERT INTO supplier VALUES(20,’AnotherUniqueSupplier’);

We can also run multiple INSERT statements to populate the table with multiple rows. For Example, MYSQL script files can contain multiple INSERT statements in a *.sql script file.

That’s it. We have successfully populated the table with sample data.

—

MySQL Tutorials

MySQL Tutorials on this website:

https://www.testingdocs.com/mysql-tutorials-for-beginners/

For more information on MySQL Database:

https://www.mysql.com/

Related Posts

DataGrip Desktop Shortcut

MySQL /

Launch DataGrip on Windows

DataGrip Download

MySQL /

Install DataGrip IDE on Windows 11

MySQL Workbench Windows 11

MySQL /

New MySQL Connection in Workbench

MySQL Command-line Client

MySQL /

Command-Line MySQL Client

Start MySQL Client

MySQL /

Start MySQL Client on Windows 11

‹ MySQL Monitor Modes› Retrieve Information from a MySQL Table

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version