I have question regarding SQL and phpmyadmin queries - php

I have two table in my database first is shop_details and second one is shopOwner_login
in shop_details table there is column named
ShopOwner_email
Shop_Pass
and same in shopOwner_login table there is
ShopOwner_Id,
ShopOwner_email,
Shop_Pass
Now i want to make a form in which if user enter data in shop details table like shopowner Email and its password it will store on those two table automatically goes
Can i do this if yes please help me for it i also tried foreign key i am so confused with foreign key because i am new to php and MySql please help me.

Each of your table should have Primary key. Primary Key is the unique id (could be number or string or even uuid. Ex: ShopOwner_Id) which could be used to fetch any row in the table. This primary key is used to establish relationships between tables.
Also for relationships there could be 3 types of major relationships between two tables.
One to One
One to Many or Many to One
Many to Many
For example,
lets say you have two tables UserLogin and UserProfile. UserLogin contains -> id, email, password, and verified. Whereas UserProfile contains -> id name, address, mobile, dob, etc. Here Each UserLogin will have single UserProfile whereas each UserProfile will have only one UserLogin. So they have One to One relationship. In this case, You add the foreign key to the both tables. You will add profile_id as foreign key in UserLogin whereas login_id in UserProfile.
Lets say, you have two tables Shop and User. Where each Shop will belong to a single User (in your case shopowner) But a User can have multiple Shops. In this case, Shop and User have Many to One relationship (or User and Shop have One to Many relationship). In this case we add a foreign key of user_id (which is primary key of User table) to Shop table.
In your case I will suggest to only keep email and password in shopOwner_login table and add its foreign key to shop_details table. This way your data will be normalize and you will not have to make sure to maintain same data in multiple tables.

So,d query need to fetch the data looks like - SELECT a.ShopOwner_email Shop_Pass as EmailId,a.Shop_Pass as Password FROM shop_details as a left join shopOwner_login as b on b.ShopOwner_Id = a.ShopOwner_Id

Related

How to insert data in a table with foreign keys?

