I am trying to create a database with two tables: Location and UserProfile.
Location contains addresses, and UserProfile contains HowtownLocation and CurrentLocation.
I want Hometownlocation and Currentlocation to contain IDs from Location, and cascade when a user is deleted (assuming more than one user does not share the same address).
One user can have multiple addresses (i.e Hometown and Current), and one address can be used by many users.
I have a sort-of working version, but I still need it to cascade changes... and I want to be able to input data into it in PHPMyAdmin by creating a new UserProfile.
I am using MySQL Workbench, but that is not important.
:)
MySQL Reference helps. The Syntax is:
[CONSTRAINT [symbol]] FOREIGN KEY
[index_name] (index_col_name, ...)
REFERENCES tbl_name (index_col_name,...)
[ON DELETE reference_option]
[ON UPDATE reference_option]
reference_option:
RESTRICT | CASCADE | SET NULL | NO ACTION
Also make sure that your database is not MyISAM, but InnoDB which is the default in newer MySQL versions as far as i know.
Related
Scenario:
I Created a POS (point of sale) system using mysql database. I am managing all shops data in one database. All operation was on server before but now the requirement is changed and i want to make it local too. The challenge i face is Duplicate entry for key primary
For example:
The system is used by two shop. If one shop added record where id=1 in item table in his local database and the second shop also added record where id = 1 in same table in his local database. Now when i send both data to my server database, it will give me error on Duplicate entry for key primary.
Conclusion:
I am not using MYSQL replication because it not suit my database structure so what will be the best solution for this issue?
You can solve this problem in many ways:
You should not sync the primary key as well from the local to remote, rather you can have some order ID (SHOPID_SOMERANDOM-NUMBER) which will be unique for shops .
Otherwise you can keep a composite key as primary key like Autoincrement_ID+SHOP_ID so that while syncing this will never be duplicate.
This shop_ID should be generated from the server at the time of installation and should not be duplicate.
Good day, I have developed a system using php and mysql. From 5 systems turned into 1 system. Basically, there is a huge possibility that in system 1 there will be a user table and has primary keys. The same with other systems.
My problem is there are identical primary ids to be migrated in current developed system.
In system 1, there are 70,000 data in user table. System 2, 22,000 records. and less than 3000 in other systems. Unfortunately, primary keys were foreign keys in other tables.
How can I migrate those data without conflict in primary keys and how can I update foreign keys?
Please help.
You have to create a mapping between the various user records and the new consolidated user master table. Pls note that this may be true not only for the users, but also for other type of data as well.
When you create the mapping table, then have a new user id field, an old user id field and a field identifying the source system. Then copy over the users by source system to the mapping table and during the copy specify the source system. This way you can distinguish between the users with the same user ids in the various source systems and generate the new user ids.
When you migrate other data from the source systems containing user id, you need to use the mapping table to replace the old user ids with the new one.
I have 2 tables (with identical structure) and I want both to update after a form submission and both have the exact same data.
The tables structure are as below:
id | parent_id | name | surname
The id is the primary key, which means that I cannot execute the following query because it throws an error (Duplicate entry '1' for key 'PRIMARY'):
INSERT INTO table_2 SELECT * FROM table_1
My goal is to keep the data identical (including the PRIMARY) in both tables after a row is updated or a new row is added. How can I do this and avoid the Duplicate entry error?
Because your forms are on two different pages, you need some way to connect the two when creating the second record.
Using language as an example, I would have my "primary" language form on one page. Submit that information to your "default/primary" language table.
Then, on your "secondary" table, I would include a select input that listed out my "primary" languages. The values would be the primary key of your "primary" language. The user has to choose a parent language to connect to. Otherwise the secondary language has no idea who it belongs to.
On your secondary table, I would not have an auto_incrementing primary key. That way you can share the same ID as your parent table.
I would create a foreign key to the "secondary" table that was related to the "primary" table id field.
This will allow you to run queries like:
Not tested at all - and probably incorrect syntax.
SELECT * FROM `primary` WHERE something JOIN `secondary` ON `secondary.id` WHERE `secondary.id` = `primary_id`
But, that would give you an idea of what I'm talking about.
Edit
Based on our conversation, it sounds like you will either need to drop your secondary table on each insert (to clear the primary keys). Or I found this thread on resetting the key that might be helpful.
I am currently working on a PHP/MySQL project for an assignment. In studying the efficient design of databases while working on the assignment I notice that in many cases it is good practice to create a third table when working with only two sets of data.
For example, if we have a table for "Students" and a table for "Addresses" it appears to be a good idea to create a third table i.e. "Student_Addresses" since a student can hypothetically have more than one address (separated parents etc.) and a single address can represent more than one student (siblings).
My question is: How do we go about populating that third table? Is there a way that it is done automatically using primary and/or foreign keys?
I've tried Google and my textbook to understand this but I've gotten nowhere. Links to tutorials or articles would be greatly appreciated.
Thanks for your help. I hope the question and example are clear.
n:m or 1:m normalization rule
Option 1:
user table
id
f_name
s_name
......
user address table
id
user_id // this should be index only as foreign keys will allow 1:1 only
address line 1
address line 2
address line 3
address_type (home, office ....)
Option 2:
user table
id
f_name
s_name
......
address table
id
address line 1
address line 2
address line 3
address_type (home, office ....)
user_address table
userId
addressId
according to your description option 2 would be the right solution. After adding the data to user table and address table then you need to add the data to user_address table manually. Some Object relational mapper (ORM) may do add the data to the third table automatically but you need to define the relations. check http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/association-mapping.html.
http://docstore.mik.ua/orelly/linux/sql/ch02_02.htm
http://www.keithjbrown.co.uk/vworks/mysql/mysql_p7.php
You can save the data in the third table using triggers when the data is inserted/updated/deleted in your base tables. You can learn more about triggers at
mySQL Triggers
However in your case it would be better if you could write the logic at the application/code level to make an entry in the third table. You can set up foreign key relationships to this table from your base tables so that the data remains consistent.
There is no native method in MySQL to populate Student_Addresses in your situation - you have to take care of entering data (connections) by yourself, but you can use - for example - transactions - see answers in this topic: SQL Server: Is it possible to insert into two tables at the same time?
For taking care of connections consistency - in Student_Addresses make not-null fields for relations to ID from Student and ID from Address, make both of these field as unique key together and use ON UPDATE CASCADE and ON DELETE CASCADE. This will take care of removing records from junction table when removing records from any of two other tables and also won't allow you to add same address to the same student twice.
I don't think data will be populated automatically rather it's responsibility of user to insert data.
I am note sure about PHP but using Hibernate and Java this can be done seemlessly. Since data of Students and addresses could be coming through some web application Hibernate can map java objects to records in table and also populate relationship table.
I am using xamp.
I created a DB using SQL Yog,
I opened my localhost/phpmyadmin/
then selected the newly created database.
I wanted to make relations among tables, for instance there are two tables,USER and USERSTATS, I want to create relation depending on USER_ID, which exists in both table.
I selected create relation option, selected reference key from USERS table, then click on STATS table and selected foreign key, i got a prompt "Create relation", I clicked OK.
Now it was to supposed to be creating relation, but it's not, just a small blank popup window opens in firefox, with link
localhost/phpmyadmin/pmd_general.php?db=MYDBNAME&server=1&token=d9d3ed2661d4cc1d0db47eca1ebee996
But it is not creating the relation.
Please assist me in resolving this issue
Have you created your tables with InnoDB ? InnoDB accepts creation of foreign keys
Did you go through the complete phpmyadmin installation steps? You have to create the phpmyadmin specific tables. Without them you will not be able to see relations or create them.
http://docs.phpmyadmin.net/en/latest/setup.html#phpmyadmin-configuration-storage
mysql doesn't support the foreign key relations, though it accepts that keyword. That's probably the reason phpMyAdmin doesn't allow this.
If you can use InnoDB engine for your table, foreign keys are supported.
From an answer from stackoverflow for why-my-table-doesnt-support-foreign-keys
ALTER TABLE tableName ENGINE = InnoDB;
you need to add an index to the field you want as foreign key. You can do it by going to the table and clicking "Index" or you can do it manually: "ALTER TABLE YourTable ADD INDEX ( YourField )"