MySQL Databases, Tables, Rows, and Columns

Learn MySQL Databases, Tables, Rows, and Columns with practical examples, explanations, best practices, and real-world coding techniques.

Schemas, tables, and records

A MySQL database, also called a schema in MySQL terminology, groups related objects such as tables, views, and routines. A table defines a structure for a particular kind of entity or relationship. Its columns describe attributes and their data types; its rows contain individual records. For example, a customers table might contain one row per customer and columns for a stable identifier, email address, and creation time. Thinking in these terms helps developers decide where each fact belongs instead of treating a database as an unstructured collection of values.

Columns should have clear meanings and appropriate types. A price needs an exact decimal representation rather than an approximate floating-point value; a date should use a date or time type rather than a formatted string. Nullability expresses whether a value may be absent, while defaults should represent a deliberate domain rule. Character set and collation affect how text is stored, compared, and sorted. These choices matter to application behavior and migrations, so they should be intentional rather than inherited accidentally from an unknown server default.

Keys identify and relate rows

A primary key uniquely identifies each row and should remain stable enough to be referenced by other tables. A unique constraint protects a different business key, such as an email address, when duplicates are not allowed. A foreign key expresses a relationship to a referenced key and can prevent orphaned records. These rules should be chosen from the meaning of the data. A person's name is usually not a safe primary key because names can change and different people can share one.

Related entities generally belong in separate tables. An order row can reference a customer using a customer identifier rather than copying all customer details into every order. A line-item table can connect orders and products while storing values specific to the purchase, such as quantity and the price at the time of sale. That historical price is not necessarily a redundant copy of the current product price; it records a separate fact about the transaction. This kind of modeling makes relationships explicit and allows a query to join information when it is needed.

CREATE TABLE customers (
  customer_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  email VARCHAR(254) NOT NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (customer_id),
  UNIQUE KEY uq_customers_email (email)
) ENGINE = InnoDB;

CREATE TABLE orders (
  order_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  customer_id BIGINT UNSIGNED NOT NULL,
  total DECIMAL(12, 2) NOT NULL,
  PRIMARY KEY (order_id),
  CONSTRAINT fk_orders_customer
    FOREIGN KEY (customer_id) REFERENCES customers (customer_id)
) ENGINE = InnoDB;

The example gives each table a primary key and requires each order to point to a customer. The unique email constraint prevents duplicate values according to the column's collation rules. The referenced and referencing key definitions need to be compatible for a foreign key. In real schemas, decide what should happen when a referenced customer is deleted; restricting deletion may be safer than cascading when orders must be retained for accounting or support.

Naming, constraints, and practical organization

Use names that make tables and columns understandable to the team, and apply a consistent convention for singular or plural table names. Avoid ambiguous names such as value or data when a more descriptive term is available. Do not store several independent values in one comma-separated text field if the application needs to validate, filter, or relate each value separately. A dedicated related table is generally easier to constrain and query.

Constraints make a schema do more than document intent. NOT NULL, primary keys, unique keys, foreign keys, and supported check constraints can prevent invalid states at the database boundary, even when multiple programs write to the same database. Constraints do not replace application validation; both layers serve different purposes. Application validation can give users immediate feedback, while database constraints protect stored data from race conditions and alternate clients.

Plan for change and query patterns

A schema evolves as product requirements change. Keep changes in reviewed migrations, test them with representative data, and consider locks and compatibility with application versions during deployment. Indexes should support actual lookups, joins, or sorting rather than being added to every column. Each index consumes storage and adds work to writes. Use query plans and workload measurements to decide where indexes help.

The central design question is what fact each row represents and which rules must always hold. Clear entities, stable keys, well-chosen types, and explicit relationships make querying and maintenance more predictable. Continue with MySQL data types, table relationships, and keys and constraints.

Code Examples

Define related customer and order tables

sql
CREATE TABLE customers (
  customer_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  email VARCHAR(254) NOT NULL,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (customer_id),
  UNIQUE KEY uq_customers_email (email)
) ENGINE = InnoDB;

CREATE TABLE orders (
  order_id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  customer_id BIGINT UNSIGNED NOT NULL,
  total DECIMAL(12, 2) NOT NULL,
  PRIMARY KEY (order_id),
  CONSTRAINT fk_orders_customer
    FOREIGN KEY (customer_id) REFERENCES customers (customer_id)
) ENGINE = InnoDB;
    

The primary keys identify rows, the unique key protects email uniqueness, and the foreign key requires each order to reference a customer.

Query related records

sql
SELECT o.order_id, c.email, o.total
FROM orders AS o
JOIN customers AS c
  ON c.customer_id = o.customer_id
ORDER BY o.order_id DESC;
    

A join retrieves customer information without duplicating customer attributes in every order row.

Pro Tip

Define each table around one clear entity or relationship, then enforce important identity and integrity rules with keys and constraints.

🎨 IgKnight Docs

Master HTML, CSS, JS, Python, .NET and more with easy-to-follow lessons and practical code examples.

© 2025 IgKnight Learning Platform. All rights reserved.