1.創(chuàng)建表
CREATE TABLE public.company (
id int4 NOT NULL ,
name?text NOT NULL,
age int4 NOT NULL,
address bpchar(50) NULL,
salary float4 NULL,
join_date date NULL,
CONSTRAINT company_pkey PRIMARY KEY (id)
);
2.插入數(shù)據(jù)(不傳入id)
INSERT INTO public.company
(name, age, address, salary, join_date)文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-663690.html
VALUES('Kobe', 20, 'Lake', 10000, '1996-07-13');文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-663690.html
?3.由于主鍵id沒(méi)有實(shí)現(xiàn)自增,所以出現(xiàn)上面錯(cuò)誤。
4.新建序列: id-increase。postgresql中的通過(guò)序列,可以實(shí)現(xiàn)mysql中主鍵自增的效果。
?5.將新建序列id-increase應(yīng)用到company表的id主鍵上。nextval('"id-increase"'::regclass)
6.再次執(zhí)行插入語(yǔ)句,數(shù)據(jù)插入成功。
7.更簡(jiǎn)單的方法是在創(chuàng)建表的時(shí)候,使用serial類(lèi)型數(shù)據(jù)來(lái)指定主鍵id,輕松實(shí)現(xiàn)主鍵自增。
?建表語(yǔ)句:
CREATE TABLE COMPANY(
ID SERIAL PRIMARY KEY,
NAME TEXT NOT NULL,
AGE INT NOT NULL,
ADDRESS CHAR(50),
SALARY REAL,
join_date?date NULL,
);
插入數(shù)據(jù)(不傳入id):
INSERT INTO company
(name, age, address, salary, join_date)
VALUES('Kobe', 20, 'Lake', 10000, '1996-07-13');
到了這里,關(guān)于通過(guò)DBeaver 給Postgre SQL表 設(shè)置主鍵自增的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!