Constrains
CHECK
CREATE TABLE products (
product_no integer,
name text,
price numeric CHECK (price > 0)
);
CREATE TABLE products (
product_no integer,
name text,
price numeric CHECK (price > 0),
discounted_price numeric CHECK (discounted_price > 0),
CHECK (price > discounted_price)
);
NULL / NOT NULL
CREATE TABLE products (
product_no integer NULL,
name text NOT NULL,
price numeric NOT NULL CHECK (price > 0)
);
UNIQUE
create table products (
product_no integer,
name text,
price numeric,
unique (product_no)
);
create table products (
product_no integer,
name text,
price numeric,
unique (product_no, name)
);
PRIMARY_KEY
CREATE TABLE products (
product_no integer UNIQUE NOT NULL,
name text,
price numeric
);
FOREIGN KEY
CREATE TABLE orders (
order_id integer PRIMARY KEY,
product_no integer REFERENCES products (product_no),
quantity integer
);
Ćwiczenia
- utworzyć każdy wymieniony constraint
- uzupełnić tabele danymi
- dokonać modyfikacji, które spowodują złamanie warunków constraint-ów