MySQL Create Table
Tables are the core structures in a relational database. The CREATE TABLE
statement in MySQL is used to create new tables. In this example, we'll create a table containing names and details of famous Tamil kings.
Key Topics
1. CREATE TABLE Command
To create a table with Tamil kings' details, use the following syntax:
CREATE TABLE tamil_kings (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100),
reign_period VARCHAR(50),
notable_contributions TEXT
);
Code Explanation: This command creates a table named tamil_kings
with three columns: id
(an auto-incrementing primary key), name
(a string up to 100 characters), reign_period
(a string up to 50 characters), and notable_contributions
(text for detailed contributions).
2. Specifying Columns and Data Types
Each column must be defined with a name and a data type. Here, we use INT
, VARCHAR
, and TEXT
data types.
3. Defining Primary Keys
The primary key is used to uniquely identify each record in the table. In this case, id
serves as the primary key.
Best Practices
- Choose appropriate data types to optimize storage and performance.
- Always define a primary key for unique identification.
- Use meaningful column names for clarity.
Key Takeaways
- Use
CREATE TABLE
to define new tables in MySQL. - Specify columns with appropriate data types and constraints.
- Primary keys are essential for uniquely identifying records.