One to Many Database field uniqueness rule in YII - yii

I have a table. Lets say we have a userid of 23, which corresponds to various roles that the user might hold: scientist, researcher and developer. ID's respectively: 2,8,9.
So, userid can be duplicated but roleid as it relates to userid is unique. Note that if there are more then 1 users I want it so that roles can be duplicated (unique to the user, duplicated across different users) such as:
In Yii, under rules of the model you can create a rule to do uniqueness. I want the RoleID field to be unique for that user. So if user x has role y, it won't add role y again (creating duplicates).
here's the schema:

Related

How can I create a relation ManyToMany between three different tables with TypeORM NestJS?

I am developing a database with TypeORM, and will have tables such as user, company and user_roles.
Each user can be part of multiples companies, and each user can have one user_role associated within that company. For example: user "A" is part of company "company_x" and has a role of admin. However, user "A" is also part of company "company_z" and has a role of member.
How can I achieve this using entities in TypeORM in nestJS framework in order to create my migrations? I was trying to do this while creating a pivot table with #ManyToMany() and #JoinTable(), but how can I relate three tables?
My desired output would be:
user_id
company_id
user_role_id
1
1
1
1
2
2
and I have three tables users, company and user_roles.
PLEASE HELP

Delete rows from multiple tables due to data from one table

I have multiple tables that I need to delete rows from and they are all contingent on data from one table. For instance, I have a Users Table and in the Users Table the Users have a UserId. I am trying to get rid of all the data that is in relation to that one user in all the other tables.
For instance, there is a Members Table and in the Members table, there is a Foreign Key relationship to the Users table so it's tied by the UserId. The Members table can have multiple members associated with the UserId (A User can be members to more than one thing so it's a one to many relationship). Then there is also a Permissions table and in the Permissions table it has a foreign key relationship to the Members table. And in that table the Members can have multiple permissions (A Member can have permissions to more than one thing is that's a one to many relationship).
What I am needing to do is delete all the rows in the Permissions table that are in relation to the all the MemberId's that are in relation to the UserId from the User's Table (i.e. - Bob has a UserId of 7 and in the Members Table he is a Member of 3 things so he has 3 MemberId's associated to his name and in the Permissions Table those 3 MemberIds also have 3 Permissions associated to those 3 MemberIds. I am needing to delete all 9 Permission rows according to those 3 MemberIds, then delete all the 3 Member rows according to the 1 UserId, and then delete the one UserId according to a UserName).
I've tried to Inner Join multiple tables and also connect them with Unions but I am having difficulty of tying all those tables down to that one UserId and carrying that data through the flow of logic.
User has a UserId of 7
User is is associated with 3 MemberId's
The MemberId is associated with multiple PermissionId's
So I need to delete all those PermissionId's (I did a WHERE statement just on the User's MemberId's), all 3 instances of the MemberId's, and the User.
If you are aware of UserID that you want to delete from different table, then this is easy to handle from table Users and Members. You need to use a Sub Query when you will delete records from Permissions table. But if there are relation (PK/FK) established between tables, you need to maintain some sequence as below-
Important Note: Delete is a risky operation unless you have proper Backup. So please try to execute scripts on test data first.
At first you have to delete records from Permission table with following script-
DELETE FROM Permissions
WHERE MemberID IN
(
SELECT MemberID FROM Members
WHERE UserID = 7
)
In second step, you have to delete records from Members table as below-
DELEET FROM Member WHERE UserID = 7
In third step, you have to delete users from Users table as below-
DELETE from Users WHERE UserID = 7

SQL database structure with two changing properties

Let's assume I am building the backend of a university management software.
I have a users table with the following columns:
id
name
birthday
last_english_grade
last_it_grade
profs table columns:
id
name
birthday
I'd like to have a third table with which I can determine all professors teaching a student.
So I'd like to assign multiple teachers to each student.
Those Professors may change any time.
New students may be added any time too.
What's the best way to achieve this?
The canonical way to do this would be to introduce a third junction table, which exists mainly to relate users to professors:
users_profs (
user_id,
prof_id,
PRIMARY KEY (user_id, prof_id)
)
The primary key of this junction table is the combination of a user and professor ID. Note that this table is fairly lean, and avoids the problem of repeating metadata for a given user or professor. Rather, user/professor information remains in your two original tables, and does not get repeated.

Storing list of strings in MySql column

I have a users table which contains data for registered users, each row represents a user. One column in particular should contain a list of groups the user is part of, at the moment that column is of TEXT type and I'm storing that list as a string where groups are separated with a semicolon, something like:
admin;moderators;devteam
And I was wondering: "Is this a good idea?", is there a better/safer way to do this that doesn't require a lot of effort to implement or is this "ok"?
Here is a pic of the table as of now:
And I was wondering: "Is this a good idea?"
Short answer: probably not.
Why
If you will ever need to do any manipulation on that column, you will find yourself in big trouble. Simply selecting all users in a group will require some operations on a string (usually not performance-friendly). Same will hold true for sorting, joining and all the other operations SQL is great for.
Solution
What you describe is a typical example of N:N relationship, where each user can belong to multiple groups and each group can have multiple users in it.
The 'standard' way of modeling this relationship is creating a new table, where each row will represent a user belonging to a group. The columns will be group and userID.
With data from your example
userID | group
--------|----------
1 | admin
1 | moderator
1 | test
This allows to have one row for each user in the users table, and getting the groups of a specific user is as simple as
select group
from user_groups
where userID = '1'

Database design using SQL Server 2005,

I have a user table with userid (pk), password, usertype.
I have another table student with stdid (pk), stdname, stdaddress.
I have a third table faculty with facid (pk), facname, facaddress.
What I want to do is populate the user table with the pk from either student table or faculty table.
How do I implement this in SQL Server 2005.
Can someone help me?
Edited Below
In my database, the student and faculty table are already implemented as part of another module. I need to implement the authentication part over LAN to be able to be accessed from anywhere, so I can not restructure that part. So I am using the user table with userid (username) and password in that table.
The userid is either stdid or facid and also the pk of the user table.
What you have is a bad design. You do not want to use one or the other other PK as the PK in a table. This cannot every work. What happens when you try to insert student 10 (who is Joe Jones) but faculty 10 (Mary Smith) is already in the table. Well, the insert would fail becuse of the unique requirement of a PK.
If all studetns and faculty are always users, then userID is the PK and FacultyId and StudentID are the FKs to that table not the reverse.
insert into user_table (select facid as userid from facultaty_table union select stdid as userid from student_table)
this will leave column password and usertype as null otherwise it pumps up userid_table with all the PKs from the others
What are the data types of the keys? Is there a guarantee that they're going to be unique across both the student and faculty tables?
This seems like it's going to be a difficult design to work with over time. Maybe try moving the central focus from the "person" tables to the user table and assign roles from there? Something like this:
User
------
Id
Password
UserType
(where's the username?)
UserInfo
------
UserID
Name
Address
Role
------
Id
Name
UserRole
------
UserID
RoleID
(The Role table would have at least two records, one called "Student" and one called "Faculty", but could allow for more roles as well.)
There are a few benefits here:
You need only store names and addresses in one place, rather than duplicate the model across different roles.
A user can have multiple roles. For example, a faculty member could take some classes and be a "student" for a while. A grad student can teach some classes and be a "faculty member" for a while.
New roles can be added without having to change the data model.