Related
I'm starting to learn MySQL with an online course, and my teacher says it's good practice to first create all tables, then alter the tables and insert primary/foreign keys.
I'm doing an exercise that way, even though the teacher himself has provided the answer where he wrote the primary key inside the tables, and altered only foreign keys.
The problem I'm having is when inserting data, an error shows "column X cannot be null". That's my first column, "idcliente", which I altered to be a primary key with auto_increment, and now I assume it's not incrementing numbers automatically when I insert data. I have checked online and seen about identity (haven't learned yet, so it's not ideal for me), that values cannot be null (I've inserted data before this way, when primary key is inside the table code, and it works). I'm not sure what am I doing wrong. Appreciate any answers. Thank you!
CREATE TABLE CLIENTE (
IDCLIENTE INT,
NOME VARCHAR(30) NOT NULL,
SEXO ENUM('M','F') NOT NULL
);
CREATE TABLE TELEFONE (
IDTELEFONE INT,
TIPO ENUM('RES','COM','CEL'),
NUMERO VARCHAR(11),
ID_CLIENTE INT
);
ALTER TABLE CLIENTE
ADD CONSTRAINT PK_CLIENTE
PRIMARY KEY AUTO_INCREMENT (IDCLIENTE);
ALTER TABLE TELEFONE
ADD CONSTRAINT PK_TELEFONE
PRIMARY KEY AUTO_INCREMENT (IDTELEFONE);
ALTER TABLE TELEFONE
ADD CONSTRAINT FK_CLIENTE_TELEFONE
FOREIGN KEY (ID_CLIENTE)
REFERENCES CLIENTE(IDCLIENTE);
INSERT INTO CLIENTE VALUES (NULL,'JOAO','M');
"ERROR 1048 (23000): Column 'IDCLIENTE' cannot be null"
I tested your code and then I took a look at the table definition:
mysql> show create table CLIENTE\G
*************************** 1. row ***************************
Table: CLIENTE
Create Table: CREATE TABLE `CLIENTE` (
`IDCLIENTE` int(11) NOT NULL,
`NOME` varchar(30) NOT NULL,
`SEXO` enum('M','F') NOT NULL,
PRIMARY KEY (`IDCLIENTE`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
You did not define the PK as AUTO_INCREMENT.
This is the problem:
ALTER TABLE CLIENTE
ADD CONSTRAINT PK_CLIENTE
PRIMARY KEY AUTO_INCREMENT (IDCLIENTE);
This is not the correct syntax to define an AUTO_INCREMENT column. It might be the syntax to define an optional index type, according to the syntax documented in https://dev.mysql.com/doc/refman/5.7/en/alter-table.html. But the index type seems to be ignored by InnoDB. Whatever you type in between PRIMARY KEY and the column list is ignored.
The syntax for the AUTO_INCREMENT option goes with the column, not with the constraint.
mysql> ALTER TABLE CLIENTE
MODIFY COLUMN IDCLIENTE INT AUTO_INCREMENT,
ADD PRIMARY KEY (IDCLIENTE);
INSERT INTO CLIENTE VALUES (NULL,'JOAO','M');
Don't supply the NULL:
INSERT INTO CLIENTE (NOME, SEXO) VALUES ('JOAO','M');
Since your column IDCLIENTE is set to Auto Increment, you do not need to supply it when doing an insert. MySQL would automatically insert it for you.
Use insert statement as
INSERT INTO CLIENTE VALUES ('JOAO','M');
Auto Increment will create a unique number automatically while trying to insert a record in the table.
So Don't Use the NULL inside the query, try this:
INSERT INTO CLIENTE (NOME,SEXO) VALUES ('JOAO','M');
Use this query: SHOW COLUMNS FROM TABLE-NAME FROM DATABASE-NAME;
which will describe you the detailed information about your table structure.
I have three tables:
CREATE TABLE Address (
ResidentID CHAR(5) NOT NULL,
Location varchar(255) NOT NULL,
KEY ResidentID(ResidentID)
);
CREATE TABLE Customer (
CustomerID CHAR(5) NOT NULL,
ContactName varchar(40) NOT NULL,
PRIMARY KEY (CustomerID)
);
CREATE TABLE Supplier (
SupplierID CHAR(5) NOT NULL,
SupplierName varchar(40) NOT NULL,
PRIMARY KEY (SupplierID)
);
I want to store CustomerID and SupplierID in the Address.ResidentID field with using of foreign keys:
ALTER TABLE Address ADD CONSTRAINT fk_CustomerID1 FOREIGN KEY(ResidentID) REFERENCES Customer(CustomerID);
ALTER TABLE Address ADD CONSTRAINT fk_SupplierID1 FOREIGN KEY(ResidentID) REFERENCES Supplier(SupplierID);
But second 'ALTER TABLE' raises Error: relation already exists
Any suggestions?
Data example:
CustomerID ContactName
C0001 Den
SupplierID ContactName
S0001 John
So Address table should contains:
ResidentID Location
C0001 Alaska
S0001 Nevada
You need to either reference addresses from the Customer / Supplier (if they only have one) or two different columns.
The reason you see in this SQLFiddle You cannot INSERT the required columns into the Address table if the ResidentID references BOTH tables. You could only insert lines that would match the contents of Customer AND Supplier but you want an OR connection that you can't create that way.
(Note: In my solutions I assume addresses to be optional. As Tom pointed out in the comments that may not be what you wanted, or expected. Make sure to mark the FK Columns in the first solution as NOT NULL if you want addresses to be mandatory, its more complicated for the second one. You have to mind the correct insertion order then.)
Either:
CREATE TABLE Address (
AddressID CHAR(5) NOT NULL,
Location varchar(255) NOT NULL,
PRIMARY KEY (AddressID)
);
CREATE TABLE Customer (
CustomerID CHAR(5) NOT NULL,
AddressID CHAR(5),
ContactName varchar(40) NOT NULL,
PRIMARY KEY (CustomerID)
);
CREATE TABLE Supplier (
SupplierID CHAR(5) NOT NULL,
AddressID CHAR(5),
SupplierName varchar(40) NOT NULL,
PRIMARY KEY (SupplierID)
);
ALTER TABLE Customer ADD CONSTRAINT fk_AddressID_Cust FOREIGN KEY(AddressID) REFERENCES Address(AddressID);
ALTER TABLE Supplier ADD CONSTRAINT fk_AddressID_Supp FOREIGN KEY(AddressID) REFERENCES Address(AddressID);
or
CREATE TABLE Address (
CustomerID CHAR(5),
SupplierID CHAR(5),
Location varchar(255) NOT NULL,
PRIMARY KEY (CustomerID, SupplierID)
);
CREATE TABLE Customer (
CustomerID CHAR(5) NOT NULL,
ContactName varchar(40) NOT NULL,
PRIMARY KEY (CustomerID)
);
CREATE TABLE Supplier (
SupplierID CHAR(5) NOT NULL,
SupplierName varchar(40) NOT NULL,
PRIMARY KEY (SupplierID)
);
ALTER TABLE Address ADD CONSTRAINT fk_CustomerID1 FOREIGN KEY(CustomerID) REFERENCES Customer(CustomerID);
ALTER TABLE Address ADD CONSTRAINT fk_SupplierID1 FOREIGN KEY(SupplierId) REFERENCES Supplier(SupplierID);
The approach you're trying is (a) not possible and (b) undesirable even if it was possible.
The best approach is to have a CustomerAddress table and a SupplierAddress table, each with a single FK to the matching base table; or if you must, a cross-reference table with appropriate constraints.
If your motivation for having a single Address table was code reuse, you can still do that ... think in terms of a template xxxAddress table design that can refer to any base xxx table. You can write non-database code that treats the base table name as a parameter and then could handle any number of xxxAddress tables as you add more base tables over time.
Or if your motivation for having a single Address table was to simplify reporting, you can always create a view or stored proc that returns a union of all such tables + an added field to indicate the base table for each address row.
Angelo I am revising this a bit based on your comment ---
Angelo, I ran your sample code in a local MySQL instance (not SQLFiddle) and observed an error.
I was surprised (you learn something every day) that MySQL did allow two foreign key constraints to be defined on the same field; however when you attempt to insert data, when trying to point the FK to the Customer table, I get an error saying a foreign key constraint fails referencing the Supplier table; and vice versa for the insert trying to point the FK to the Supplier table.
So my revised statement is (a) it is possible to create the hydra-headed FK in at least some DBMSs -- verified in MySQL, MS SQL Server and Oracle -- although (b) this only makes sense to use when the foreign key can refer to the same logical entity by ID across multiple tables (e.g. to ensure there is a corresponding record in all required tables, for example); and (c) if used to refer to multiple tables where the primary key is NOT the same logical entity, only works if by chance the same primary key value just happens to exist in all referenced tables, which is likely to lead to subtle, hard-to-find errors.
In other words, your example would work when attempting to insert a record referring to Customer ID=3 only if there was also a Supplier ID=3, which are really logically unrelated.
So my slightly revised answer to the OP is, what you're trying to do is not possible (or logical) when the foreign key is referring to different ENTITIES, as in the OP example of Customers and Suppliers.
I have the following table that will store a file (image, PDF, etc.)
CREATE TABLE `tbl_file` (
`id` INT(10),
`size` FLOAT,
`name` VARCHAR(45),
`type` VARCHAR(16),
`content` BLOB,
`date_time` TIMESTAMP,
PRIMARY KEY (`id`)
)
There are many tables that will have files (tbl_order & tbl_payment) and I want to use only one table to store files (tbl_file). However, some tables may have multiple files for each row.
For example, there are multiple files for one order (invoice, PO, BL, contract) and there may be multiple files for each payment, etc.
So I made the following junction tables for each table that may have multiple files ( I didn't include the foreign key code).
CREATE TABLE `tbl_order_file` (
`order_id` INT(10),
`file_id` INT(10),
PRIMARY KEY (`order_id`, `file_id`),
)
and
CREATE TABLE `tbl_payment_file` (
`payment_id` INT(10),
`file_id` INT(10),
PRIMARY KEY (`payment_id`, `file_id`),
)
the problem here, is that one file can be related to both tables. Maybe it's an advantage rather than a problem but I would like to know if there is a better way to do this or to find a way to restrict the file so it's only referenced in one table.
I am using MySQL with innodb engine.
One way to declare constraint to enforce this for you is to make sure each row in tbl_file is marked as the type of file, either 'O' or 'P' (or other types in the future).
CREATE TABLE `tbl_file` (
`id` INT(10),
`file_type` CHAR(1) NOT NULL, -- must be 'O' or 'P'
`size` FLOAT,
`name` VARCHAR(45),
`type` VARCHAR(16),
`content` BLOB,
`date_time` TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY (`id`, `file_type`)
);
Then each subordinate table forces all its rows to be marked with the respective type. Therefore a row in tbl_order_file can reference only rows in tbl_file that have the same file_type:
CREATE TABLE `tbl_order_file` (
`order_id` INT(10),
`file_id` INT(10),
`file_type` CHAR(1) NOT NULL, -- must be 'O'
PRIMARY KEY (`order_id`, `file_id`),
FOREIGN KEY (`file_id`, `file_type`) REFERENCES `tbl_file` (`id`, `file_type`)
);
And likewise for tbl_payment_file:
CREATE TABLE `tbl_payment_file` (
`payment_id` INT(10),
`file_id` INT(10),
`file_type` CHAR(1) NOT NULL, -- must be 'P'
PRIMARY KEY (`payment_id`, `file_id`),
FOREIGN KEY (`file_id`, `file_type`) REFERENCES `tbl_file` (`id`, `file_type`)
);
One difficulty in MySQL particularly is that MySQL doesn't support CHECK constraints to allow the table definitions to restrict the value of file_type. You'd have to do it in a trigger or with application code.
Re your comment:
Let's say I want to go with my solution without having file_type, what would be an issue in the future?
Well, as you already stated in your original question, without some constraint, there's nothing preventing one row in the file table from being referenced by multiple junction tables, and you could end up with data that is an anomaly for your application.
On the other hand, you might want to allow that flexibility, if a given file pertains to both an order and a payment. If you had no ability to maintain the multiple references, you'd then have to make duplicate copies of such files to allow them to be in multiple categories.
One more issue: none of these solutions prevents your app from inserting a row in the file table that is childless; i.e. a file that has no references from any junction table.
First of all: if you have many-to-one relations between tables, then don't create a primary key on order_id.
I would put all files into one table (tbl_file) and add two foreign key fields to the table: order_id and payment_id, which would reference to the respective entry in the payment or order table.
So an entries (in case of one payment having more then one file) would look like:
id payment_id order_id size name type content date_time
1 1 null 434 File.txt text/plain <blob> 24.11.2012
2 1 null 131 File2.txt text/plain <blob> 24.11.2012
I have 2 tables, customers and affiliates. I need to make sure that customers.email and affiliates.email are exclusive. In other words, a person cannot be both a customer and an affiliate. It's basically the opposite of a foreign key. Is there a way to do this?
You can use a table that stores emails and have unique constrain on the email, and reference that table from the customer and affiliate. (still need to ensure that there are no 2 records referencing the same key)
You can use trigger before insert and before update to check if the email is not present.
Or you can leave this validation to the application logic - not in the database, but in the applicationc ode.
There is no key you can do this with, but it sounds like you shouldn't be using two tables. Instead, you can have one table with either customer/affiliate data (that needs to be unique in this table) and another table that has the type (customer/affiliate).
CREATE TABLE People (
pplid,
pplEmail,
ptid,
UNIQUE KEY (pplEmail)
)
CREATE TABLE PeopleType (
ptid,
ptType
)
INSERT INTO PeopleType VALUES (1, 'affiliates'), (2, 'customers');
You can try the following.
Create a new table, which will be a master for customers and affiliates:
CREATE TABLE party
(
id int not null auto_increment primary key ,
party_type enum('customer','affiliate') not null,
email varchar(100),
UNIQUE (id,party_type)
);
--Then
CREATE TABLE customer
(
....
party_id INT NOT NULL,
party_type enum('customer') NOT NULL DEFAULT 'customer',
PRIMARY KEY (party_id,party_type)
FOREIGN KEY (party_id,party_type) REFERENCES party(id,party_type)
);
CREATE TABLE affiliates
(
....
party_id INT NOT NULL,
party_type enum('affiliate') NOT NULL DEFAULT 'affiliate',
PRIMARY KEY (party_id,party_type)
FOREIGN KEY (party_id,party_type) REFERENCES party(id,party_type)
)
-- enum is used because mysql still doesn't have CHECK constraints
This way each party can be only of one type
I was working on creating some tables in database foo, but every time I end up with errno 150 regarding the foreign key. Firstly, here's my code for creating tables:
CREATE TABLE Clients
(
client_id CHAR(10) NOT NULL ,
client_name CHAR(50) NOT NULL ,
provisional_license_num CHAR(50) NOT NULL ,
client_address CHAR(50) NULL ,
client_city CHAR(50) NULL ,
client_county CHAR(50) NULL ,
client_zip CHAR(10) NULL ,
client_phone INT NULL ,
client_email CHAR(255) NULL ,
client_dob DATETIME NULL ,
test_attempts INT NULL
);
CREATE TABLE Applications
(
application_id CHAR(10) NOT NULL ,
office_id INT NOT NULL ,
client_id CHAR(10) NOT NULL ,
instructor_id CHAR(10) NOT NULL ,
car_id CHAR(10) NOT NULL ,
application_date DATETIME NULL
);
CREATE TABLE Instructors
(
instructor_id CHAR(10) NOT NULL ,
office_id INT NOT NULL ,
instructor_name CHAR(50) NOT NULL ,
instructor_address CHAR(50) NULL ,
instructor_city CHAR(50) NULL ,
instructor_county CHAR(50) NULL ,
instructor_zip CHAR(10) NULL ,
instructor_phone INT NULL ,
instructor_email CHAR(255) NULL ,
instructor_dob DATETIME NULL ,
lessons_given INT NULL
);
CREATE TABLE Cars
(
car_id CHAR(10) NOT NULL ,
office_id INT NOT NULL ,
engine_serial_num CHAR(10) NULL ,
registration_num CHAR(10) NULL ,
car_make CHAR(50) NULL ,
car_model CHAR(50) NULL
);
CREATE TABLE Offices
(
office_id INT NOT NULL ,
office_address CHAR(50) NULL ,
office_city CHAR(50) NULL ,
office_County CHAR(50) NULL ,
office_zip CHAR(10) NULL ,
office_phone INT NULL ,
office_email CHAR(255) NULL
);
CREATE TABLE Lessons
(
lesson_num INT NOT NULL ,
client_id CHAR(10) NOT NULL ,
date DATETIME NOT NULL ,
time DATETIME NOT NULL ,
milegage_used DECIMAL(5, 2) NULL ,
progress CHAR(50) NULL
);
CREATE TABLE DrivingTests
(
test_num INT NOT NULL ,
client_id CHAR(10) NOT NULL ,
test_date DATETIME NOT NULL ,
seat_num INT NOT NULL ,
score INT NULL ,
test_notes CHAR(255) NULL
);
ALTER TABLE Clients ADD PRIMARY KEY (client_id);
ALTER TABLE Applications ADD PRIMARY KEY (application_id);
ALTER TABLE Instructors ADD PRIMARY KEY (instructor_id);
ALTER TABLE Offices ADD PRIMARY KEY (office_id);
ALTER TABLE Lessons ADD PRIMARY KEY (lesson_num);
ALTER TABLE DrivingTests ADD PRIMARY KEY (test_num);
ALTER TABLE Applications ADD CONSTRAINT FK_Applications_Offices FOREIGN KEY (office_id) REFERENCES Offices (office_id);
ALTER TABLE Applications ADD CONSTRAINT FK_Applications_Clients FOREIGN KEY (client_id) REFERENCES Clients (client_id);
ALTER TABLE Applications ADD CONSTRAINT FK_Applications_Instructors FOREIGN KEY (instructor_id) REFERENCES Instructors (instructor_id);
ALTER TABLE Applications ADD CONSTRAINT FK_Applications_Cars FOREIGN KEY (car_id) REFERENCES Cars (car_id);
ALTER TABLE Lessons ADD CONSTRAINT FK_Lessons_Clients FOREIGN KEY (client_id) REFERENCES Clients (client_id);
ALTER TABLE Cars ADD CONSTRAINT FK_Cars_Offices FOREIGN KEY (office_id) REFERENCES Offices (office_id);
ALTER TABLE Clients ADD CONSTRAINT FK_DrivingTests_Clients FOREIGN KEY (client_id) REFERENCES Clients (client_id);
These are the errors that I get:
mysql> ALTER TABLE Applications ADD CONSTRAINT FK_Applications_Cars FOREIGN KEY
(car_id) REFERENCES Cars (car_id);
ERROR 1005 (HY000): Can't create table 'foo.#sql-12c_4' (errno: 150)
I ran SHOW ENGINE INNODB STATUS which gives a more detailed error description:
------------------------
LATEST FOREIGN KEY ERROR
------------------------
100509 20:59:49 Error in foreign key constraint of table foo/#sql-12c_4:
FOREIGN KEY (car_id) REFERENCES Cars (car_id):
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
See http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html
for correct foreign key definition.
------------
I searched around on StackOverflow and elsewhere online - came across a helpful blog post here with pointers on how to resolve this error - but I can't figure out what's going wrong. Any help would be appreciated!
You should make car_id a primary key in cars.
Note: I had the same problem, and it was because the referenced field was in a different collation in the 2 different tables (they had exact same type).
Make sure all your referenced fields have the same type AND the same collation!
Check that BOTH tables have the same ENGINE. For example if you have:
CREATE Table FOO ();
and:
CREATE Table BAR () ENGINE=INNODB;
If you try to create a constraint from table BAR to table FOO, it will not work on certain MySQL versions.
Fix the issue by following:
CREATE Table FOO () ENGINE=INNODB;
Subtle, but this error got me because I forgot to declare a smallint column as unsigned to match the referenced, existing table which was "smallint unsigned." Having one unsigned and one not unsigned caused MySQL to prevent the foreign key from being created on the new table.
id smallint(3) not null
does not match, for the sake of foreign keys,
id smallint(3) unsigned not null
I got this completely worthless and uninformative error when I tried to:
ALTER TABLE `comments` ADD CONSTRAINT FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE;
My problem was in my comments table, user_id was defined as:
`user_id` int(10) unsigned NOT NULL
So... in my case, the problem was with the conflict between NOT NULL, and ON DELETE SET NULL.
Also both the tables need to have same character set.
for e.g.
CREATE TABLE1 (
FIELD1 VARCHAR(100) NOT NULL PRIMARY KEY,
FIELD2 VARCHAR(100) NOT NULL
)ENGINE=INNODB CHARACTER SET utf8 COLLATE utf8_bin;
to
CREATE TABLE2 (
Field3 varchar(64) NOT NULL PRIMARY KEY,
Field4 varchar(64) NOT NULL,
CONSTRAINT FORIGEN KEY (Field3) REFERENCES TABLE1(FIELD1)
) ENGINE=InnoDB;
Will fail because they have different charsets. This is another subtle failure where mysql returns same error.
I use Ubuntu linux, and in my case the error was caused by incorrect statement syntax (which I found out by typing perror 150 at the terminal, which gives
MySQL error code 150: Foreign key constraint is incorrectly formed
Changing the syntax of the query from
alter table scale add constraint foreign key (year_id) references year.id;
to
alter table scale add constraint foreign key (year_id) references year(id);
fixed it.
The referenced field must be a "Key" in the referenced table, not necessarily a primary key. So the "car_id" should either be a primary key or be defined with NOT NULL and UNIQUE constraints in the "Cars" table.
And moreover, both fields must be of the same type and collation.
I also received this error (for several tables) along with constraint errors and MySQL connecting and disconnecting when attempting to import an entire database (~800 MB). My issue was the result of The MySQL server max allowed packets being too low. To resolve this (on a Mac):
Opened /private/etc/my.conf
Under # The MySQL server, changed max_allowed_packet from 1M to 4M (You may need to experiment with this value.)
Restarted MySQL
The database imported successfully after that.
Note I am running MySQL 5.5.12 for Mac OS X (x86 64 bit).
check to make the field you are referencing to is an exact match with foreign key, in my case one was unsigned and the other was signed so i just changed them to match and this worked
ALTER TABLE customer_information
ADD CONSTRAINT fk_customer_information1
FOREIGN KEY (user_id)
REFERENCES users(id)
ON DELETE CASCADE
ON UPDATE CASCADE
Solved:
Check to make sure Primary_Key and Foreign_Key are exact match with data types.
If one is signed another one unsigned, it will be failed.
Good practice is to make sure both are unsigned int.
I was using a duplicate Foreign Key Name.
Renaming the FK name solved my problem.
Clarification:
Both tables had a constraint called PK1, FK1, etc. Renaming them/making the names unique solved the problem.
The referenced column must be an index of a single column or the first column in multi column index, and the same type and the same collation.
My two tables have the different collations. It can be shown by issuing show table status like table_name and collation can be changed by issuing alter table table_name convert to character set utf8.
all, I solved a problem and wanted to share it:
I had this error <>
The issue was in that in my statement:
alter table system_registro_de_modificacion add foreign key
(usuariomodificador_id) REFERENCES Usuario(id) On delete restrict;
I had incorrectly written the CASING: it works in Windows WAMP, but in Linux MySQL it is more strict with the CASING, so writting "Usuario" instead of "usuario" (exact casing), generated the error, and was corrected simply changing the casing.