Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Databases
  • SQL
  • MySQL
  • PostgreSQL
  • PL/SQL
  • MongoDB
  • SQL Cheat Sheet
  • SQL Interview Questions
  • MySQL Interview Questions
  • PL/SQL Interview Questions
  • Learn SQL and Database
Open In App
Next Article:
SQL CREATE TABLE
Next article icon

CREATE TABLE in SQL Server

Last Updated : 05 Apr, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

SQL Server provides a variety of data management tools such as querying, indexing, and transaction processing. It supports multiple programming languages and platforms, making it a versatile RDBMS for various applications. With its robust features and reliability, SQL Server is a popular choice for enterprise-level databases.

In this article, we will learn how to efficiently use CREATE TABLE statements to create a table in our database. We will cover all the basic concepts with clear and concise examples along with their respective.

CREATE TABLE Statement in SQL Server

In SQL Server, the CREATE TABLE statement is used to create a table in our database. We can say it is a fundamental step for defining a table with our database. This statement allows us to set the table name with attributes of its specified columns.

Syntax:

CREATE TABLE [Schema].[table_name ](

column_01 [datatype] (length){Add constraint optional},

column_02 [datatype] (length) {Add constraint optional}

..............

............

) ON [File group] ;

Example of CREATE TABLE Statement in a Specific Schema

In this, we are going to create a table named 'geeksforgeeks'. It will consist of 'id', 'name', and 'potd' as a column. We are also going to insert values in our table and display them. We are going to create our table in a specified schema i.e. 'dbo'.

Schema: It is a collection of database objects such as table, views etc.

Query:

CREATE TABLE [dbo].[geeksforgeeks]
(
[id] [int] IDENTITY(1,1) ,
[name] [varchar](20),
[potd] [int]
) ON [PRIMARY]

After executing this statement, we can see a table has been created in our database.

Output:

table-gfg
Creating table geeksforgeeks

Explanation: We can see our table has been created in our database. In our table, id, name, and potd columns have been created with integer, varchar, and integer data type respectively. IDENTITY(1,1) in the 'id' column means this column value will be automatically generated by SQL starting from 1 and then incrementing by 1 for each row.

Now we are done with creating our table, let's add some data to our table and display them. We will use the "INSERT INTO" statement to insert data into our table.

Query:

-- Inserting values into our table 'geeksforgeeks'
INSERT INTO geeksforgeeks ([name],[potd])
VALUES ('Vishu', 200);
INSERT INTO geeksforgeeks ([name],[potd])
VALUES ('Neeraj', 150);
INSERT INTO geeksforgeeks ([name],[potd])
VALUES ('Aayush', 125);
INSERT INTO geeksforgeeks ([name],[potd])
VALUES ('Vivek', 100);

-- Displaying table's data
SELECT * FROM geeksforgeeks;

Output:

table_display
Table - geeksforgeeks

Explanation: We can observe that all our table's entered data has been displayed. As specified earlier, we can see that we have not explicitly inserted value in our 'id' column. We can see that the and 'id' column values start from 1 and then incremented by 1 for each next row.

Example of CREATE TABLE with Constraints

In this, we are going to create our table 'courses' in our database. It will consist of the id, name, and the course as a column. Unlike previous example, we are going to specify UNIQUE and NOT NULL constraints in some of the columns of this table.

Query:

CREATE TABLE [courses]
(
[id] [int] UNIQUE,
[name] [varchar](20) NOT NULL,
[course] [varchar] (50)
) ON [PRIMARY]

Likewise in the previous example, our table will be created after the successful execution of the above query.

Output:

table-courses
Table - courses

We can see that our table has been created. Now violating any of the above-mentioned constraints will result in an error.

Let's try to break UNIQUE Key Constraint Condition

Query:

INSERT INTO courses ([id],[name],[course])
VALUES (1, 'Vishu', 'Python');
INSERT INTO courses ([id],[name],[course])
VALUES (1, 'Neeraj', 'Java');

Output:

error-unique
Unique Key Error

Explanation: We can see that we have tried to duplicate the id which is prohibited in a column with a unique key constraint. We can see that an error has been thrown displaying the warning for inserting a duplicate value in a Unique key column.

Conclusion

Overall, SQL Server is a strong RDBMS built to efficiently manage large data sets. The CREATE TABLE command in SQL Server is used to establish and specify the structure of a table within a database. We have seen how to apply column constraints in conjunction with the CREATE TABLE statement in a particular schema. You now understand the CREATE TABLE statement. You can now create queries related to it and get the desired results.


Next Article
SQL CREATE TABLE

V

vishuvaishnav3001
Improve
Article Tags :
  • SQL Server
  • Databases

Similar Reads

  • SQL Server ALTER TABLE
    In SQL Server, there are various commands to Add, Update, and Delete a Database Schema called DDL or Data Definition Language commands. A Table in a database is part of the database schema and the 'ALTER TABLE Moify Column' command is used to make changes to the database table column data type, colu
    5 min read
  • PostgreSQL - CREATE TABLE
    In PostgreSQL, the CREATE TABLE statement is used to define a new table within a database. It allows us to specify the table's structure, including column names, data types, and constraints, ensuring data integrity and consistency. Understanding the PostgreSQL table creation process is essential for
    5 min read
  • SQL Server Describe Table
    SQL Server is a widely used Relational Database Management System (RDBMS) that allows users to create and manage databases effectively. When working with databases in SQL Server it is essential to understand the schema of the tables present in the database. Describing a table means getting informati
    4 min read
  • SQL Server Copy Table
    Copying or replicating tables is one of the crucial functions of database management systems. Copy table is a crucial option to create table data backups or to create duplicate data from a table to another table with few columns or some of the data for various purposes. In this article, We will lear
    4 min read
  • SQL CREATE TABLE
    In SQL, creating a table is one of the most essential tasks for structuring your database. The CREATE TABLE statement defines the structure of the database table, specifying column names, data types, and constraints such as PRIMARY KEY, NOT NULL, and CHECK. Mastering this statement is fundamental to
    5 min read
  • MySQL CREATE TABLE
    Creating tables in MySQL is a fundamental task for organizing and managing data within a database. Tables act as structured containers, similar to spreadsheets, where data is stored in rows and columns. In this article, we will explore the process of creating tables in MySQL using both the Command L
    4 min read
  • SQL Server Common Table Expressions
    SQL Server is a relational database management system (RDBMS) that is used to handle complex data and maintain it in of tabular manner. With the help of SQL Server, one can easily protect their data as it provides various security features. In this article, we are going to explore SQL server's CTE a
    8 min read
  • How to Declare a Variable in SQL Server?
    In SQL Server, variables play a critical role in the dynamic execution of SQL scripts and procedures. Variables allow you to store and manipulate data temporarily within the scope of a batch or procedure. By using the DECLARE statement, you can create variables with specific data types, which can th
    6 min read
  • Copy Tables Between Databases In SQL Server
    Copying tables between databases in SQL Server can be crucial for data migration, backups, or setting up test environments. One effective method to achieve this is by generating scripts using SQL Server Management Studio (SSMS). In this article, we will learn how to Copy Tables Between Databases In
    3 min read
  • Create Database in MS SQL Server
    Databases in Microsoft SQL Server are crucial for managing data, categorized into system databases, which are auto-created and user databases, created by users. In this article, We will learn about the basics of system and user databases along with methods for creating and managing them using T-SQL
    6 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences