Strange behaviours of MySQL Workbench - mysql

In my DB I've a table defined as follow:
I've noticed 2 strange behaviours (v 6.0):
1)
Exporting this table from mysql workbench menu I get this, where I have:
sex enum('M','F') DEFAULT NULL,
from mysql Reference Manual I read:
If an ENUM column is declared to permit NULL, the NULL value is a valid value for the column, and the default value is NULL. If an ENUM column is declared NOT NULL, its default value is the first element of the list of permitted values.
and:
birth_date date DEFAULT NULL,
that I declared as NOT NULL.
2)
I've tried this query :
"insert into users(name) values('mark');"
the insert works in mysql workbench and not in sqlFiddle.
I expected some kind of error from mysql WB but I reveive just warnings:
1 row(s) affected, 5 warning(s):
1364 Field 'surname' doesn't have a default value
1364 Field 'birth_date' doesn't have a default value
1364 Field 'email' doesn't have a default value
1364 Field 'password' doesn't have a default value
1364 Field 'username' doesn't have a default value
Am I missing something or are they some kind of bug?

This is a bug, I'd say. There's no default set for the specific columns. How comes MySQL Workbench generates a default clause then? Please file a bug report at http://bugs.mysql.com to get this fixed.

Related

INSERT INTO using subquery is telling me an uninvolved field is the error?

I'm not entirely sure how to explain what's happening but basically I'm trying to insert values into my table using a subquery and it's telling me that I'm getting an error because a field that is not involved in the query at all does not have a default value.
INSERT INTO customerPayment (customerOrderId)
SELECT ID FROM customerOrder
WHERE customerOrder.orderStateId = (
SELECT ID
FROM orderState
WHERE orderState.state = "Payment Recieved"
);
ERROR 1364 (HY000): Field 'total' doesn't have a default value
And then when I go into the table itself to try and set a default value for total, it then tells me I have an invalid default value for another unrelated field.
ALTER TABLE customerPayment ALTER total SET DEFAULT 0.0;
ERROR 1067 (42000): Invalid default value for 'paymentDate'
It may be relevant to note that 'paymentDate' currently has a default value of curdate().
From what I can see, the total column was created with NOT NULL condition but wasn't assigned with any default value. Something like this example:
total DECIMAL(4,4) NOT NULL,
And it seems CURDATE() or CURRENT_DATE() can't be assigned as default value from my testing. Instead, the column datatype should be TIMESTAMP or DATETIME then can only assign with default value of CURRENT_TIMESTAMP() or NOW(). So, maybe the first step is to change the paymentDate datatype and default value like:
ALTER TABLE customerPayment MODIFY COLUMN `paymentDate` TIMESTAMP NOT NULL DEFAULT NOW();
Then you can proceed with modifying the total column like:
ALTER TABLE customerPayment MODIFY COLUMN total DECIMAL(4,4) NOT NULL DEFAULT 0.0;
Then probably you can do your INSERT after that.
Here's a fiddle: https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=4af3d11f90916469d8fbe61011b3fcab

Create table with NULL timestamp columns in MySQL 5.7

`effective_end_utc` timestamp NOT NULL COMMENT 'The UTC timestamp for when the target ceases to be in effect.',
This will end up giving me
ERROR 1067 (42000) at line 27: Invalid default value for 'effective_end_utc'
Base on other reponse, I have even set the mode to following at the begining of schema
SET GLOBAL SQL_MODE='ALLOW_INVALID_DATES'
Any idea whats going wrong?
The schema should be changed with a default value like
`reported_timestamp_utc` timestamp NULL DEFAULT NULL COMMENT

mysql table default formula

In mysql (Ubuntu 13.10, MySql 5.5) I'm trying to create a table that will automatically create a random alphanumeric ID with this code:
create table YGraph (
YGraphEdgeId CHAR(8) NOT NULL PRIMARY KEY DEFAULT SUBSTRING(MD5(RAND()) FROM 1 FOR 8),
YGraphStartVertex CHAR(6) NOT NULL,
YGraphEndVertex CHAR(6) NOT NULL
);
but phpmyadmin is complaining:
#1064 - 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 'SUBSTRING(MD5(RAND()) FROM 1 FOR 8), YGraphStartVertex CHAR(6) NOT NULL, ' at line 2
. . . and putting quotes around the formula only give a generic
#1067 - Invalid default value for 'YGraphEdgeId'
I simply want a new record creation to run the default formula and stick an 8-place random alphanum in the YGraphEdgeId field. I seem to remember this formula working in an INSERT. What am I doing wrong?
Currently you can't use a function to provide a default value for a column, see https://dev.mysql.com/doc/refman/5.5/en/data-type-defaults.html
As per MySQL Documentation on Data Type Default Values
...the default value must be a constant; it cannot be a function or an expression....
Hence you can't use functions or expressions there.
Secondly, you can assign a default string value but it should abide by the length of the column data-type used. String used to define default value was more that 8 characters. And hence the error thrown.

