insert in database with column number - mysql

i want to insert in a table using columns order not name
insert into tableName(1,2,5) values('val1','val2','val3');
i dont want to use
insert into tableName values('val1','val2','val3');
because the table does not contain just 3 columns
how can i do it
because columns name are encrypted so I can not rely on this
insert into tableX("cCGSvKJVQXnt8A==","aDOlOQrPfg==","qsdcx112")
values('val1','val2','val3');
is there any idea how can i deal with this
thank

You can't use the ordinal number of a column in an insert statement. However, you can accomplish what you're trying to do (insert values into specific columns in a table) using the column names instead.
Presume your table has five columns; I'm going to call them "Alpha", "Bravo", "Charlie", "Delta", and "Echo", since you haven't given us the schema for your table, but replace these names with the names of the columns actually in your table. I'm guessing that your third and fourth column (my "Charlie" and "Delta") are nullable. You can then insert a tuple/row in your table with the other three columns filled using syntax like this:
INSERT INTO TableName(Alpha, Bravo, Echo) VALUES ("val1", "val2", "val3");
If, per your comments above, your column names are unprintable characters (which is a terrible, terrible idea), you can explicitly insert NULLs into the missing columns:
INSERT INTO TableName VALUES ("val1", "val2", NULL, NULL, "val3");
but the weakness here is that, if additional columns are subsequently added to your table's schema, the insert statement will start failing.

You need to put the column names where you have the 1,2,5. You can't use the column number.
insert into tableName(1,2,5) values("val1","val2","val3");

Related

How to avoid to rewrite attributes with AUTO_INCREMENT

If I have a table like this:
CREATE TABLE Person (id INT AUTO_INCREMENT PRIMARY KEY, age INT);
Is there a simpler method than rewriting the attributes of the column like that ?
INSERT INTO Person (age) VALUES (18);
I know that for the DEFAULT values there is the keyword DEFAULT, but is there a similar one for AUTO_INCREMENT? I work with pretty long tables and I don't want to rewrite all the column names each time I make an INSERT.
Thank you for your help.
https://dev.mysql.com/doc/refman/5.7/en/example-auto-increment.html says:
No value was specified for the AUTO_INCREMENT column, so MySQL assigned sequence numbers automatically. You can also explicitly assign 0 to the column to generate sequence numbers, unless the NO_AUTO_VALUE_ON_ZERO SQL mode is enabled. If the column is declared NOT NULL, it is also possible to assign NULL to the column to generate sequence numbers.
So any of the following will work:
INSERT INTO Person VALUES (0, 18);
INSERT INTO Person VALUES (NULL, 18);
INSERT INTO Person VALUES (DEFAULT, 18);
However, it's considered good practice to list all columns explicitly when you write any INSERT statement. If someone changes the order of columns in the table, your VALUES might not get inserted into the right columns unless you list the column names explicitly. Also if someone adds or drops a column.

Insert into table from another with different columns count (MySQL)

Question, why this dose not work?
create table one (a int(1) default 1, b int(2));
create table two (b int(1));
insert into one select * from two;
error:
Column count doesn't match value count at row 1
a know it, a can count, but why, philosophically?
database knows, what the name of inserting column from table two is b, knows that the column a in table one has a default value equal 1..
so, what problem of executing this query?
And general - How can i do this differently, not manual, without information of a columns and their count, if this way is impossible?
I know this:
table two always have all the same columns, that the table one have. But table one have another columns too, that have a some default values.
Is there some way to do that? insert all data from two in one, and fill the remaining columns by some default or other values!
Need help!
Thank you very match!
When you run:
insert into one
select * from two;
The SQL engine automatically puts in the columns that are implied.
For the insert, this is the list of columns in declaration order.
For the *, this is the list of columns in declaration order.
There is no "matching" of columns by names, only lists of columns in each table.
So, the query is really:
insert into one(a, b)
select b from two;
That looks like an error to me.
Moral of the story? Write that code that you intend. Always include columns lists, particularly for insert statements. So write:
insert into one(b)
select b from two;

Copy Data From One Database To Another In Same Table

I want to copy data from one database which has table named domains to another database which also has the table named domains to it.
I have tried doing it using phpmyadmin but it doesn't copy maybe because of Auto_increment values. it just doesn't get copied to another database.table.
I want to know what can be done in this regard? also I don't want to copy old ID(auto_increment) values from first database to another.
phpmyadmin response.
#1136 - Column count doesn't match value count at row 1
both the structures are same in the databases still this.
My Query.
INSERT INTO `site1`.`domains`
SELECT * FROM `site33`.`domains`
^ This much is fixed.
now comes the auto_increment problem I Get:
#1062 - Duplicate entry '1' for key 'PRIMARY'
You could do something like this:
INSERT INTO database2.table1 (field2,field3)
SELECT table2.field2,table2.field3
FROM table2;
Note:
Do not include the column which is auto increment in your insert and select statement.
Sql Fiddle example
You are using an insert statement that has an incorrect number of values, for instance:
for a table with columns a, b and c, these are all invalid:
INSERT INTO YourTable VALUES (1, 2);
INSERT INTO YourTable(b, c) VALUES (1, 2, 3);
INSERT INTO YourTable(a, b, c) VALUES (2, 3);
The number of columns in the column list must match the number of values in the value list.
You may only omit the column list, but only if you specify one value for each column and in the order in which the columns exist in the table. This is bad practice, though. It is better to always specify the exact columns you need.

MySQL insertion with all values at once

I was doing an INSERT INTO TABLE(...,...,...,...,...) VALUES(...,...,...,
When I closed by mistake my MySQL Query Browser. The table has too many columns, so I was wondering, is there a command that you don't need to type all names of the columns table?
If so, how?
THere is
INSERT INTO TABLE VALUES(...,...,...)
You just need to specify ALL fields in EXACTLY same sequence as they're in table definition.
For AUTO_INCREMENT column, or for columns where you want to use DEFAULT value as defined in table definition (also TIMESTAMPs) use null as a value.
If you are insterting into all the columns you can write:
insert into tablename values(...,...,etc.)

MySQL skip a column name when inserting values

I have a table in which the first column is auto_increment. I want to insert data into the table, but skip the first column as it is updated automatically when a new row is begun.
so:
INSERT INTO table VALUES (NULL,"lady","gaga","rulz");
But NULL cannot be inserted into a column as I specified earlier. What do I need to replace NULL with so that the column doesn't get anything inserted into it?
Just make sure you specify the respective column names
INSERT INTO table (col1, col2, col3) VALUES ("lady","gaga","rulz");
You don't even need to fill all columns (if they are not required), Ie.
INSERT INTO table (col2) VALUES ("gaga");
insert into table(field1, field2, field3) values('lady', 'gaga', 'sucks')
You need to explicitly specify the column names and order. In other words
INSERT INTO table (field2, field3, field4) VALUES ("lady","gaga","rulz");
BTW it is typically a good idea to avoid the implicit insert syntax (but maybe for the simplest / debug time snippets), lest you get surprised when/if the underlying table schema was somehow changed.
Also, so you know, something, will get inserted into the (here) field1 column. When you define or alter the schema of a table, a given field can either be nullable or not, and/or it can have a default value or not. In the SQL provided above, you can only omit the values for fields that are either nullable or have a default value; SQL will otherwise return an error and won't insert any part of the new record.