W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
AUTO INCREMENT(自動(dòng)增長(zhǎng))會(huì)在新記錄插入表中時(shí)生成一個(gè)唯一的數(shù)字。
PostgreSQL使用序列來(lái)標(biāo)識(shí)分區(qū)的自增長(zhǎng),數(shù)據(jù)類型有smallserial,serial和bigserial。這些屬性數(shù)據(jù)庫(kù)MySQL數(shù)據(jù)庫(kù)支持的AUTO_INCREMENT屬性。
使用MySQL設(shè)置自動(dòng)增長(zhǎng)的語(yǔ)句如下:
CREATE TABLE IF NOT EXISTS `w3cschool_tbl`( `w3cschool_id` INT UNSIGNED AUTO_INCREMENT, `w3cschool_title` VARCHAR(100) NOT NULL,
`w3cschool_author` VARCHAR(40) NOT NULL,
`submission_date` DATE, PRIMARY KEY ( `w3cschool_id` )
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
MySQL是用AUTO_INCREMENT這個(gè)屬性來(lái)標(biāo)識(shí)細(xì)分的自增。
PostgreSQL使用序列來(lái)標(biāo)識(shí)等級(jí)的自增長(zhǎng):
CREATE TABLE w3cschool
( id serial NOT NULL, alttext text, imgurl text )
SMALLSERIAL,SERIAL和BIGSERIAL范圍:
偽類型 | 存儲(chǔ)大小 | 范圍 |
---|---|---|
SMALLSERIAL | 2字節(jié) | 1到32,767 |
SERIAL | 4字節(jié) | 1到2,147,483,647 |
BIGSERIAL | 8字節(jié) | 1到922,337,2036,854,775,807 |
SERIAL數(shù)據(jù)類型基礎(chǔ)語(yǔ)法如下:
CREATE TABLE tablename ( colname SERIAL );
預(yù)先我們要?jiǎng)?chuàng)建一張公司表,并創(chuàng)建以下幾個(gè)分段:
w3cschooldb=# CREATE TABLE COMPANY( ID SERIAL PRIMARY KEY, NAME TEXT NOT NULL, AGE INT NOT NULL, ADDRESS CHAR(50), SALARY REAL );
現(xiàn)在往表中插入幾條記錄:
INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY) VALUES ( 'Paul', 32, 'California', 20000.00 ); INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY) VALUES ('Allen', 25, 'Texas', 15000.00 ); INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY) VALUES ('Teddy', 23, 'Norway', 20000.00 ); INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY) VALUES ( 'Mark', 25, 'Rich-Mond ', 65000.00 ); INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY) VALUES ( 'David', 27, 'Texas', 85000.00 ); INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY) VALUES ( 'Kim', 22, 'South-Hall', 45000.00 ); INSERT INTO COMPANY (NAME,AGE,ADDRESS,SALARY) VALUES ( 'James', 24, 'Houston', 10000.00 );
查看公司表的記錄如下:
id | name | age | address | salary ----+-------+-----+------------+-------- 1 | Paul | 32 | California | 20000 2 | Allen | 25 | Texas | 15000 3 | Teddy | 23 | Norway | 20000 4 | Mark | 25 | Rich-Mond | 65000 5 | David | 27 | Texas | 85000 6 | Kim | 22 | South-Hall | 45000 7 | James | 24 | Houston | 10000
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: