MySQL unique id across multiple tables - mysql

I have 2 tables with the following structure:
CREATE TABLE IF NOT EXISTS `car` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(255) NOT NULL ,
`title` VARCHAR(255) NOT NULL
PRIMARY KEY (`id`) ,
UNIQUE INDEX `name_UNIQUE` (`name` ASC) )
ENGINE = InnoDB;
CREATE TABLE IF NOT EXISTS `book` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`name` VARCHAR(255) NOT NULL ,
`title` VARCHAR(255) NOT NULL
PRIMARY KEY (`id`) ,
UNIQUE INDEX `name_UNIQUE` (`name` ASC) )
ENGINE = InnoDB;
I generating the name column from the title for example: if title: Foo Bar -> name: foo-bar. I'm using this value in the URL. Eariler i had URLs like this /car/foo-bar or /book/foo-bar having same name value in both table wasnt a problem. But i want shorter URLs now: /foo-bar or /foo-bar-2.
How can I make the name value unique across multiple tables?

If you're building an OO application, you would put the name in a separate table and add foreign keys from books and cars to "names"... or whatever that table means in your application, for example product.
Otherwise you can enforce it with triggers, but I don't know how triggers work in mysql.

If you want to force it to be unique, then you could create a third table called name_reference for example with a prinary key of the combination of the fields name and a field called type. Then you could have book and car have foreign keys to the name_reference.

Related

Inserting into multiple tables at once

Structure:
Table A)
CREATE TABLE Item (
Id INT(11) UNSIGNED NOT NULL AUTO_INCREMENT COMMENT 'Unique id of an item',
`By` VARCHAR(50) DEFAULT NULL,
PRIMARY KEY (Id),
CONSTRAINT FK_Item_User_Name FOREIGN KEY (`By`)
REFERENCES User(Name) ON DELETE NO ACTION ON UPDATE CASCADE
)
Table B)
CREATE TABLE ItemName (
Item_Id INT(11) UNSIGNED NOT NULL COMMENT 'Item id this name is referencing',
Language VARCHAR(5) NOT NULL COMMENT 'language code by ISO 639-2/T',
Translation VARCHAR(50) NOT NULL COMMENT 'Item name for given language',
PRIMARY KEY (Item_Id, Language),
CONSTRAINT FK_ItemName_Item_Id FOREIGN KEY (Item_Id)
REFERENCES Item(Id) ON DELETE CASCADE ON UPDATE CASCADE
)
Table C)
CREATE TABLE User (
Name VARCHAR(50) NOT NULL,
Password VARCHAR(50) NOT NULL,
Salt VARCHAR(255) NOT NULL,
Blocked TINYINT(1) DEFAULT 0,
PRIMARY KEY (Name),
UNIQUE INDEX UK_User_Name (Name)
)
Question:
Now I want to insert a new item. Let's say the user provides us with:
Translation
Language code
Username
What i got so far:
I was thinking of puting it in a transaction and inserting into each table after eachother. But then i'm stuck on how do i know what Item.Id to use in the ItemName.Item_Id field since the Item table will AI a new Id for the insert.
I could get the last Item.Id, but then it might be pointing to another item if multiple users were to be inserting a new item simulaneuosly.
ps. Engine=InnoDB
BEGIN;
INSERT into one table;
get the last_insert_id() -- there are many ways to do this, depending on the API
INSERT into next table...
... ;
COMMIT;
Do the inserts in the order that avoids violating your FOREIGN KEYs.

Error during the creation of table due to foreign key

