Create Table
The CREATE TABLE statement is used to create a new table in a database. In that table, if you want to add multiple columns, use the below syntax.
Syntax
- CREATE TABLE table_name (
- column1 datatype,
- column2 datatype,
- column3 datatype,
- ....
- );
The column parameters specify the names of the columns of the table.
The data type parameter specifies the type of data the column can hold (e.g. varchar, integer, date, etc.).
Create Table Example
- CREATE TABLE Employee(
- EmpId int,
- LastName varchar(255),
- FirstName varchar(255),
- Address varchar(255),
- City varchar(255)
- );
The EmpId column is of type int and will hold an integer.
The LastName, FirstName, Address, and City columns are of type varchar and will hold characters and the maximum length for these fields is 255 characters.
Insert Value in this Table
The INSERT INTO statement is used to insert new records in a table.
It is possible to write the INSERT INTO statement in two ways.
Syntax
The first way specifies both the column names and the values to be inserted.
If you are adding values for all the columns of the table, then no need to specify the column names in the SQL query. However, make sure that the order of the values is in the same order as the columns in the table.
- INSERT INTO table_name (column1, column2, column3, ...)
- VALUES (value1, value2, value3, ...);
- '2nd way
- INSERT INTO table_name
- VALUES (value1, value2, value3, ...);
Example
Insert value in a 1st way. The column names are used here
- INSERT INTO Employee (EmpId,LastName,FirstName,ADDRESS,City)
- VALUES (1, 'XYZ', 'ABC', 'India', 'Mumbai' );
- INSERT INTO Employee (EmpId,LastName,FirstName,ADDRESS,City)
- VALUES (2, 'X', 'A', 'India', 'Pune' );
Join the conversation! Your thoughts help the community grow.