How to get the lenght property of a column in sql? - mysql

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

Related

Adding a calculated date column to a MySQL dataset

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

What exactly does SELECT database(); mean?

So I'm really new to learning SQL and I'm interested about the command
SELECT DATABASE();
At my point in learning, I know that command is used to show what database I'm currently working with. I accidentally made the typo,
SELECT DATABASE() L; and the MySQL terminal displayed the letter L above the name of the database I was working with. Why does this happen? And what exactly does SELECT DATABASE() mean?
I tried this again and did the command
SELECT DATABASE() HI; and it did the same thing and showed "HI" above the name of my selected database. Using the correct syntax, it normally shows, "DATABASE()" above the db that I'm currently working with.
database() is an information function provided by MySQL to return the name of the current database. It can be used anywhere a string constant would be expected.
MySQL does not require a from clause, so a select is a complete query that returns one row -- the values calculated in the select.
Databases allow you to assign a name ("column alias") to an expression in the select. Normally, this would be written as:
select database() as database_name
However, the as is optional (although I strongly recommend using it for column aliases). And nothing enforces a reasonable name.
So:
select database() l
returns a result set with one row and one column. The column is called l and the value in the one row is the database name.
You are selecting a dataset with one column. The contents of the column is the current database. You are naming that column L.
Compare with
select 4 label;
I am not expert in database. But I am quite sure that show database(); will show you the selected database in other words the database which you have chosen.

changing column name in mysql from sum(xyz) to xyz

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

Is it possible to give condition in MYSQL database column?

I am using MYSQL database for my application. Is it possible to give condition in each columns to accept values within the range.
For example, Lets say I am having column 'age' of integer type in a table. During insert statement this column should accept value only from 0-100. i.e it should not accept negative values or greater than 100. If so, it should return me an error.
How could I setup this condition in MYSQL database?
Is there any alternative option to do it?
Thanks in advance.
MySQL doesn't support CHECKs, you must do it in your application or use a Trigger or Stored Procedure to check for wrong data:
The CHECK clause is parsed but ignored by all storage engines.

SHOW TABLES and MYSQL_NUM_ROWS

I have just noticed that where mysql_num_rows should return the number of rows returned for either SELECT or SHOW commands, returns 0 for SHOW TABLE command specifically.
Instead it shows the affected rows count instead of the num rows.
Can anyone please tell me if this is a bug or if am I missing anything here?
SHOW TABLE command is used to Show you table name in your database . On the other hand , mysql_num_rows is used to count how many result got from your query. This query is depend on your requirement basis ...
As stated on the PHP documentation page:
Retrieves the number of rows from a result set. This command is only
valid for statements like SELECT or SHOW that return an actual result
set.
My guess is that SHOW TABLES is not a technical query that would produce the type of result set that mysql_num_rows enumerates.
These "helper" functions (such as SHOW, EXPLAIN, DESCRIBE etc.) won't let you issue their results like you would in a regular table.
But if you're looking for how you can do this, for SHOW TABLES you can do
SELECT `table_name` FROM `information_schema`.`tables`
WHERE `table_schema`=DATABASE()
-- DATABASE() selects current database name
-- you can use the name of any database as a string instead
So basically you can use the information_schema database to get that information.
It was a bug in mysql, fixed it with the update.