I have table1 already in my db.
Table1:
CREATE TABLE `product` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`typename` varchar(255) DEFAULT NULL,
`typecode` varchar(55) DEFAULT NULL,
`parent1` int(11) DEFAULT NULL,
`parent2` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
KEY `parent1` (`parent1`),
KEY `parent2` (`parent2`)
) ENGINE=InnoDB AUTO_INCREMENT=396 DEFAULT CHARSET=latin1;
I tried to create the second table with foreign key which has reference to product.typename
this is the creation query I have used.
CREATE TABLE measurements (
id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
age_group varchar(20) NOT NULL,
article_type varchar(255) DEFAULT NULL,
dimension text ,
createdOn int(11) NOT NULL,
updatedOn int(11) NOT NULL,
createdBy text NOT NULL,
foreign KEY(article_type) references product(typename)
)ENGINE=InnoDB AUTO_INCREMENT=396 DEFAULT CHARSET=latin1;
But this table creation is a failure with the following error.
ERROR 1215 (HY000): Cannot add foreign key constraint
I have done show engine innodb\g
------------------------
LATEST FOREIGN KEY ERROR
------------------------
2015-05-15 19:03:28 131f71000 Error in foreign key constraint of table db/measurements:
foreign KEY(article_type) references product(typename)
)ENGINE=InnoDB AUTO_INCREMENT=396 DEFAULT CHARSET=latin1:
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.6/en/innodb-foreign-key-constraints.html
for correct foreign key definition.
Can some one point me the problem and what is this first columns concept?
Referenced column should be Primary key. Here
foreign KEY(article_type) references product(typename)
you want to reference with typename column which is not PK.
To do it in properly way you should create table ProductType like this:
CREATE TABLE `ProductType` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`typename` varchar(255) DEFAULT NULL,
`typecode` varchar(55) DEFAULT NULL,
) ENGINE=InnoDB AUTO_INCREMENT=396 DEFAULT CHARSET=latin1;
then you can create reference like this:
CREATE TABLE measurements (
id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
age_group varchar(20) NOT NULL,
IdProductType NOT NULL,
dimension text ,
createdOn int(11) NOT NULL,
updatedOn int(11) NOT NULL,
createdBy text NOT NULL,
foreign KEY(IdProductType) references ProductType(Id)
)ENGINE=InnoDB AUTO_INCREMENT=396 DEFAULT CHARSET=latin1;
Don't forget to do it with Product table.
Above solution is only suggestion, you have to consider your table structure yourself.
A foreign key references a key. This is usually the primary key, but doesn't have to be. In your case however you reference a column (typename) which is not defined as a key. This shows a design flaw.
You decided to use technical IDs as primary keys for your tables. You can do this. But if you do this, keep two things in mind:
You've created IDs in order to link tables easily. So don't reference a record by another column (such as typename), but by its ID.
You must still make sure that the table's natural key is unique.
As to point 2: What is your table's natural key? What is or are the fields that uniquely identify a record (apart from your technically created ID)? Is it typename? Is typename the product's name and must it be unique? Or is this typecode? Whatever it is, give this field a unique constraint, so you cannot have the same product twice in your table.
Maybe it would help you learn to design your database, if you didn't use technical IDs at all. Give it a thought.
Just a side note: Be aware that MySQL has a strange way of using the keyword KEY:
create table t (col int key);
Here KEY really means the table's primary key. col cannot be null and col must be unique. It is short for:
create table t (col int primary key);
However,
create table t (col int, key(col));
is something entirely else. Here, KEY is not short for PRIMARY KEY, but a synonym for INDEX. col can be null, col doesn't have to be unique. So better use the synonym INDEX to make it clear to a reader:
create table t (col int, index(col));
When working with an additional ID, as you are doing, you even need a unique index:
create table t (id int primary key, col int, unique index(col));
or
create table t (id int, col int, primary key(id), unique index(col));

MySQL issue.. creating and referencing tables at once

I was trying to create multiple tables (some of them referncing the other tables) at once.
I think I matched data types and set primary/foreign keys correctly. But I can only see an error
of 'You cannot add foreign key constraint'. I thought the referenced tables might be created on the first before other tables refernce it, so I reversed the order and the result was the same.
Lastly I tried creating and executing only the referenced tables first(item_type), then referencing tables(item) later.. and... it worked!
However, I wonder if those codes can be executed at once.
Here is code below..
(just two tables are shown to make it simple..)
CREATE TABLE item (
i_id SMALLINT unsigned NOT NULL AUTO_INCREMENT,
i_name VARCHAR(30) NOT NULL,
t_id SMALLINT unsigned NOT NULL,
PRIMARY KEY (i_id),
FOREIGN KEY (t_id) REFERENCES item_type(t_id)
) ENGINE=INNODB;
CREATE TABLE item_type (
t_id SMALLINT unsigned NOT NULL AUTO_INCREMENT,
t_name VARCHAR(20) NOT NULL,
PRIMARY KEY(t_id)
);
You can't define a foreign key to a table that doesn't exist, so doing the CREATE TABLE operations in the order above is not going to work. If you create the item_type table first, then the item table with the foreign key to item_type, it should work.
Database engines execute sql code in batches, so one statement that crate table is one batch, but in you example, first batch references second batch which is not executed yet, so change order of batches and it will be working.
CREATE TABLE item_type (
t_id SMALLINT unsigned NOT NULL AUTO_INCREMENT,
t_name VARCHAR(20) NOT NULL,
PRIMARY KEY(t_id)
);
CREATE TABLE item (
i_id SMALLINT unsigned NOT NULL AUTO_INCREMENT,
i_name VARCHAR(30) NOT NULL,
t_id SMALLINT unsigned NOT NULL,
PRIMARY KEY (i_id),
FOREIGN KEY (t_id) REFERENCES item_type(t_id)
) ENGINE=INNODB;

