Table of Contents
MS SQL Server: CREATE TABLE Command in SQL Server is easier to understand when the core ideas are paired with practical examples. The sections below explain the topic clearly, highlight useful steps, and point out details that can prevent common errors.
In SQL Server (Transact-SQL), the CREATE TABLE statement helps 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 instance,
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);
Lesson: Main key PRIMARY KEY in SQL Server
FAQ
What is MS SQL Server: CREATE TABLE Command in SQL Server?
In SQL Server (Transact-SQL), the CREATE TABLE statement helps create and define tables.
Why is MS SQL Server: CREATE TABLE Command in SQL Server important?
A clear understanding of MS SQL Server: CREATE TABLE Command in SQL Server helps you make informed decisions, avoid common mistakes, and use the relevant tools or techniques more effectively.
How should beginners approach MS SQL Server: CREATE TABLE Command in SQL Server?
Start with the fundamental concepts, follow the examples step by step, and test each change in a safe environment before applying it to important systems or data.
Reader Comments 0
Sign in with email or Google to join the discussion.