MySQL insertion with all values at once - mysql

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.)

Related

Create a table and specify data types according to an existing query

Let's say I have a table like this
this table is the result of a query from another larger table stored in my database
All I want is to create a table like this one above and specify for each column a custom format and store it into my database
I know that I could do create table mytab as select ... etc
however i don't know how to specify the column formats that I want in mysql
could you please help ?
If you have the query sql, you should be able to do a select into to store the results in a table. Add a LIMIT clause to just store one row. You could then do SHOW CREATE TABLE tablename (from this SO answer) to get the SQL for creating the table. It would be up to you to figure out what your primary key should be.
Assuming with column formats you mean data types: Use CAST to cast to the desired data type.
create new_table as
select
cast( a.metrique as varchar(100) ) as metrique,
cast( b.nombre_de_lignes as int ) as cote_de_lignes, ...
from ...
You may specify columns properties completely or partially. Like there is no SELECT part, and you simply create empty table.
I.e. like
CREATE TABLE table_name ({any definitions allowed in table creation query:
columns specifications, indices, constraints, FKs, etc.})
SELECT ...
In this form each output column in SELECT must have alias which matches according column name defined in CREATE TABLE part. If alias is absent in the structure then a column with the name==alias will be added to the table definition with dynamically formed properties.

Mysql ,how to add two new columns and insert data at the same time?

It's my code
alter table num_of_people add column date_pre varchar(40);
alter table num_of_people add column time_pre varchar(40);
The result like this:
enter image description here
But I can't insert new data into these two columns:
insert into num_of_people(date_pre,time_re) select left(Deal_time,8),right(Deal_time,2) from num_of_people;
Nothing happened:
enter image description here
I create a new table,that's worked:
create table Date_time(
date_p varchar(40),
time_p varchar(40)
);
insert into date_time(date_p,time_p) select left(Deal_time,8),right(Deal_time,2) from num_of_people;
So,Why?
I just want to split the charactor in column Deal_time of table num_of_people. What's the better way? I am a novice in MySQL.Thanks a lot.
According to your images, you should perform an Update query instead of Insert to the table num_of_people.
To explain why you can't insert new data into those two columns, my best guess is that at least one of your existings columns must be a NOT NULL column that has no explicit DEFAULT value defined. Even if you succeeded in executing the Update, the total number of your table's records would be doubled.
Ref: http://dev.mysql.com/doc/refman/5.6/en/data-type-defaults.html
For data entry into a NOT NULL column that has no explicit DEFAULT
clause, if an INSERT or REPLACE statement includes no value for the
column, or an UPDATE statement sets the column to NULL, MySQL handles
the column according to the SQL mode in effect at the time:
If strict SQL mode is enabled, an error occurs for transactional
tables and the statement is rolled back. For nontransactional tables,
an error occurs, but if this happens for the second or subsequent row
of a multiple-row statement, the preceding rows will have been
inserted.
If strict mode is not enabled, MySQL sets the column to the implicit
default value for the column data type.

insert in database with column number

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");

INSERT ... SELECT to same table

I have a table that has a number of columns. For each row, I'd like to select three columns (PAR_BOOK, PAR_PAGE, PAR_LINE) and concatenate the contents of those three columns into a new fourth column (APN).
So, if PAR_BOOK=0108, PAR_PAGE=291 and PAR_LINE=07, APN should be 010829107
Make sense?
But, I'm unsure of what query I should use to do this. I need the results stored back in the same table as it needs to be ultimately exported out as a csv to work with the program that's going to map the data.
Assuming your fourth column is already in the table, you would use the following update query:
UPDATE YourTable
SET APN = CONCAT(PAR_BOOK, PAR_PAGE, PAR_LINE)
If your fourth column is not present in the table yet, you should use the ALTER TABLE statement to add it first before running the UPDATE statement:
ALTER TABLE YourTable
ADD APN VARCHAR(256) NULL
Inserting into the same table with INSERT INTO ... SELECT ... is no problem at all. MySQL holds the selected rows in a temporary table.

How can I modify an existing column's data type?

One of the columns in a somewhat large table (~10,000 records) is of the data type DECIMAL(10,0). I'm using MySQL.
I'd like the values to be displayed to 2 decimal places, so I need to alter this to DECIMAL(10,2), without screwing up the table's existing records. How could this be done?
Which DBMS are you using ? you can try like this for MySQL :
alter table tblName modify columnName newDataType;