出处:http://netkiller.github.io/postgresql/index.html
5.1.INSERT
CREATE TABLE test (c varchar(5));
test=> INSERT INTO test VALUES ('1'); INSERT 0 1 test=> INSERT INTO test VALUES ('12'); INSERT 0 1 test=> INSERT INTO test VALUES ('123'); INSERT 0 1 test=> INSERT INTO test VALUES ('1234'); INSERT 0 1 test=> INSERT INTO test VALUES ('12345'); INSERT 0 1 test=> INSERT INTO test VALUES ('123456'); ERROR: value too long for type character varying(5) test=> INSERT INTO test VALUES ('1234567'); ERROR: value too long for type character varying(5) test=>
超出长度会提示 ERROR: value too long for type character varying(5)
通过 ::varchar(5) 截取5前五个字符,后面抛弃
test=> INSERT INTO test VALUES ('123456'::varchar(5)); INSERT 0 1 test=> INSERT INTO test VALUES ('1234567'::varchar(5)); INSERT 0 1 test=> INSERT INTO test VALUES ('12345678'::varchar(5)); INSERT 0 1
test=> select * from test; c ------- 1 12 123 1234 12345 12345 12345 12345 (8 rows)