In MySQL, how do I change the column name of a table from "sum(xyz)", to say "xyz"? I have tried the following solutions for just changing the column name:
Change Column Name in MySQL
Rename column SQL Server 2008
However, it always throws up an error saying that the syntax is not right. I feel that it is because of the sum() function, because it doesn't allow me to use SELECT on that column too (when done separately).
Is there any way past this? A solution to access the values in that column without changing the column header is also appreciated!
I am using WAMPSERVER to run MySQL version 5.1.53.
Thanks
So, your column name is "sum(xyz)" and you can't do a select because of the "sum()" function.
Try
SELECT ` sum(xyz)` as xyz from mytable;
Try this:
ALTER TABLE mytable CHANGE COLUMN `sum(xyz)` `xyz` <yourdatatype>;
Use AS to make virtual field
Your code will be same as SELECT SUM(yourField) as xyz FROM table
Related
I currently have a MariaDB database with columns named after dates : 20200105, 20200914 etc.
If I try to add a column using ALTER TABLE dates ADD COLUMN IF NOT EXISTS (test VARCHAR(255));, it works and the test column is created.
If I type ALTER TABLE dates ADD COLUMN IF NOT EXISTS (20201205 VARCHAR(255));, though (so, with a number replacing "test"), the creation does not work anymore and MariaDB tells me that there is an error with my SQL syntax.
I have tried to put quotes around the column name, but that does not work (not even with "test").
Is there something obvious I am missing ?
Use backticks to escape the column name:
ALTER TABLE dates ADD COLUMN IF NOT EXISTS (`20201205` VARCHAR(255));
But really best practice frowns upon the use of naming your database objects with mandatory backticks. The reason for using a name like 20201205 as a column name is that you will forever be needing to escape it using backticks. Also, from a data design point of view, your data should grow with new dates in terms of increasing the number of records, not columns.
I have a column dateTime which consists of dates of the format "MM-DD-YYYY, hh-mm-ss" and I need to create a STORED column on the same table to get rid of the time element. I've tried:
ALTER TABLE table ADD COLUMN startOfDay AS(date(dateTime)) STORED;
but this gives a wrong syntax error. How do I make it work? I think the error is due to the AS part.
First when asking a question and you tell that you have a error, always show the error message in your post.
Secondly to use STORED columns you need MySQL 5.7 instance or higher.
At the moment I only have a 5.6 instance running so I can't test the query. But looking at the MySQL documentation I would suggest the following query syntax:
ALTER TABLE <table-name> ADD COLUMN <column-name> DATE GENERATED ALWAYS AS (DATE_FORMAT(<name-of-datetime-column>, `%Y-%m-%d`)) STORED COMMENT '<description>';
Just replace the placeholders with the names you have. To be sure and learn how things work, always check the MySQL reference manual on the subject.
See: https://dev.mysql.com/doc/refman/5.7/en/create-table-generated-columns.html
I have a database with tables and I need to work with inputs from java. For that, I need to know if the input from java can be inserted to the database by not overloading the length of a column. I've tried using SELECT COLUMNPROPERTY( OBJECT_ID('utilizador'),'U_NOME','PRECISION'); but it is giving me an error saying
SQL Error (1305):FUNCTION bd.COLUMNPROPERTY does not exist
Could some one please help me?
Once again, I'm trying to get the maximum input value that can be inserted to the column, not some that already exists. I just need to know which is the biggest size that I can insert. Per example, if I have a column named U_NOME and its a char with length 30, I want to get the 30, not the lenght of some data that already is in that column.
Thank you.
COLUMNPROPERTY is a SQL Server-specific function.
Based on your error code, it appears you're using MySQL, which doesn't implement COLUMNPROPERTY. Use the INFORMATION_SCHEMA database's COLUMN to query the length of the column in question:
SELECT CHARACTER_MAXIMUM_LENGTH
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = "u_nome"
AND TABLE_NAME = "utilizador"
AND TABLE_SCHEMA="<database_name>"
You can try DESCRIBE your_table_here. This will give the table column information.
Examples and details of other options from MySQL are:
Explain / Describe
SHOW COLUMNS
In SQL Server you can do the below
SELECT Top 1 DATALENGTH(U_NOME) FROM utilizador
In MySQl it would be
Select LENGTH(U_NOME) FROM utilizador LIMIT 1
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).
I want to create a table where I will get a column like this
c00001
c00002
c00003
c00004
and so on.
I use xampp mysql but any SQL server command would be helpful. This means I want to increment this id followed by a varchar.
You can't accomplish this in MySQL using triggers or generated columns because neither of those can access an auto-increment column value.
Your best bet may we to use a view to build the strings you want based on the auto-increment values.
Try something like this:
create or replace view v_your_table as
select *,
concat('c',lpad(id,5,'0')) as str_id
from your_table;