Creating and Modifying Tables
Create, alter and drop tables
Tables are the building blocks of a database, storing data in a structured format. In this article, we’ll explore how to create and alter tables.
What Is a Table?
A table is a collection of rows and columns. Each column represents a specific data field (e.g., name, age), while each row represents a single record. Designing tables effectively is critical for managing your data efficiently.
Basic Syntax for Creating Tables
The CREATE TABLE
statement defines the structure of a table, specifying column names, data types, and optional constraints.
Adding Data to a Table
Once your table is created, use the INSERT INTO
statement to populate it with data like you did in the previous article.
Viewing Table Structure
To understand a table’s structure, you can use database-specific commands.
Altering Table
The ALTER TABLE
statement lets you modify an existing table by adding, altering, or dropping columns.
Adding a Column
Modifying a Column
Dropping a Column
Deleting a Table
If a table is no longer needed, you can delete it using the DROP TABLE
statement. Be careful, as this
DROP TABLE
permanently removes the table and its data.
Key Takeaways
- The
CREATE TABLE
statement is used to define a table’s structure, including columns and their data types. - Use
ALTER TABLE
to make changes to an existing table—add, modify, or drop columns. - Always double-check before using
DROP TABLE
, as it removes the table and all the data.
Next, we’ll explore how to establish relationships between tables using foreign keys. This will help you connect data effectively across your database.