CREATE TABLE command in SQL Server
In SQL Server (Transact-SQL), the CREATE TABLE statement is used to create and define tables.
Syntax CREATE TABLE command in SQL Server
CREATE TABLE ten_bang
(
cot1 kieu_du_lieu [ NULL | NOT NULL ],
cot2 kieu_du_lieu [ NULL | NOT NULL ],
…
);
Variable name or variable value
ten_bang
The name of the table you want to create.
cot1, cot2
The column you want to create in the table. Each column has 1 data type. The column must be defined as NULL or NOT NULL, if left blank, the database will default to NULL.
For example
CREATE TABLE nhanvien
( nhanvien_id INT NOT NULL,
ho VARCHAR(50) NOT NULL,
ten VARCHAR(50),
luong MONEY
);
The CREATE TABLE command above will create a table named nhanvien with 4 columns:
- The nhanvien_id column (employee ID) has an INT data type and does not contain a NULL value.
- The second column is ho (employee family) of VARCHAR data type (maximum 50 characters) and does not contain NULL value.
- The third column is the ten (employee name) of the VARCHAR data type (up to 50 characters) and can contain NULL values.
- The fourth column is luong (employee salary) with MONEY data type and can contain NULL values.
The only problem with this CREATE TABLE statement is that the primary key has not been defined for the table. It is possible to edit a bit and define it as the primary key as follows.
CREATE TABLE nhanvien
( nhanvien_id INT PRIMARY KEY,
ho VARCHAR(50) NOT NULL,
ten VARCHAR(50),
luong MONEY
);
By using the keyword PRIMARY KEY after the nhanvien_id field, SQL Server will retrieve the username as the primary key for the table.
Previous article: Data types in SQL Server
Lesson: Main key PRIMARY KEY in SQL Server
You should read it
May be interested
- DELETE command in SQL Serverthe delete statement in sql server (transact-sql) is used to delete one or more records from a table in sql server.
- LOCAL TEMPORARY TABLE in SQL Serverthis tutorial explains how to use local temporary table in sql server with syntax and examples.
- GOTO command in SQL Serverthe goto command is a simple jump command, which allows an unconditional jump program from goto to a location in the program that has a label (laber) command in the same function.
- Create automatic table of contents in Word 2003creating an automatic table of contents in word 2003 makes the document look more professional, easy to find the item you need by clicking on the table of contents. so how to create a table of contents in word fastest? the answer is to create an automatic table of contents!
- Clean command in Windowsthe clean command deletes all partitions or formats the volume from the current drive. the command applies to windows server (semi-annual channel), windows server 2019, windows server 2016, windows server 2012 r2, windows server 2012.
- DELETE TOP command in SQL Serverthe delete top command in sql server is used to delete records from a table in sql and limits the number of records based on an existing value or percentage.
- Nbtstat command in Windows(applies to: windows server (semi-annual channel), windows server 2016, windows server 2012 r2, windows server 2012)
- CASE function in SQL Server (part 1)in sql server, the case function verifies the value based on the list of given conditions, then returns one or more results. in this article we will illustrate a number of different uses of this function in different situations.
- The sfc command in Windows(applies to windows server (semi-annual channel), windows server 2016, windows server 2012 r2, windows server 2012)
- The cacls command in Windowsthe cacls command displays or modifies an arbitrary access control list (dacl) on the specified file. the command applies to windows server (semi-annual channel), windows server 2016, windows server 2012 r2, windows server 2012.