not null and no empty char in mysql workbench - mysql-workbench

how do for stablish a column varchar in mysql workbench as not null and not allow empty string or blank string?.
values not allowed
''
' '
NULL
Thanks

It makes more sense to do input validation on the application level rather than on the DB level.
If you really insist, you can use triggers to generate an error.
See: http://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html

Related

How do you manually insert a uuid type field in Postgres SQL DB?

I have a Postgres table with a field called user_uuid with type uuid. I also have a valid UUID value that I want to manually insert in that row, but I can't seem to find a way to manually create this.
This is an example of the statement I'm trying to execute:
insert into my_table (account_number, type, user_uuid) values ('1252', 'residential', 'dOfa6513-aOfd-4e78-9941-724b22804e9f');
I've tried appending ::UUID which I read somewhere might work, and to enclose the UUID text value inside curly brackets, instead of single quotes. None of that has worked, and the docs are not helpful either. The error I get is the following:
invalid input syntax for type uuid: 'dOfa6513-aOfd-4e78-9941-724b22804e9f'
The UUID you're trying to insert is not a valid UUID.
You can check the validity here https://www.freecodeformat.com/validate-uuid-guid.php
This is one example of a valid UUID: a8adfa00-6680-49b3-bf94-caa8c3f1d823,
can try pass this into your insert query and check if ok.
There are 2 occurrences of the letter O in your uuid.
It should have been the digit 0 instead (zero) to make it a proper hexadecimal string: d0fa6513-a0fd-4e78-9941-724b22804e9f

why is a non null postgres column with default throwing a constraint error?

In Postgres I have a not null column with default set to false. I am using a spring boot JPA/hibernate java app to write to this table. My understanding is with the default value if null is passed it should set it to the default. But I am getting the below exception. Other types like character seem to work. How do I get it to correctly use the default if null is passed?
The default value is used if no value is passed for that column, not if a null is explicitly passed.
You didn't share your Hibernate code, so I can't comment on it, but you need to change the query so it will omit the is_custom column from the insert statement.

how to understand column types from sql file?

I am not good at column types as I understand. From another country with another system they just send me a sql file and they claim that there is an image on that sql file. I guess it is byte array, however I couldnt insert it into PostgreSQL. When I try to insert it says:
LINE 1: ...ES ('00246c4e-1bc8-4dde-bb89-e9dee69990d5', '0', 0xffa0ffa40...
^
********** Error **********
ERROR: syntax error at or near "xffa0f
Could you please help me to create related table with its column properties?
I know that it is not good question, however here is starting of sql file;
INSERT INTO `fps` VALUES ('00246c4e-1bc8-4dde-bb89-e9dee69990d5', '0', 0xffa0ffa4003a0907000932d325cd000ae0f3199a010a41eff19a010b8e2......
What is the type of 0xffa0ff....?
'00246c4e-1bc8-4dde-bb89-e9dee69990d5' is a UUID.
'0' is just a character string. There are a few different string types to choose from. However, if all of these values are integers, you may want to create the column as an INTEGER instead.
0xff... is a hex string, though not in a format that Postgres will recognise. You can store this data in a bytea column, but in order for the INSERT to succeed, you will need to modify the script, replacing, for example,
0xab...ef
with
'\xab...ef'

PostgreSQL COPY CSV with two NULL strings

I have a source of csv files from a web query which contains two variations of a string that I would like to class as NULL when copying to a PostgreSQL table.
e.g.
COPY my_table FROM STDIN WITH CSV DELIMITER AS ',' NULL AS ('N/A', 'Not applicable');
I know this query will throw an error so I'm looking for a way to specify two separate NULL strings in a COPY CSV query?
I think your best bet in this case, since COPY does not support multiple NULL strings, is to set the NULL string argument to one of them, and then, once it's all loaded, do an UPDATE that will set values in any column you wish having the other NULL string you want to the actual NULL value (the exact query would depend on which columns could have those values).
If you have a bunch of columns, you could use CASE statements in your SET clause to return NULL if it matches your special string, or the value otherwise. NULLIF could also be used (that would be more compact). e.g. NULLIF(col1, 'Not applicable')

Postgres default value NULL and empty string

Usually the default value is used if the value to insert is NULL. Is there a way to tell postgres that it should use the default value also on empty string ('')?
No, you can't do so directly.
The only valid ways to specify a default value are:
To omit the column from an INSERT column-list; or
To explicitly specify the keyword DEFAULT for a column in an INSERT or UPDATE.
Neither NULL or '' are valid ways to request a default value for a column.
If you wish to replace NULL or '' with the default you must use a BEFORE ... FOR EACH ROW trigger to do so.
Making a generic one isn't especially trivial, as you can't use the DEFAULT keyword when assigning to the NEW tuple within a trigger; you'd instead have to look up the system catalogs for the default expression.
Of course, you can always just repeat the default expression in the trigger if you know it and don't mind coding a trigger specific to it.
All in all, it's way better to fix the application so it follows the standard and doesn't expect the DB to replace NULL or '' with a default value.