Change table stucture of MySQL table - mysql

Revisiting MySQL, and trying to change the structure of the table. I want to change two rows from a varchar(30) to higher.
I have googled it, and tried what appears to be the correct statement but I'm getting a syntax error:
ALTER TABLE 'compdetails' CHANGE 'compName' varchar(60) not null;
This statement however gives me a syntax error. I have also tried without the '' around the table/column names without any luck.
"Error 2064 (42000): You have an error
in your SQL Syntax...."
Can anyone jog my memory?

Just so:
ALTER TABLE 'compdetails' CHANGE compName compName varchar(60) not null;

Firstly, if you want to quote table names, then you need to use back ticks not single quotes. A single quote is only used for string literals.
Secondly, CHANGE is used when you want to rename the column, whereas MODIFY allows you to change the column definition without renaming it. So you should be using:
ALTER TABLE compdetails MODIFY compName varchar(60) NOT NULL;
The online MySQL documentation is very good and I would encourage you to check it whenever you come across problems with syntax.

ALTER TABLE `compdetails` MODIFY COLUMN `compName` varchar(60) not null;

Related

How do I add a new column to MySQL via phpMyAdmin?

Coming from years of experience with MS SQL Server I though it would be easy to add a simple nullable integer column to a MySQL database table using phpMyAdmin. I simply found the part of the UI that most resembled the part of SSMS where a new column is added to a table in SQL Server, clicked add column, entered a name, selected int, and null for default value.
The table itself is a posts table created by WordPress. When I click save I get an error saying
ALTER TABLE 'wp_posts' ADD 'acserp' INT NULL DEFAULT NULL AFTER 'comment_count';
MySQL said: Documentation
#1067 - Invalid default value for 'post_date'
I really don't see what adding an integer column has to do the the post_date column unless some row in the posts table has an invalid value for post_date and mySQL does some sort of checks to make sure that noting is wrong with the rest of the table before adding anything.
It looks like the answer is that you have to use raw SQL queries and tweak the SQL_Mode setting.
SET SQL_MODE='ALLOW_INVALID_DATES';
ALTER TABLE wp_posts ADD new_table INT AFTER comment_count
Invalid default value for 'create_date' timestamp field
You forgot to define Length/Value of the integer field. It is the third column from the left. Give value to it and your issue will be resolved. Read this for more information.

Maria DB : Alter a field to a PERSISTENT Calculated

I have created a table and I wish to make a Computed Column from the concatenated values of three other fields in the table.
I want this Computed Field to take place at INSERT or UPDATE, so I am specifying PERSISTENT
I have tried the following code (in various ways) in phpMyAdmin but always get errors, which seem to be referencing immediately after ALTER table
I did not see a way of doing this when adding the field in phpMyAdmin, so I hoped I could ALTER it.
Alter TABLE 'tlImages'
CHANGE COLUMN tlImageQuery
AS CONCAT(tlImgTitle,"~",tlImgDescrip,"~",tlImgWhereWhen) PERSISTENT;
MariaDB version 10.0.29-MariaDB-cll-lve - MariaDB Server
phpMyAdmin . Version information: 4.0.10.18
First, lose single quotes around the table name, they are not suitable for this purpose. Use backticks or nothing.
You will still get a syntax error further in the statement, because AS clause should be in brackets. Add them.
You will still get a syntax error because you are missing column type before the AS (...) clause, add it.
You will still get a syntax error because CHANGE COLUMN needs two column names, old and new, use MODIFY instead.
Alter TABLE `tlImages`
MODIFY COLUMN tlImageQuery VARCHAR(128)
AS (CONCAT(tlImgTitle,"~",tlImgDescrip,"~",tlImgWhereWhen)) PERSISTENT
;
(Type VARCHAR(128) is given just as an example).

Create SQL table with SQL keyword column

