Altering table to add column before specific column - mysql

Say I have this structure:
col1 | col2 | col3 | col4 | created | createdby
I want to add a column after col4, but there are a few problems. There could be any number of 'Col' columns, so using AFTER isn't an option (Ill never know what it comes after). So, naturally, I though I can just do BEFORE created, right? Well this doesn't work:
ALTER TABLE table ADD extracol VARCHAR(255) BEFORE created
Is there a way to get this working? I just get an invalid syntax error. I would have though if I can add a column AFTER a specific column, then I can do it BEFORE but apparently that's not the case?

It is possible, but it takes two queries and little 'out of the box' thinking:
First insert the new column after the column we really want it before:
ALTER TABLE table ADD extracol VARCHAR(255) AFTER created
Then use another alter command to move the previous column after the new one. You'll need the right column type for the other column here:
ALTER TABLE table MODIFY created DATETIME AFTER extracol
Untested. Backup all your data before testing or running.

Unfortunately you cannot do that
If you really want them in that specific order you will have to create a new table with the columns in that order and copy the existing data Or rename columns
There is no easy way

ALTER TABLE tablename ADD columnname INT AFTER anothercolumn
the above syntax is valid in MySQL but is not valid in SQL Server.

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.

change the type of a column in a view?

I have a view test with the column year( int) and ID (int)
I want to change every value in the year column into a string and concatenate " 's" to every value in the column. How would be the best way to go about this? I've tried altering my views but it never works for some reason.
alter test alter column year varchar(11)
Sorry i'm new to sql and been trying to look up how to do this for a while and can't find a definite answer to this.
Syntax to change column name in MySql:
alter table table_name change old_column_name new_column_name data_type(size);

use of hyphens in table not working

I had a table name called emp-reg in mysql.When I gave desc emp-reg it showed me an error.
Where as when I changed the table name RENAME TABLE emp-reg TO emp and then gave desc emp it worked perfectly.Why is desc not working for-
what will happen if we perform insert operation in table such asemp-reg?
Instead, you can use _ under score.
- is treated as minus sign and hence is rejected.
Or you can use back ticks around the name with - in it. Say
`emp-reg`.
If you want to rename any of such columns in your table, use alter table command with change option.
ALTER TABLE table_name CHANGE COLUMN `old-col-name` `new_col_name` int
You can use any desired or matching data type for the column with the earlier definition.

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 long does it take to create a large table in MYSQL?

When I imported a table with 30 million rows from a text file to a MYSQL table it only took 1 minute. However, I realized that I missed a column and that I needed to add it to the table. From the MYSQL command line, I wrote the following command:
create tableC as(tableA.T1, tableB.ZID from tableA, table B where A.ZID = B.ZID)
It's been over one hour and the command has not terminated. Does anyone know the reason why? TableB was already in the MYSQL server.
Not 100% sure but you might be better off altering the table first and adding the column, then doing an update to populate that column
ALTER TABLE tableB ADD COLUMN colA yourColumnDefinition;
UPDATE tableB SET colA = <however you do it>;
I'm not totally sure which table you are adding the column to or how you are computing it. Based on the query you posted it looks like you are creating a mapping table to map two table's IDs to each other. If that is the case you would probably be better off putting a foreign key in one of the tables.
Again, this might not be exactly what you are looking for but if you just want to add a column to a table you already created this might be a better approach.