Good evening guys, I hope you all are well. I'm from latin america, so I hope you excuse my writing.
I have a database with 3 tables:
tenant (the users)
company (the user's companies)
company_x_tenant (which is generated by the relation many-to-many that tenant and company have)
so company_x_tenant has foreign keys (that are the primary keys that company and tenant have).
I'm trying to insert data into company_x_tenant table, but it's not allowed until I insert data into the tenant table and then the company table.
The problem is that I don't know if when I insert data into the company table, I should make a second insert to the company_x_tenant table. My boss says that the company_x_table exists because the website needs to prevent every user from deleting any company, so just one user can have complete access to it's own companies and delete or update them.
I hope I made myself clear :( Thank you!!
If only one user should be related to a company in this manner, there is no need for the company_x_tenant table; those are mostly for "many to many" relationships. Just put some "owner_user_id" field in the company table that is a foreign key to users.user_id.

Should a junction table for a many-to-many relationship have a surrogate primary key?

I'm working with mySQL and Laravel.
I have two tables: users and groups. Each has an id field and a name field.
I want users to be able to belong to many groups and vice versa; so I'm setting up a many to many relationship with a junction table: group_user.
My first thought is that it only needs two fields: user_id and group_id, both Foreign Keys.
But something tells me I might need/want an id for this table too. I've seen it both ways in code examples across the internet and I just want to know what I would be missing by not having it.
I just can't think of how I would actually make use of it; since the table is just to relate two other tables.
The artificial primary key can be very useful sometimes in your code. For example when you would like to delete a record you can use the same pattern as in the case of other tables.
You have to pass only one id instead of the parts of the compound key.

Register - Login Database Scheme

Here is what I want to ask:
I want to make a system to register patients so then they will be able to login. I have 3 type of users though.
admin (no need for registration)
doctor (standard number of doctors, no need for registration)
patient (they will be registered)
I want to keep more info for them than just id, username, password, email.
I am thinking of having more than 1 tables to do this and link them with primary and foreign keys:
1st table
accounts (it will store the login data)
Example:
acc_id(primary key)
acc_password
acc_username
acc_type
2nd table
doctors_extra_info
Example:
acc_id (foreign key)
doc_info_id (primary key)
doc_name
...
...
3rd table
patients_extra_info
Example:
acc_id (foreign key)
pat_info_id (primary key)
pat_name
...
...
4th table
admin_info
Example:
acc_id (foreign key)
admin_id (primary key)
admin_email
a. Which is the best way of doing this?
b. In the part of
registration, how to deal with primary and foreign keys? Two insert
commands in two different tables? [In order to have the same acc_id
in the account table and the extra info table]
c. At the login part,
I need to check the type of user and redirect (header(Location: ..);)
to a page? Is this the right way of doing it?
Any suggestions?
Thank you.
If you're using PHP then when you insert a record you can instantly retrieve the ID created using mysql_insert_id(). You then use this to create other records as your foreign key.
With regards to redirects, I'd simply get the user type from the database and then check the type of user and redirect to page required.
Generally though the tables you have created do not correlate properly. Remember the defining thing about the people using the system is that they are a person, and shouldn't be deined by their job role. They should have a account_type_id linking to another table. Otherwise you have three tables essentially holding the same information.
For example you should have your tables like this
User table
user_id
first_name
last_name
email
account_type_id*
Accounts type table
user_id
account_type_id*
account_type //e.g. patient, doctor, admin etc
This means now that you can easily extend the database with new tables, user access levels, new columns without having to duplicate the same column across three tables and so on. Try reading up on database normalization. A very good video from youtube is http://www.youtube.com/watch?v=fg7r3DgS3rA

which database type are good for arrays?

i want my database to support one company haveing many users how can i do that? exampleusers table(UID,username,password)company table(CID,companyname,usersthatistheownerofthecompany) how can i do that? what should i do ? should i make an array in php like 1241,423,4123 *uid's that will be inserted on usersthatistheownerofthecompany row ? or is there any better idea ?
If you want each user to have one (and never more) company, you should have :
user table
uid
username
...
company_id
company table
company_id
company_name
...
Then, user.company_id would be a foreign key, that references company.company_id.
And, then, you store :
One line in user for each user
Referencing the id of the right company for that user
which is the company_id of the right line in the company table.
And one line for each company in company
There is no user's related information stored in the company table -- and as each user "points" to a company, a company can have several users.
Storing data as an array like you suggested is definitely not quite a good idea -- just not the way a relational database works.
If each user can have several companies, and each comparny can have several users, you'll have to add a third table (a join table), that will indicate, for each user(s), to which company(ies) they are attached to :
user table
uid
username
...
company table
company_id
company_name
...
user_company table
uid
company_id
In this situation, there is no user-related stuff in the company table, and there is no company-related stuff in the user table : the link between those is in the user_company table.
Of course, user_company.uid will be a foreign-key to user.uid ; and user_company.company_id will be a foreign-key to company.company_id.
There is a better idea - it's called a cross-table join. What you do is you create a third table, which contains two columns. In those two columns you store the primary key of the tables you're connecting to eachother.
The idea is that you're creating a relation between a company and a user. In a relational database, relations are indicated between tables by using foreign keys.
Of course, this only applies when you want to connect multiple users to multiple companies (an "M-N" relationship). If you want to connect multiple users to a single company, simply add a column for the company id to the user.
Any relational database is a good way to go. Have a look at MS SQL or MySQL.

creating two rows at the same time with a foreign key relation

using php and mysql
I have two tables, a users table and a profiles table.
When a new account is created on my site the user and profile rows are created at the same time.
I want a foreign key to reference from the users table to the profiles table so that the users profile information will be stored in a seperate table.
My questions is, how do I ensure that the foreign key links to the correct profiles table.
The current way I would go about doing this is, after the new user and profiles row was created I would query the profiles in descending order to get the last row created and grab the profile id and insert it into the users table profile id foreign key reference. I'm sure that there must be a better way to do this though.. isn't there?
Create user with mysql_query();
Assuming the primary key of the users table is an auto-increment field, call mysql_insert_id();
Use that as the foreign key when you insert the profile.

Categories