|
|
|
|
![]() ![]() |
Aug 31 2007, 04:23 AM
Post
#1
|
|
|
Premium Member Group: [HOSTED] Posts: 410 Joined: 16-February 06 From: Kolkata, India Member No.: 11,322 myCENTs:82.69 |
I have created a test table with a single column - Name using the SQL query given below.
CODE CREATE TABLE Test ( Name CHAR(10) NOT NULL ) A NOT NULL Constraint has been added to the field, yet when I run either of the queries, the operation does not fail. CODE INSERT INTO test VALUES(); CODE INSERT INTO test VALUES(""); Another example of a constraint not working under MySQL could be the FOREIGN KEY. CODE CREATE TABLE parent ( id INT PRIMARY KEY ); CREATE TABLE CHILD ( id INT, FOREIGN KEY (id) REFERENCES parent(id) ); insert into parent values (1); insert into child values (2); The last line of the query should result in an error, because the value 2 does not exist under the parent table. Am I missing out on something or is it just that MySQL has show-off constraints just for the sake of being SQL. |
|
|
|
Dec 31 2007, 07:30 PM
Post
#2
|
|
|
Member [ Level 1 ] Group: Members Posts: 30 Joined: 31-December 07 Member No.: 27,267 |
Hi turbopowerdmaxsteel,
A bit of a late reply, but I noticed no one made a response to your post. I do hope this is still relevant, but welcome to MySQL. Coming from an Oracle background, I know how you feel. I'm sure that (by now) you have already figured out why MySQL's behavior is as such, but for the benefit of others who may find this useful, here is an explanation. MySQL NULLs and empty values are different. When inserting new values, MySQL will use the default value based on your column type. In this case for CHARs, it will be an empty value. Had you provided a different default (on the table schema), it will be used instead of an empty value in your "INSERT INTO test VALUES();" statement. Given this, we see that there really is no issue in the "not null" constraint. Try inserting a NULL instead. As for Foreign Keys, it is available for all storage engines as of MySQL 5.2. For lower versions, only select engines such as InnoDB supports it. The storage engine can be chosen upon creating a table. Cheers. |
|
|
|
Dec 31 2007, 08:04 PM
Post
#3
|
|
|
Premium Member Group: [HOSTED] Posts: 410 Joined: 16-February 06 From: Kolkata, India Member No.: 11,322 myCENTs:82.69 |
Thanks for the information, altimit and welcome to Astahost. Infact I had assumed that this was the desired behavior of MySQL (Constraints being only phony). I thougth it was similar to variables with no types as we have in Javascript, PHP, etc for performance reasons. I had entrusted the validation part to Javascripts and PHP. But, its always better to have them at the lowest level.
I have only done a rapid e-learning on MySQL from random pages on the Net. Good ol' google mostly helps out when I am stuck. All these engine stuff where like hocus-focus, so I skipped them altogether. My localhost is running on MySQL 5.0.33, so I tried using the InnoDB engine and ran the following query:- CODE CREATE TABLE parent ( id INT PRIMARY KEY )Engine=InnoDB; CREATE TABLE CHILD ( id INT, FOREIGN KEY (id) REFERENCES parent(id) )Engine=InnoDB; INSERT INTO parent VALUES (1); INSERT INTO child VALUES (2); The query, however, executed successfully even though its still breaking the foreign key constraint. Am I still doing something wrong? |
|
|
|
Jan 1 2008, 11:51 AM
Post
#4
|
|
|
Member [ Level 1 ] Group: Members Posts: 30 Joined: 31-December 07 Member No.: 27,267 |
Thanks very much.
Yes this is quite a common scenario, but would boil down to the fact that MySQL treats NULLs as actual "values" (to put it very bluntly). I would suggest you add a "NOT NULL" clause to the Id's you are trying to associate (i.e., "id INT NOT NULL"); perhaps a good way of thinking on why the current scenario does not raise an exception is that the Child Id has successfully been matched against a NULL Parent Id. Did it do the trick? This post has been edited by altimit: Jan 1 2008, 11:53 AM |
|
|
|
Jan 1 2008, 12:02 PM
Post
#5
|
|
|
Premium Member Group: [HOSTED] Posts: 410 Joined: 16-February 06 From: Kolkata, India Member No.: 11,322 myCENTs:82.69 |
I tried that using the following code:-
CODE CREATE TABLE parent ( id INT PRIMARY KEY NOT NULL )Engine=InnoDB; CREATE TABLE CHILD ( id INT NOT NULL, FOREIGN KEY (id) REFERENCES parent(id) )Engine=InnoDB; INSERT INTO parent VALUES (1); INSERT INTO child VALUES (2); But, it got executed nice and well too. |
|
|
|
Jan 1 2008, 12:20 PM
Post
#6
|
|
|
Member [ Level 1 ] Group: Members Posts: 30 Joined: 31-December 07 Member No.: 27,267 |
Thanks for the quick response;
It would seem that the Child ID is not currently being indexed, would that be correct? Kindly try the following set of statements: CODE CREATE TABLE parent ( id INT NOT NULL, PRIMARY KEY (id) ) ENGINE = INNODB; CREATE TABLE child ( parent_id INT NOT NULL, INDEX (parent_id), FOREIGN KEY (parent_id) REFERENCES parent(id) ) ENGINE = INNODB; INSERT INTO parent VALUES (1); INSERT INTO child VALUES (2); My installation flags the final statement with an error "#1216 - Cannot add or update a child row: a foreign key constraint fails". How about yours? |
|
|
|
Jan 1 2008, 12:44 PM
Post
#7
|
|
|
Premium Member Group: [HOSTED] Posts: 410 Joined: 16-February 06 From: Kolkata, India Member No.: 11,322 myCENTs:82.69 |
Amazingly it runs without any error for me. What could the issue be? My MySQL Version is 5.0.33 and I tried running the query on phpMyAdmin - 2.9.2.
|
|
|
|
Jan 1 2008, 12:51 PM
Post
#8
|
|
|
Member [ Level 1 ] Group: Members Posts: 30 Joined: 31-December 07 Member No.: 27,267 |
I must admit, that is quite strange.
Using phpMyAdmin, can you please verify that the engine being used is indeed InnoDB? Let us hope that there is no server configuration blocking the engine from being used, and automatically substituting MyISAM instead. I have done my testing on a live server with MySQL version 4.1.22. phpMyAdmin's version is 2.11.0, but the does not matter as I used SSH and connected via MySQL's console. Please also remember that InnoDB is transactional; you might want to create the tables first, before adding any data (in a separate phpMyAdmin command "instance"). This post has been edited by altimit: Jan 1 2008, 12:56 PM |
|
|
|
Jan 1 2008, 01:08 PM
Post
#9
|
|
|
Premium Member Group: [HOSTED] Posts: 410 Joined: 16-February 06 From: Kolkata, India Member No.: 11,322 myCENTs:82.69 |
I did try creating the Tables first, but it didn't work. You have hit the correct cause, though. The engine gets reset to MyISAM. When I tried to create a table with phpMyAdmin's interface, I don't see INNODB in the list. These are the storage engines available: MyISAM, Memory, Archive, Mrg_MyISAM.
I wonder why? |
|
|
|
Jan 1 2008, 01:15 PM
Post
#10
|
|
|
Member [ Level 1 ] Group: Members Posts: 30 Joined: 31-December 07 Member No.: 27,267 |
Hi,
I have a hunch that you are using a Windows Essentials MySQL installation, instead of the standard Windows one. Would this be correct? Also, kindly try issuing an "SHOW ENGINES;" statement. What is the output? Thanks. |
|
|
|
![]() ![]() |
Similar Topics
|
Lo-Fi Version | Time is now: 2nd December 2008 - 01:35 PM |