Yii Database structure

I'm designing database for Yii web application and I'm not sure I'm doing it right way.
CREATE TABLE user
(
id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(128) NOT NULL,
password VARCHAR(128) NOT NULL,
email VARCHAR(128) NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE post
(
id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(128) NOT NULL,
description TEXT NOT NULL,
media TEXT NOT NULL,
tag_id INTEGER NOT NULL,
status INTEGER NOT NULL,
create_time INTEGER,
update_time INTEGER,
user_id INTEGER NOT NULL,
CONSTRAINT FK_post_user FOREIGN KEY (user_id)
REFERENCES user (id) ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE comment
(
id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
content TEXT NOT NULL,
status INTEGER NOT NULL,
create_time INTEGER,
user_id INTEGER NOT NULL,
post_id INTEGER NOT NULL,
CONSTRAINT FK_comment_post FOREIGN KEY (post_id)
REFERENCES post (id) ON DELETE CASCADE ON UPDATE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TABLE tag
(
id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(128) NOT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
Problems:
1.) In post table I have tag_id. I was thinking to store all existing and newly added tags in tag table and then when someone use already existed tag or made new tag to add as tag_id to post table with ",". Is this the best solution?
2.) In comment table I have user_id and post_id. How I think foreign key are working there should be set 2 foreign keys in comment table? I tried that but got error.
3.) In post table there is media row. I'm going to store in this row location to uploaded image OR embed code from youtube/or any other source. Is this ok to use only same row for that or should I use seperated? 1 for images, 1 for embed code?
Thanks in advance
1) In case I'd use column with name tags instead tag_id where i'd keep serialized array of tag_id's from table tags
2) The problem i think because you tried add foreign keys with same name.
Try to use
CONSTRAINT FK_comment_post FOREIGN KEY
CONSTRAINT FK_comment_post_2 FOREIGN KEY
3) What will be if you'll decide to add another media info in post(pdf_file, or music). Add new column in table post it's not correct. i'd recommend you create additional table postmeta where you can create any type of content, and instead media use post_text. Like in wordpress.

Toxi mysql performance, tagging. Help understanding

I am using the Toxi scheme for tagging items on my website. Being quite new to mysql let alone tagging, I'm just doing a sanity check. Here is my table initialization script.
CREATE TABLE IF NOT EXISTS Items (
item_id INT UNSIGNED NOT NULL AUTO_INCREMENT ,
item_name VARCHAR(45) NULL ,
media_type VARCHAR(20) NULL ,
file VARCHAR(45) NULL ,
description VARCHAR(500) NULL ,
PRIMARY KEY (item_id)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS Tags (
tag_id INT UNSIGNED NOT NULL AUTO_INCREMENT ,
tag_text VARCHAR(25) NOT NULL ,
PRIMARY KEY (tag_id) ,
UNIQUE INDEX (tag_text)
) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS Item2Tag (
item_id INT UNSIGNED NOT NULL ,
tag_id INT UNSIGNED NOT NULL ,
PRIMARY KEY (item_id, tag_id) ,
INDEX (tag_id) ,
FOREIGN KEY fk_Item (item_id) REFERENCES Items (item_id) ,
FOREIGN KEY fk_Tag (tag_id) REFERENCES Tags (tag_id)
) ENGINE=InnoDB;
http://forge.mysql.com/wiki/TagSchema
Question 1
Is my understanding correct that there is an entry in the "Item2Tag" table for every "item_id" to "tag_id"? It just seems like that is going to be a huge table when I have ~3000 items and each item could have ~5 tags. Is that not a concern/not really a big table?
Question 2
Could someone help me understand importance of having Foreign Keys/References? Why do I need those and what do they do?
Question 1: Yes, that's correct.
Question 2: You don't really need them for Toxi schema as far as I know. But they help you avoid having entries in the reference table while not in the item table. It's more of a constrain to prevent headaches than a need. ie. You delete item number x, the entry associated to item number x also gets deleted.