Wednesday, December 18, 2024

SQL CREATE TABLE Keyword

 The CREATE TABLE statement in SQL is used to create a new table in a database. It defines the structure of the table, including its columns, data types, and constraints.

Here’s the basic syntax for CREATE TABLE:

CREATE TABLE table_name (
    column1 datatype constraints,
    column2 datatype constraints,
    column3 datatype constraints,
    ...
);

Example:

CREATE TABLE Employees (
    EmployeeID INT PRIMARY KEY,
    FirstName VARCHAR(50),
    LastName VARCHAR(50),
    BirthDate DATE,
    Salary DECIMAL(10, 2)
);

Explanation:

  • CREATE TABLE Employees: Creates a table named Employees.
  • EmployeeID INT PRIMARY KEY: Defines the EmployeeID column as an integer and a primary key, ensuring each value is unique.
  • FirstName VARCHAR(50): Defines a FirstName column with a maximum length of 50 characters.
  • BirthDate DATE: Defines a column for the date of birth.
  • Salary DECIMAL(10, 2): Defines a column for salary, allowing up to 10 digits, with 2 decimal places.

You can also add constraints like NOT NULL, UNIQUE, DEFAULT, etc., depending on the requirements.

No comments:

Post a Comment