What are Constraints ? What are the types of Constraints ?

Will be written shortly !

What are Constraints ? What are the types of Constraints ?

What are Constraints?
Constraints are a way to limit the kind of data that can be stored in a table.
Types of Constraints:
1:Check Constraint:
A check constraint is the most generic constraint type.We can use it on multiple columns. It allows you to specify that the value in a certain column must satisfy an arbitrary expression. For instance, to require positive product prices, you could use:


CREATE TABLE products (
product_no integer,
name text,
price numeric CHECK (price > 0)
);

2:Not Null Constraint:
A not-null constraint simply specifies that a column must not assume the null value.It can also be used with multiple columns. A syntax example:

CREATE TABLE products (
product_no integer NOT NULL,
name text NOT NULL,
price numeric
);

3:Unique Constraint:
Unique constraints ensure that the data contained in a column or a group of columns is unique with respect to all the rows in the table.
Unique Constraint column allows NULL values.
The syntax is

CREATE TABLE products (
product_no integer UNIQUE,
name text,
price numeric
);
If a unique constraint refers to a group of columns, the columns are listed separated by commas:

CREATE TABLE example (
a integer,
b integer,
c integer,
UNIQUE (a, c)
);

4:Primary Key Constraints:
a primary key constraint is simply a combination of a unique constraint and a not-null constraint.Primary keys can also constrain more than one column; the syntax is similar to unique constraints.A table can have at most one primary key (while it can have many unique and not-null constraints).

CREATE TABLE products (
product_no integer PRIMARY KEY,
name text,
price numeric
);

5:Foreign Key Constraints:
A foreign key constraint specifies that the values in a column (or a group of columns) must match the values appearing in some row of another table. We say this maintains the referential integrity between two related tables.A foreign key can also constrain and reference a group of columns.
So we define a foreign key constraint in the orders table that references the products table:

CREATE TABLE orders (
order_id integer PRIMARY KEY,
product_no integer REFERENCES products (product_no),
quantity integer
);

here product_no is foreign key of table orders which refers product_no of products table.