SQL create table
Created By: chatGPT
To create a table in SQL, you use the CREATE TABLE statement. This statement allows you to specify the table name and define its columns, including their data types. Here’s a basic example of how to create a table for storing user information.
UserID is defined as an INT and will serve as the primary key, ensuring that each user has a unique identifier. The UserName and Email are defined as VARCHAR, which means they can hold variable-length strings. The NOT NULL constraint ensures that these fields cannot be left blank. DateOfBirth is of type DATE, allowing you to store a date value. Lastly, CreatedAt is set to automatically capture the timestamp when a record is inserted.CREATE TABLE Users (
UserID INT PRIMARY KEY,
UserName VARCHAR(50) NOT NULL,
Email VARCHAR(100) NOT NULL,
DateOfBirth DATE,
CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
If you want to include additional constraints, such as UNIQUE for the email field to prevent duplicate entries, you can modify the table creation syntax accordingly:
After executing the CREATE TABLE statement, the specified table will be created in your database and will be ready to store data as per your defined structure. You can verify that the table was created successfully by checking the list of tables in your database.CREATE TABLE Users (
UserID INT PRIMARY KEY,
UserName VARCHAR(50) NOT NULL,
Email VARCHAR(100) UNIQUE NOT NULL,
DateOfBirth DATE,
CreatedAt TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);