DEV Community

Kailas Warade
Kailas Warade

Posted on

SQL CREATE TABLE Challenge: Design a Real-World E-Commerce Orders Table

SQL CREATE TABLE: Can You Design a Real-World E-Commerce Orders Table?


When working with an SQL database, creating a table is one of the first tasks. But choosing the correct data types and constraints is just as important as defining the columns.

Let's take a simple example from an e-commerce application.

Whenever a customer places an order, the application needs to store information such as the order ID, customer ID, order date, payment method, total amount and delivery address.

Suppose you are responsible for designing the "orders" table.

How would you create it?

  1. Understand the table requirements

Our e-commerce application needs an "orders" table with the following columns:

Column| Purpose
order_id| Unique order identification
customer_id| Customer who placed the order
order_date| Date of the order
total_amount| Total order amount
payment_method| Payment method used
order_status| Current order status
shipping_address| Delivery address

Before writing the CREATE TABLE statement, we need to decide which data types and constraints are appropriate.

For example:

  • "order_id" should uniquely identify each order.
  • "customer_id" should not accept NULL values.
  • "order_date" should store a valid date.
  • "total_amount" should support decimal values.
  • "order_status" should have a default value of 'Pending'.
  • "total_amount" should not accept negative values.
  1. SQL CREATE TABLE Challenge

Which of the following SQL statements correctly creates the table?

Option A

CREATE TABLE orders (
order_id INT,
customer_id INT,
order_date VARCHAR(50),
total_amount VARCHAR(100),
payment_method INT
);

Option B

CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT NOT NULL,
order_date DATE NOT NULL,
total_amount DECIMAL(10,2)
CHECK (total_amount >= 0),
payment_method VARCHAR(30),
order_status VARCHAR(20)
DEFAULT 'Pending',
shipping_address VARCHAR(255)
);

Option C

CREATE TABLE orders (
order_id DATE PRIMARY KEY,
customer_id VARCHAR(100),
order_date INT,
total_amount DATE,
payment_method DECIMAL(10,2)
);

Option D

CREATE TABLE orders (
order_id INT UNIQUE,
customer_id INT,
order_date DATE,
total_amount INT,
order_status DATE
);

  1. Understand the SQL Concepts

This challenge covers some important SQL DDL concepts.

CREATE TABLE: Creates a new table in the database.

PRIMARY KEY: Uniquely identifies each record in a table and does not allow NULL values.

NOT NULL: Ensures that a column must contain a value.

DECIMAL(10,2): Stores numeric values with two digits after the decimal point.

CHECK: Enforces a condition on values inserted or updated in a column.

DEFAULT: Assigns a predefined value when no value is supplied for that column.

These constraints help maintain data consistency and prevent invalid records from entering the database.

  1. Your Turn

Which option would you choose: A, B, C or D?

Don't just select an option. Look carefully at the data types and constraints.

Share your answer in the comments and explain why you selected it.

I'll be interested to see how many developers can identify the correct table definition.

  1. Learn More About SQL DDL

If you want to practise more SQL table creation and management concepts, including CREATE TABLE, ALTER TABLE, DROP TABLE and table relationships, explore this tutorial:

"SQL DDL – Manage Tables and Relationships" (https://www.sankalandtech.com/Tutorials/sql-plsql-faq-interview/ddl-manage-tables-relationships-sql-faq.html)

You can find more practical SQL examples, interview questions and database tutorials on "Sankalan Data Tech" (https://www.sankalandtech.com/).

Keep practising SQL with real-world database scenarios. Understanding why we use a particular constraint is just as important as remembering its syntax.

sql #database #programming #beginners

Top comments (0)