w3resource

Create a MySQL table named 'countries' with a unique combination of columns 'country_id' and 'region_id'

MySQL Create Tables: Exercise-13 with Solution

13. Write a MySQL query to create a table countries including columns country_id, country_name and region_id and make sure that the combination of columns country_id and region_id will be unique.

Sample Solution:

-- Creating a table named 'countries' if it doesn't already exist to store information about countries

CREATE TABLE IF NOT EXISTS countries(
    -- Column to store the two-letter country code, marked as NOT NULL (required), UNIQUE, with a default value of an empty string
    COUNTRY_ID varchar(2) NOT NULL UNIQUE DEFAULT '',

    -- Column to store the name of the country (up to 40 characters), with a default value of NULL
    COUNTRY_NAME varchar(40) DEFAULT NULL,

    -- Column to store the region ID with decimal precision of 10, 0, marked as NOT NULL (required)
    REGION_ID decimal(10,0) NOT NULL,

    -- Defining a composite PRIMARY KEY on COUNTRY_ID and REGION_ID columns
    PRIMARY KEY (COUNTRY_ID,REGION_ID)
);

Let execute the above code in MySQL command prompt

Here is the structure of the table:

mysql> DESC countries;
+--------------+---------------+------+-----+---------+-------+
| Field        | Type          | Null | Key | Default | Extra |
+--------------+---------------+------+-----+---------+-------+
| COUNTRY_ID   | varchar(2)    | NO   | PRI |         |       |
| COUNTRY_NAME | varchar(40)   | YES  |     | NULL    |       |
| REGION_ID    | decimal(10,0) | YES  |     | NULL    |       |
+--------------+---------------+------+-----+---------+-------+
3 rows in set (0.01 sec)

Explanation:

The said MySQL code creates a table named "countries" if it doesn't already exist. The table has three columns:

  • COUNTRY_ID: Stores two-letter country codes, marked as NOT NULL, UNIQUE, and has a default value of an empty string.
  • COUNTRY_NAME: Stores the names of countries (up to 40 characters) with a default value of NULL.
  • REGION_ID: Stores region IDs with decimal precision of 10, 0, marked as NOT NULL.

Additionally, a composite PRIMARY KEY is defined on the 'COUNTRY_ID' and 'REGION_ID' columns.

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

Previous: Write a SQL statement to create a table countries including columns country_id, country_name and region_id and make sure that the column country_id will be unique and store an auto incremented value.
Next: Write a SQL statement to create a table job_history including columns employee_id, start_date, end_date, job_id and department_id and make sure that, the employee_id column does not contain any duplicate value at the time of insertion and the foreign key column job_id contain only those values which are exists in the jobs table.

What is the difficulty level of this exercise?



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/mysql-exercises/create-table-exercises/create-table-exercise-13.php