SQL Table: Non-Null & Unique Constraints

To create a non-empty unique table in SQL, you can use the following syntax:

CREATE TABLE table_name (
    column1 datatype CONSTRAINT constraint_name NOT NULL UNIQUE,
    column2 datatype,
    ...
);

In the above syntax, table_name refers to the name of the table to be created, column1 and column2 are the names of the columns in the table, and datatype represents the data type of the column. CONSTRAINT constraint_name is used to name the constraint, but it can be optional. NOT NULL is used to make a column mandatory; that is, the value in that column cannot be empty. UNIQUE is used to ensure that the values in the column are unique and do not have duplicates in the table.

Here is an example:

CREATE TABLE students (
    id INT CONSTRAINT pk_students PRIMARY KEY,
    name VARCHAR(100) NOT NULL UNIQUE,
    age INT
);

The example above created a table called students, which has three columns: id, name, and age. The id column is set as the primary key, and the name column is set as both non-null and unique.

bannerAds