Create database associated with another database
I have two databases: users and addresses. 'Users' stores the websites users, whereas 'addresses' stores the many addresses which the users are able to generate.
I would like to associate the databases with each other, such that a user with ID=1 in the 'users' database owns a table in the 'addresses' database with ID=1. I would also like the user (again with id=1) to be able to generate as many addresses as he likes, which fills up the other table which is essentially owned by him.
I have a bunch of code but the majority of it is me troubleshooting and I haven开发者_如何学JAVA't made much progress. Any help is appreciated, thanks.
You're close, but instead of having the ID be the same in each table, you'll want to connect them another way. If a user can have many addresses, you'll probably want a user_id column in your addresses table. Then given a user, with an ID of 1, you can connect the two with a query such as:
SELECT * FROM addresses WHERE user_id = 1;
There's a bit about this sort of thing in this article: http://dev.mysql.com/tech-resources/articles/intro-to-normalization.html
You don't want multiple databases for this, you want just one.
1 database with 2 tables. Actually, 3 tables. Maybe 4.
- User
- Address
- AddressTypes
- User_Address
User
-------------
UserID int (PK)
User_Last_Name
User_First_Name
Address
---------------
AddressID int (PK)
AddressTypeID int (FK: AddressTypes)
etc.
Address_Types
---------------
AddressTypeID int (PK)
AddressType nvarchar(50)
User_Address
---------------
UserID (FK: User)
AddressID (FK: Address)
AddressTypeID (FK: AddressTypes)
The PK of User_Address would be the combination of all three IDs.
精彩评论