Data consistency and using session instead of foreign key and relations - mysql

I have a database with 15 tables that are connected between each other by different relations.
The main table is the login table:
login_id
user_name
user_pass
area_id
The area_id means that each area have it's own user and password so each action made, we can figure out in which area it happened.
Now instead of connecting all 15 tables into the login table with 1 to many relation and area_id is the foreign key, can I save the area_id in a session (because the backend language is PHP) and add it in each table without the relation.
So now, the user_info table contains:
name
address
phone
mother_name
contact
alternative_contact
area_id
But here this table is not connected directly to login table but it contains a similar field area_id and which it's value is added from session, and so we can now create a query by saying:
SELECT something FROM login JOIN user_info WHERE user_info.area_id = login.area_id
Does this makes data consistent or should I connect the login table with all other tables ?

Related

How I can Make Schema for Tables (Client, Companies and Users)

I am developing a program in PHP and I need to create MYSQL Schema for the following tables
Users
Clients
Companies
My Requirements are
A Client may have many companies and a company must belong to a client
Client can be able to login into the application as a user
A Company may have many users and each user can to login to an application
There is a superuser, which does not belong to any client|company but still can be login into the application
To handle the login system, I introduced a table users and created the schema as given below
table: clients
id as PK
name
table: companies
company_id as PK
name
client_id as FK
table: users
id as PK
company_id as FK
client_id as FK
is this ok? the way I am created the schema.

MySQL - How to change the primary key while importing the data from one database to another database?

Basically, I have two table users and user_details. Users table is as follows.
id name
1 A
2 B
and user details table is as follows
id user_id division branch
1 1 D1 B1
2 1 D2 B2
User_details table have user_id as the foreign key.
I need to import data from these two table into another database having similar two tables.
The problem is tables in db2 might already have entries with similar primary keys of user table. i.e. same user_id's.
Is there any way , I can change primary keys while maintaining the foreign key relation with user details table.
e.g. If db2 has user ids till 100, user id from db 1 should become 101 and this should also change the corresponding user id in user details table to 101 and so on.

mysql database how to store contact data

I want to fetch contacts from phone
and store into mysql database
I have:--
User table
id,userId, name, username, password
Contact table
id
userId(foreignkey(from user table))
contact_name
mobileNo(unique-primary key)
email(unique key)
Now problem is:
1)Suppose I am fetching sonia's contact list
where,
she has one contact:-
Name mobileNo email
Soma 5675675675 aaa#mail.com
another user preeti's contact list has:-
Name mobileNo email
sree 999999385 aaa#mail.com
and again another user lila's contact list has:-
Name mobileNo email
mona 5675675675 agawasti#mail.com
In my database
mobileNo and email is unique-primary key,
but for every user name/email/mobileNo may be different..how I can store and manage these data
for individual user??
I got one solution :-
1] User_info Table
id | UserId | name | username |password
2] Contacts Table
ContactId | Contact_Name | Contact_Phno |Contact_Email
3] User_Contact Table
UserId | ContactId
but Here, in Contacts table how can I manage to store sonia,preeti/lila's contact information? because the values are different.
You can have only one primary key per table, but maybe you defined a composite key (can't tell from the question)? I would suggest making 'id' a primary key and to add uniqueness constraints on the mobileNo and email. This choice also plays nicely with most of the web-frameworks you might be using.
ALTER TABLE `contact` ADD UNIQUE `unique_index`(`mobileNo`, `email`);
check your data type for phone number. May be it is integer and your phone number is over its limit..

database schema for two similar users

I have a doubt about this DB schema I'm making.
I have two similar users, but one has extra information than the other:
Type 1 : Administrator
- Name
- Lastname
- Email
- Password
Type 2: Student
- Name
- Lastname
- Email
- Password
- Status
- Sex
- Among other type of personal information fields
So, I'm hesitating about either make these two separate tables, and when they're going to log in, query them both (Because I have only one logging screen), or unify them as only one table User, and make another called like "extra" with a foreign key from User pointed to the latter.
What would be the most efficent way to accomplish this? Thanks for your time
I would make two tables and do the join after log in. Cache the extra facts about the user after they're logged in.
You should have a User table with these columns:
Id, Name, Lastname, Email, Password, IsAdmin
With a Student table:
UserId, Status, Sex, ...
A Student must also be a User - this will reduce duplication of data.
If you need more permissions than IsAdmin then remove that column and make UserPermissions and Permission tables.
If you're really that concerned about a join, then just make everything nullable and in one User table. I doubt it will matter in your use case (this is a much bigger topic).
An administrator is a role played by a person.
A student is a role played by a person.
A person could play one role at a time, or maybe multiple down the road. This is a business rule and should not factor into your database schema.
Use single table inheritance to allow for different types of roles in the same table.
create table people (
person_id int primary key,
given_name varchar(...),
surname varchar(...),
password varchar(...)--consider a `users` table instead
);
create table roles (
role_id int primary key,
person_id int not null references people(person_id), --use the long-hand foreign key syntax for mysql
type varchar(...), --admin or student
status varchar(...), --consider PostgreSQL over mysql, as it supports check constraints
sex varchar(...)
);

pull db row with foreign key

Lets say I have 2 tables in my db
user:
id, username
userpost:
userid, post
I have defined a Relation to connect userpost.userid to user.id in side the db (mysql).
Is there a query (as simple as possible) to pull the whole row (including user's row)
WITHOUT knowing the relationship at the programmer side? meaning, relaing on the relationship defined in the db itself
something like "SELECT * from userpost include foreign key"
SELECT user.*, userpost.*
FROM user, userpost
WHERE user.id=userpost.userid
Possible problem would be the 1:many relationship between user and userpost: You would get each user-record multiple times (once for each userpost record)