CREATETABLE employee(
id SERIALPRIMARYKEY,
name VARCHAR(255)NOTNULL,
birthdate timestampNOTNULL,
info TEXT,
salary REALCHECK(salary >2000),
is_staff booleanDEFAULTTrueNOTNULL,
is_delete booleanDEFAULTFalseNOTNULL,
create_time timestampDEFAULTCURRENT_TIMESTAMP);
查看表信息
>\d employee
Table "public.employee"
Column | Type | Collation | Nullable | Default
-------------+-----------------------------+-----------+----------+---------------------------------------------
--------------
id| integer || not null | nextval('employee_id_seq'::regclass)
name | character varying(255)|| not null |
birthdate | timestamp without time zone || not null |
info | text |||
salary | real |||
is_staff | boolean || not null |true
is_delete | boolean || not null |false
create_time | timestamp without time zone |||'2019-08-29 14:22:06.446794'::timestamp with
out time zone
Indexes:
"employee_pkey" PRIMARY KEY, btree (id)
Check constraints:
"employee_salary_check" CHECK (salary > 2000::double precision)
约束测试
> INSERT INTO employee (name, birthdate, salary) values('王一', now(), 2500);
INSERT 0 1
> INSERT INTO employee (name, birthdate, salary) values('赵四', now(), 1800);
ERROR: new row for relation "employee" violates check constraint "employee_salary_check"
DETAIL: Failing row contains (2, 赵四, 2019-08-29 15:21:11.911674, null, 1800, t, f, 2019-08-29 14:22:06.446794).
> INSERT INTO employee (name, birthdate, salary) values('赵四', now(), 3000);
INSERT 0 1
>select * from employee;id| name | birthdate | info | salary | is_staff | is_delete | create_time
----+------+----------------------------+------+--------+----------+-----------+----------------------------
1 | 王一 | 2019-08-29 15:20:43.819572 || 2500 | t | f | 2019-08-29 15:31:40.29647
3 | 赵四 | 2019-08-29 15:32:04.320762 || 3000 | t | f | 2019-08-29 15:32:04.320762
(2 rows)