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.
Related
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.
ALTER TABLE customer_rewards
ALTER COLUMN count(rental_id) total_rentals
bigint(21);
I'm trying to change name of the column: count(rental_id) to total_rentals
MySql recognizes count as a function instead of a column name. I can't go in and change the name where you create the table, I can only use DDL to modify existing columns. I've tried using quotes (both single and double) and it still isn't working. How can I get around this?
I'm afraid to ask how that table ended up with such a column name... Anyway: this identifier contains parentheses, so it needs to be quoted: for this you need to use backticks in MySQL:
ALTER TABLE customer_rewards ALTER COLUMN `count(rental_id)` total_rentals bigint(21);
I want to sort a MySQL table using ALTER TABLE (so it stays sorted), and it works fine when I simply put in 1 column name like this:
ALTER TABLE data ORDER BY upvotes DESC;
But I want to sort it based upon the difference of two values, and when I do that I get "Unrecognized alter operation."
This is the code I want to work:
ALTER TABLE data ORDER BY (upvotes-downvotes) DESC;
Is there a way to do this, or am I just getting the syntax wrong? Thanks in advance!
As mysql documentation on alter table says (highlighting is mine):
ORDER BY syntax permits one or more column names to be specified for sorting, each of which optionally can be followed by ASC or DESC to indicate ascending or descending sort order, respectively. The default is ascending order. Only column names are permitted as sort criteria; arbitrary expressions are not permitted. This clause should be given last after any other clauses.
As #juergend suggested in his comment, the possible workaround is to store the results of the calculation in a field and use that for sorting.
Also, I'm not sure why you are still stuck with myisam table type. I would definitely consider switching over to innodb.
I just need to change the datatype of my column
however, I used the command
alter *table_name* modify column *column_name* *datatype* and it update the datatype but the previously saved result are in the same previous datatype . I want them to be modified too. Any help? And am I clear with my query?
It's been a while and I've been bouncing between database engines a lot lately so my syntax may be off, but the general idea will still be valid:
SELECT * INTO <table_name>_bak FROM <table_name>;
-- alter your table, leaving nulls in the column
UPDATE <table_name>
SET <column_name> = b.<column_name>
FROM <table_name> t
INNER JOIN <table_name>_bak ON <primary-key-join-clause-here>;
-- disable nulls in your modified column if needed
DROP TABLE <table_name>_bak;
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.