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.
Related
I have a MySQL database for production environment. Recently we added a new column to a table. So in this table, some rows have NULL value for this column. Now we want all the rows to have a non-null value so we have to backfill the data.
What is the common approach to do backfilling? If I want to write a SQL script to do the backfilling, is there a way to abort the whole operation if any error happens?
Thank you!
Just update the table with non-null value.
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 just wanted to know if somebody could explain this.
I was just testing my code and didn't check for empty input fields (I know I have to, but just testing).. In my database table, all the fields are NOT NULL, and I was expecting a exception because I wasn't inserting anything.. But it turns out that MySQL inserts all with blank values, also, from MySQL workbench is the same thing..
Is there a way to prevent this? (From a MySQL perspective)
This behavior, although atypical, is quite well documented:
Inserting NULL into a column that has been declared NOT NULL. For
multiple-row INSERT statements or INSERT INTO ... SELECT statements,
the column is set to the implicit default value for the column data
type. This is 0 for numeric types, the empty string ('') for string
types, and the “zero” value for date and time types. INSERT INTO ...
SELECT statements are handled the same way as multiple-row inserts
because the server does not examine the result set from the SELECT to
see whether it returns a single row. (For a single-row INSERT, no
warning occurs when NULL is inserted into a NOT NULL column. Instead,
the statement fails with an error.)
So, if you want to get an error, use VALUES() with a single row. Alternatively, define a trigger that does the check.
Why does MySQL work this way? I don't know, to differentiate itself from other databases and prevent ANSI-compatibility? More seriously, I assume that this a question of efficiency, and related to the fact that MySQL does not implement check constraints. The NOT NULL declaration is just an example of a check constraint, and these are not supported.
I am using MySql for my database. I have found how to set a column's starting autoincrement value when creating a table, but I need to know how to set a new starting value for an existing column. What does the liquibase script look like to do that?
The MySQL syntax is pretty straightforward:
ALTER TABLE mytable AUTO_INCREMENT = val ;
(Note that this is really a table attribute, not a column attribute. There can be only one column in a table declared to be AUTO_INCREMENT.)
This syntax isn't supported in SQL Server or Oracle; Oracle doesn't even have a concept of an "auto_increment" column, apart from a SEQUENCE object and a TRIGGER. SQL Server calls it an IDENTITY property. So I don't know how this statement would be represented in "liquibase" syntax, other than specifying that this statement is native MySQL syntax.
You can use addAutoIncrement (http://www.liquibase.org/documentation/changes/add_auto_increment.html) to change your existing AUTO_INCREMENT column.
Don't forget to specify columnDataType in the addAutoIncrement.
I used this yesterday for our project and it worked (for MySQL).