Logo

K-Learn

SQL Create Table

< Previous Next >

A table in SQL is a collection of related data organized in rows and columns. It is the basic unit of data storage in a relational database management system (RDBMS). 

Syntax

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

Here's an example of a simple table in SQL:

Let's say we want to create a table to store information about students. We can create a table named "students" with the following columns:

  • student_id: a unique identifier for each student

  • first_name: the first name of the student

  • last_name: the last name of the student

  • age: the age of the student

  • gender: the gender of the student

 

To create the "student" table in SQL, we would use the following SQL statement:

CREATE TABLE student (
  id INT PRIMARY KEY,
  name VARCHAR(50) NOT NULL,
  email VARCHAR(50) UNIQUE NOT NULL,
  phone VARCHAR(20),
  address VARCHAR(100)
);

OUTPUT

In this above SQL INSERT Example,

Note that the "id" column is marked as the PRIMARY KEY, which means that it must contain a unique value for each record in the table. If you try to insert a record with a duplicate value in the "id" column, the INSERT statement will fail.
 
Also note that the "email" column is marked as UNIQUE, which means that each email address must be unique in the table. If you try to insert a record with a duplicate email address, the INSERT statement will fail


< Previous Next >