Insert statement for auto increment column
I create a table likes this:
CREATE TABLE test
(
ID AUTO_INCREMENT primary key,
Name VARCHAR(10) NULL DEFAULT '',
City VARCHAR(10) NULL DEFAULT ''
);
I know "insert into test(Name, City) values('Nan', 'Nanjing')" can insert record successfully.
But when using MySQL, I can use this statement to insert a record: "insert into db.test values(NULL, "Nan", "Nanjing")". While this statement can't work in Vertica.
So is it possible to construct a insert statement without adding column name like MySQL? Thanks very much in advance!
CREATE TABLE test
(
ID AUTO_INCREMENT primary key,
Name VARCHAR(10) NULL DEFAULT '',
City VARCHAR(10) NULL DEFAULT ''
);
I know "insert into test(Name, City) values('Nan', 'Nanjing')" can insert record successfully.
But when using MySQL, I can use this statement to insert a record: "insert into db.test values(NULL, "Nan", "Nanjing")". While this statement can't work in Vertica.
So is it possible to construct a insert statement without adding column name like MySQL? Thanks very much in advance!
0
Comments
\d <table_name>
You can use below method :
create sequence test_sequence;
CREATE TABLE test_1
(
Name VARCHAR(10) NULL DEFAULT '',
City VARCHAR(10) NULL DEFAULT '',
ID INT DEFAULT NEXTVAL('test_sequence')
);
dbadmin=> insert into test_1 values('Nan', 'Nanjing');
OUTPUT
--------
1
(1 row)
dbadmin=> selECT * from test_1;
Name | City | ID
------+---------+----
Nan | Nanjing | 1
(1 row)
dbadmin=> insert into test_1 values('Nan', 'Nanjing',NULL);
OUTPUT
--------
1
(1 row)
dbadmin=> selECT * from test_1;
Name | City | ID
------+---------+----
Nan | Nanjing | 1
Nan | Nanjing |
(2 rows)
Firstly, thanks very much for your response!
Secondly, your answer can't help me:
a) The ID should be primary key;
b) After using your method, the ID column is null, not 2.
So I think it isn't a answer I want.
Hence, insert into test values(NULL, "Nan", "Nanjing") will not work in Vertica for the table DDL you have up there.
Why you don't asks from MySQL to support in analytical functions of Vertica?
PS
There are a lot of databases and if everyone will ask to implement to what he used in database X, so Vertica will be stacked in implementing these features and not in what it should do.
PPS
But if you are stubborn and insist , here's directive, which will allow to you INSERT to IDENTITY column(yes, its possible, Vertica super flexible database) PPPS
FYI: but NULLs you can not insert, since IDENTITY means NOT NULL constraint.
MySQL do a lot of things implicitly, like GBY columns elimination - and IMO its wrong, terrible, horrible and source of many bugs.
Against to MySQL, Vertica requires to do things explicitly - for my opinion its a right design
+100 to Vertica
-5000 to MySQL for implicit things
Python Zen MySQL - ugly and implicit.
Vertica - beautiful and explicit.
I'm not aware of any way to do that in Vertica, so if I didn't have a canonical list of column names for a table, I would just ask the database to enumerate the columns that exist. I.e: