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:
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:
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.
CREATE TABLE in SQL Server
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.
Contact Us