I am trying to create SQL table with SQL keyword column named "lock", but "lock" seems is a SQL keyword where the workbench thought I would want to execute the "lock" function.
Create table A(
Lock Varchar(255)
);
*Putting the "Lock" into a square bracket --> [lock] doesn't work for me.
You should put back ticks ` around the reserved word:
Create table A(
`Lock` Varchar(255)
);
You can find a good explanation about reserved words and the list of them here.
Certain objects within MySQL, including database, table, index, column, alias, view, stored procedure, partition, tablespace, and other object names are known as identifiers.
If an identifier contains special characters or is a reserved word, you must quote it whenever you refer to it.
You should try to avoid the use of this words, it may be confusing for some people to watch a code with this name. Try adding a logical name to it , like TabName_Lock.
MySQL uses the ` character to escape object names:
Create table A(
`Lock` Varchar(255)
);

How to create a temporary MySQL table with a UUID name in Coldfusion?

I'm using Coldfusion8 and MySQL 5.0.91 and am trying to create a temporary table, import some data from CSV, validate, store in database and drop the temp table.
I can do all steps in MySQL directly, where it works without a problem. However when I try to create the temp table in Coldfusion I keep getting a MySQL error.
This is what I have:
<cfset variables.tt = CreateUUID()>
<cfquery datasource="#session.datasource#">
DROP TEMPORARY TABLE IF EXISTS ##variables.tt;
CREATE TEMPORARY TABLE ##variables.tt (
tmp_kto VARCHAR(7) DEFAULT ''
, tmp_kz VARCHAR(3) DEFAULT ''
, tmp_preisliste VARCHAR(1) DEFAULT ''
, tmp_eg VARCHAR(3) DEFAULT ''
) ENGINE=MEMORY;
</cfquery>
which does not work. I also tried #variables.tt# and plain variables.tt but I keep getting the same error, while it works fine when I run the code in MySQL directly (with table name "test")
Question:
I have no clue what I'm doing wrong. Is there a max length for table names perhaps?
Thanks for some insights.
EDIT:
I get the usual error:
you have an error in your SQL syntax;
check the manual that corresponds to your MySQL server version for the right syntax to use near
'CREATE TEMPORARY TABLE #variables.tt (tmp_sa VARCHAR(3) DEFAULT ''
The most likely source of your problem is that the uuid is not suitable as a tablename. For one thing, it contains hyphens. I don't know if MySQL allows this, but I can't remember ever seeing hyphens in a table name.
We have a similar situation, but with redbrick. We want to create unique names for temporary tables. Our approach, written in a custom tag, is to use the string "temp" followed by a random number. We also have some try/catch stuff going on in case we get really unlucky with the random number.
I wrote that tag several years ago. If I were to do it again, I would probably replace the random number with the cftoken, cfid, and tickcount.
Also, you can't drop a table before you create it.
It looks to me you are missing the hash marks around your variable to output.
CREATE TEMPORARY TABLE ##variables.tt (
should be
CREATE TEMPORARY TABLE #variables.tt# (
The way you have it, MySQL is attempting to use the string #variables.tt (the doubled hash escapes the character in ColdFusion) when what you want is to create a temp table with the name of the value of variables.tt.

Must I include all column attributes in an SQL statement when I want to alter only one?

I have a MySQL database that, I would like to annotate by adding comments to all of the columns,
From what I read on the MySQL documentation, it is necessary to include the data type and all attributes when changing any single one of these.
For example, I must type
ALTER TABLE `dbname`.`tablename`
CHANGE COLUMN `columnname` `columnname`
INT(11) NULL DEFAULT NULL COMMENT 'this is my comment`;
It would be much faster for me to avoid having to restate the column info for each change, for example, by only having to submit a command such as:
ALTER TABLE `dbname`.`tablename`
CHANGE COLUMN `columnname`
COMMENT 'this is my comment`;
Are there any options for adding comments that do not require me to restate the table structure?
The documentation is pretty clear that CHANGE COLUMN requires the full column definition:
CHANGE [COLUMN] old_col_name new_col_name column_definition
Anything optional would be in brackets.
Your best bet is probably to write a little one-off script to produce your ALTER TABLE commands based on the table's current schema. You should be able to extract the column definitions from whatever data access layer you're using.