Schema Management
SQL CREATE TABLE
Creating Tables with CREATE TABLE
CREATE TABLE defines tables with column constraints like PRIMARY KEY.
Introduction to SQL CREATE TABLE
The CREATE TABLE statement in SQL is used to create a new table in a database. It defines the structure of the table by specifying the columns, their data types, and optional constraints such as primary keys.
Basic Syntax of CREATE TABLE
The basic syntax for creating a table in SQL is as follows:
Understanding Data Types
Each column in a SQL table must be assigned a data type. Common data types include INT
for integers, VARCHAR(n)
for variable-length strings, DATE
for date values, and BOOLEAN
for true/false values.
Adding Constraints to Columns
Constraints are used to limit the type of data that can go into a table. They ensure the accuracy and reliability of the data. Here are some common constraints:
- PRIMARY KEY: Uniquely identifies each row in a table.
- FOREIGN KEY: Ensures referential integrity between two tables.
- NOT NULL: Ensures that a column cannot have a NULL value.
- UNIQUE: Ensures all values in a column are different.
- CHECK: Ensures that all values in a column satisfy a specific condition.
Example: Creating a Table with Constraints
Let's create a simple table named Employees
with various constraints:
Conclusion
Using the CREATE TABLE statement, you can define the structure of your database tables with precision. By applying constraints, you ensure data integrity and consistency. Understanding how to effectively use this command is fundamental for database design and management.
Schema Management
- Previous
- INSERT INTO SELECT
- Next
- ALTER TABLE