I'm having a table of user information.
Primary key: userID int
I want to map a contact list for each user so I build another table with 2 fields:
userId, contactId both int
Now I want that both fields will be together a primary key
so will be able to save several unique contacts id for each user Id referred to first table, and of course do an inner join with on contactID with userID
How do I do that in mysql(using phpMyAdmin)?
Query examples are also possible of course
Related
I want to create a database for a group feature in my Flutter Application and I want the group to consist of administrators with different roles like Moderator, Secretary, etc...
Group Table
groupId
int not null primary key
roleId
int foreign key
firstName
string (50)
lastName
string (50)
email
string (80)
address
string (100)
Role Table
roleId
int not null primary key
roleType
string (20)
groupId
int foreign key
Admin Table
adminId
int not null primary key
roleId
int foreing key
groupId
int foreign key
Can I have groupId as a Foreign Key in Role_Table and also have groupId as foreign key in the Admin_Table?
It is not fully clear to me what you want to achieve with the group. But to answer your question: it is possible to reference the same table as foreign key in different tables. So yes, you can use groupId as foreign key in Role_Table and Admin_Table.
However there are some potential issues here: Admin_Table has its own roleId. This is either an undesired redundancy, if your code has to keep both consistent, or a design that is potentially ambiguous, since a given adminId could be associated directly with one groupId, and indirectly via the role table to another groupId. Thera are some situations where this triangular relationship is needed on purpose, but are you sure in your case?
There is a potentially bigger issue: In Group_Table, a groupId (primary key) is associated to a roleId (foreign key). This means that there is only one role possible for a given group. At the same time, in the Role_Table a roleId (primary key) is associated to a groupId (foreign key). This means that either there is a one-to-one relationship between groups and roles (that needs to be kept in sync), or roles and groups form a complex interleaved hierarchy.
I have a database consist of 3 tables each table indicate one user like this:
Admin table 'User 1'.
Staff table 'User 2'.
Student table 'user 3'.
I want to implement a common table between them called 'POST'
WHERE each user can post content to the post table content column,
however I need to Identify the type of posters ' the one who posted the post ' so I'm adding a column
user_type to the table POST
the column user_type should hold the ID of the USER ' Admin, Staff, Student '
I'm in a case where I want to be able to refer to this column ( user_type) in the POST table to multiple table columns Admin table, Staff table, Student table. So
I Can identify the USER who posted the post.
All these kind of users are persons that you need to store in one parent table called "Users" or "Persons" from which the parent's table are "User 1", "User 2", "User 3"...
This is called inheritance in data modeling.
Thus you can join the post table to the father table.
As an example :
CREATE TABLE T_PERSON_PRS (PRS_ID INT IDENTITY PRIMARY KEY, PRS_NAME...)
CREATE TABLE T_PERSON_ADMIN_PAM (PRS_ID INT PRIMARY KEY FOREIGN KEY T_PERSON_PRS (PRS_ID), ... )
CREATE TABLE T_PERSON_STAFF_PSF (PRS_ID INT PRIMARY KEY FOREIGN KEY T_PERSON_PRS (PRS_ID), ... )
CREATE TABLE T_PERSON_STUDENT_PSD (PRS_ID INT PRIMARY KEY FOREIGN KEY T_PERSON_PRS (PRS_ID), ... )
CREATE TABLE T_POST_PST (PST_ID INT IDENTITY PRIMARY KEY, PRS_ID INT PRIMARY KEY FOREIGN KEY T_PERSON_PRS (PRS_ID), ...)
I'm planning on creating an "Organizations" table, where the columns are:
Organization Id (primary key), Name, Plan and Company Id (foreign key).
My issue is that per organization there can be multiple companies, that means, I would need to have multiple records with the same Organization Id but with a different Company Id, but since the Organization Id is my primary key I know I can't do this (because it's unique).
How can I structure/design this? It's essentially a one to many relationship between an organization and a company. I'm just starting out with MySQL so I may be missing something obvious.
Drop companyId from the organisations table and have organisationId as foreign key in the companies table.
CREATE TABLE Organizations (
OrganizationId INTEGER,
... other fields,
PRIMARY KEY (OrganizationId)
)
CREATE TABLE Companies (
CompanyId INTEGER,
OrganisationId INTEGER,
... other fields,
PRIMARY KEY (CompanyId),
FOREIGN KEY (OrganisationId) REFERENCES Organizations(OrganisationId)
)
In SQL i have a table for users. On my website i want people be able to make groups consisting of 2 or more users, but i have no idea how to store that. People can be a part of multiple groups at the same time. If i'd make a table groups, how could i store it's members in that table?
You would have a table groups and one called userGroups. These would look like:
create table groups (
groupId int auto_increment primary key,
groupName varchar(255)
);
create table userGroups (
userGroupId int auto_increment primary key,
userId int not null,
groupId int not null,
foreign key (userId) references users(userId),
foreign key (groupId) references groups(groupId)
);
What you're describing is called a many-to-many relationship, and it requires a third table:
UserTable: UserID (unique), UserName, etc.
GroupTable: GroupID (unique), GroupName, etc.
UserGroups: UserGroupID (unique), UserID, GroupID
With a third table holding the primary key(pk)) of a group and a pk of a person. These columns are the pk of the table. You can have other to
Columns too, e.g. date joined or whatever. You have one row in this table per person/group.
It's a common concept/pattern so make sure you learn and understand it.
I have created 2 table in 2 different databases. First database name is user which contains userDetails table, which have id as a primary key and user_name, and my second database is customer which have 1 table called as customerDetails, which have 1 id as a primary key and customer name and one view of above user table which contains id of that user table and name.
So what i want to do is, creating a foreign key of that view in customerDetails table, so that i can access user table from customer database through view. I don't know how to achieve this, as i am new to database concepts please anyone can get me out of this.
Whole scenario is as follow,
> Database Name : user
> Table Name : userDetails
> Fields : id userName
>
> Database Name : customer
> View Name : user_view
> Fields : id userName
>
> Database Name : customer
> View Name : customerDetails
> Fields : id custName
i want in last table that is in customerDetails last column as a foreign key from view. How can i achieve this?
Views are not related to foreign keys as much as getting to your data as mentioned in comments by your peers. The below uses a Junction Table to intersect users and companies, enforcing a Foreign Key constraint between databases (not a bad idea for shared info between databases).
The Junction Table is many-to-many, and hooks users and companies together.
Schema:
create schema userDB;
create table userDB.userDetails
( id int auto_increment primary key,
userName varchar(100) not null
);
create schema customerDB;
create table customerDB.customerDetails
( id int auto_increment primary key,
custName varchar(100) not null
);
create table customerDB.userCustomerJunction
( -- a many-to-many mapping
id int auto_increment primary key,
userId int not null,
custId int not null,
unique key (userId,custId), -- no dupes allowed
foreign key `ucj_2_user` (userId) references userDB.userDetails(id),
foreign key `ucj_2_cust` (custId) references customerDb.customerDetails(id)
);
Test it:
insert customerDB.customerDetails(custName) values ('Exxon Mobil'); -- id 1
insert customerDB.userCustomerJunction(userId,custId) values (1,7); -- FK Failure
-- above line generates an error 1452 as expected
insert userDB.userDetails(userName) values ('Kelly'); -- id 1
insert customerDB.userCustomerJunction(userId,custId) values (1,1); -- success, FK's satisfied
Remember that the user and company are separate entities and to interface the two would require something that ties them together. A Junction table is a fantastic place to put a column such as effectiveRights or something. It would denote what the user can do, such as insert, update, delete, view, blacklist, etc.
Creating a view between user and company is simply like any join, but in this case it would be between databases with the whichDB. in front of the table name. The view is materialized and manifested in the physical tables. So as the physical rules, the physical has the FK's in force (data integrity). And the addition of an effectiveRights column will assist you in determining what each user and company can do together: such as, yes, this user has certain rights to this company info, etc. With a rights bitmark, or separate columns for rights, all in the Junction table. For an example of Junction tables, see this Answer of mine.