質問

I have been testing a database i am doing right now and i am noticing that it is letting me insert null values into fields that are part of a primary key, despite stating in the script that the value of the field should be NOT NULL. I am using MAC's MySQL Workbench, and I have been googling around and can't figure out why this is happening. (Maybe I am too brain-fried right now... I am even starting to doubt myself)

Part of the script of the database creation (these are the tables I have tested..):

DROP DATABASE IF EXISTS solytierra ;
CREATE DATABASE IF NOT EXISTS solytierra DEFAULT CHARACTER SET latin1 COLLATE       latin1_swedish_ci ;
USE solytierra ;


DROP TABLE IF EXISTS solytierra.Cliente ;
CREATE TABLE IF NOT EXISTS solytierra.Cliente (
CIF VARCHAR(25) NOT NULL,
Nombre VARCHAR(100) NULL,
EmailGeneral VARCHAR(45) NULL,
Web VARCHAR(45) NULL,
Notas VARCHAR(150) NULL,
insertado Timestamp,
CONSTRAINT pk_Cliente PRIMARY KEY (CIF)
)  ENGINE=InnoDB;


DROP TABLE IF EXISTS solytierra.PersonaContacto ;
CREATE TABLE IF NOT EXISTS solytierra.PersonaContacto (
Cliente_CIF VARCHAR(25) NOT NULL,
Nombre VARCHAR(50) NOT NULL,
Apellidos VARCHAR(100) NOT NULL,
Notas VARCHAR(150) NULL,
CONSTRAINT pk_PersonaContacto PRIMARY KEY (Cliente_CIF , Nombre , Apellidos),
CONSTRAINT fk_PersonaContacto_Cliente FOREIGN KEY (Cliente_CIF)
    REFERENCES solytierra.Cliente (CIF)
    ON DELETE NO ACTION ON UPDATE NO ACTION
)  ENGINE=InnoDB;

...

It will let me create Clients without CIF, "PersonaContacto" without Cliente_CIF or without "Nombre"....

I have also tested other databases that i already had that used to work and it is happening the same in an all them.

役に立ちましたか?

解決

Got it!!

I don't know what sql mode i was running on by default, but with this:

SET sql_mode = TRADITIONAL;

It is now running perfectly! I didn't know that there were different sql modes! Thanks a lot to everyone for your time and efforts! It really helped me to see that the problem was in my workbench, not the code and look for the answer accordingly! I hope this thread will be useful for future beginners like me!

他のヒント

If the value being stored in the column CIF is actually a NULL, then the expression LENGTH(CIF) should also return NULL. (If it's a zero length string, then LENGTH(CIF) will return 0.

To verify:

SELECT c.CIF, LENGTH(c.CIF) FROM solytierra.Cliente c ;

SELECT c.CIF FROM solytierra.Cliente c WHERE c.CIF IS NULL;

If you are running an INSERT statement, I can't explain the behavior you are observing, either MySQL allowing a NULL value to be stored or MySQL providing an implicit default value.)

If it's a zero length string being stored, that's the behavior we would expect if the columns were not explicitly declared to be NOT NULL but were later declared to part of the primary key. It's also the behavior we'd expect if the column were defined NOT NULL DEFAULT ''.

When the NOT NULL is omitted from the column declaration and the column is later declared to be part of the PRIMARY KEY, MySQL will use an an implicit default value based on the datatype of the column (zero length string for VARCHAR, zero for an integer, etc.)

But I'm not able to reproduce the problem you report, with the table definitions you've posted.

I recommend you check the table definition by getting the output from:

SHOW CREATE TABLE solytierra.Cliente;
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top