Getting Default value N'no' is not supported for VARCHAR(3) NULL DEFAULT 'no'

I'm using the MySQL Workbench 6.0 tool to migrating data from a MS SQL Server 2000 database to a MySQL 5.6 database and many of the create table constructs are giving the error "Default value N'no' is not supported" on columns that allow null, but define a default value. Here is some example code where the complaint is on the definition of the distribution column definition.
The Table construct looks like this:
CREATE TABLE IF NOT EXISTS `dbo`.`_tbl_access` (
`distribution` VARCHAR(3) NULL DEFAULT 'no',
`email` VARCHAR(100) NULL)
The idea is that you can store NULL, but have the default be 'no'. Is this a known problem to allow NULL, but to store a value of 'no' as default?
As stated in the MySLQ 5.6 documentation, you can't use your own default value on a column which can take NULL as a value:
If the column can take NULL as a value, the column is defined with an explicit DEFAULT NULL clause.
11.5. Data Type Default Values
If you want exactly the same behavior, you can use custom triggers. You may also consider redesigning the database, e.g. to ask yourself if a record should exist in the '_tbl_access' table if the answer for the 'distribution' field is undefined (NULL).

What is the default value for a field if no default value is provided?

I hope this isn't a dumb question. You can set a default value for all variables or a function for when it is inserted. but if the field is not required to insert and you don't allow null values, what is the "blank" value that you see in phpMyAdmin? in a query is it returned as empty string, etc?
just trying to figure it out, I want to query for all records such that the value for a specific column in that record is not "empty" or blank or whatever.
thanks.
Referring to the manual,
For data entry for a NOT NULL column that has no explicit DEFAULT
clause, if an INSERT or REPLACE statement includes no value for the
column, or an UPDATE statement sets the column to NULL, MySQL handles
the column according to the SQL mode in effect at the time:
If strict SQL mode is not enabled, MySQL sets the column to the implicit default value for the column data type.
If strict mode is enabled, an error occurs for transactional tables and the statement is rolled back. For nontransactional tables, an
error occurs, but if this happens for the second or subsequent row of
a multiple-row statement, the preceding rows will have been inserted.
So your question now may be, what are the implicit default values for the various column data types? Here you go:
Implicit defaults are defined as follows:
For numeric types, the default is 0, with the exception that for integer or floating-point types declared with the AUTO_INCREMENT
attribute, the default is the next value in the sequence.
For date and time types other than TIMESTAMP, the default is the appropriate “zero” value for the type. For the first TIMESTAMP column
in a table, the default value is the current date and time. See Section 10.3, “Date and Time Types”.
For string types other than ENUM, the default value is the empty string. For ENUM, the default is the first enumeration value.
There IS no default value unless you specify one (i.e. unless you define a "default constraint" for the column in question).
Here's an example for adding a default on an existing column:
ALTER TABLE dbo.customer ALTER COLUMN contactname SET DEFAULT 'Unknown'
Here's an example creating the table with a default:
CREATE TABLE Books (
ID SMALLINT NOT NULL,
Name VARCHAR(40) NOT NULL,
PubID SMALLINT NOT NULL DEFAULT 0
)
It's good practice to declare ALL columns "not null", and provide default constraints as appropriate.
In the "books" example above, if you "insert" without specifying PubID, the PubID will be zero.
In the same example, if you "insert" without specifying ID or Name ... you'll get an error.
If you want MySQL to auto-assign an ID, use this syntax instead:
CREATE TABLE Books (
ID SMALLINT NOT NULL AUTO_INCREMENT,
Name VARCHAR(40) NOT NULL,
PubID SMALLINT NOT NULL DEFAULT 0
)
If you want to disallow null :-
alter table YOUR_TABLE modify column COLUMN varchar(255) not null default '';
The above query will disallow null and assign an empty string when the value is not supplied.
In phpmysqladmin, blank = empty.
Via PHP mysqli function or mysql function, null value is returned as null still.
Once you have apply the query, you can easily filter that by using
select ... from YOUR_TABLE
where COLUMN != ""; <-- no need to check is null
<-- because the first query already enforce not null
However, is best for you do this before perform the alter :-
update YOUR_TABLE set COLUMN = ""
where COLUMN is null;