w3resource

Insert one row into the MySQL 'countries' table for 'country_id' and 'country_name'

MySQL insert into Statement: Exercise-2 with Solution

2. Write a MySQL query to insert one row into the table countries against the column country_id and country_name.

Here is the structure of the table "countries".

+--------------+---------------+------+-----+---------+-------+
| Field        | Type          | Null | Key | Default | Extra |
+--------------+---------------+------+-----+---------+-------+
| COUNTRY_ID   | varchar(2)    | YES  |     | NULL    |       |
| COUNTRY_NAME | varchar(40)   | YES  |     | NULL    |       |
| REGION_ID    | decimal(10,0) | YES  |     | NULL    |       |
+--------------+---------------+------+-----+---------+-------+	

Sample Solution:

-- Inserting a new record into the 'countries' table with specified columns
INSERT INTO countries (country_id, country_name) VALUES('C1', 'India');

Let execute the above code in MySQL command prompt.

Here is the structure of the table:

mysql> SELECT * FROM countries;
+------------+--------------+-----------+
| COUNTRY_ID | COUNTRY_NAME | REGION_ID |
+------------+--------------+-----------+
| C1         | India        |      NULL |
+------------+--------------+-----------+
1 row in set (0.00 sec)

Explanation:

The above MySQL code inserts a new record into the 'countries' table, specifying values 'C1' for 'country_id' and 'India' for 'country_name', while allowing other columns to take default or NULL values if applicable.

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous:Write a MySQL query to insert a record with your own value into the table countries against each columns.
Next:Write a MySQL query to create duplicate of countries table named country_new with all structure and data.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.