Creating a contacts table for users in a MySQL database [closed] - mysql

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm a newbie.. not sure if this is the right place to post this, so please forgive me if it isn't.. i wanted to find out how or what would be the best approach when it comes to making a table for contacts in a database.. Here is what i have :
I have a table for users, this table contains user info like usernames and passwords,etc.. i want to make a program that will allow these users to add contacts,now i don't want to create a contacts table for each user,i would like to have a contacts table which contains all contacts and be able to reference them using an id .. Help please

You have a table of users and a table of contacts:
User - UserID, Username, Pw... etc.
Contacts - ContactID, name, phone, info...etc.
For a user to have multiple contacts and for multiple users to have the same contact, you need to create a bridge table to handle the many to many relationship. In this table you'll reference the primary keys (the UserID, and ContactID) from the tables above.
UserContacts - UserID, ContactID. (this can be a composite primary key)

You need to have a table with name of your choice..let's say Contacts
You can have this structure
ContactId (Primary Key)
ContactName
UserId
Etc..
You can start with above structure and improvise..
I recommend this article..
http://www.datanamic.com/support/lt-dez005-introduction-db-modeling.html

Table creation sample code:
CREATE TABLE contact(
contact_id VARCHAR(20),
contact_name VARCHAR(20),
user_id VARCHAR(20),
password VARCHAR(20)
);
data insertion sample code:
INSERT INTO contact(contact_id,contact_name,user_id,password)
VALUES("A123","Daniel","dan2015","password");

Related

What is the better solution for permissions of a user to a group? [duplicate]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I want to design database for a web app in which user can access particular tabs based on the permissions given to a role.
What I have done so far is I created two tables USER_TABLE and USER_ROLES.
USER_TABLE has below fields:
id (primary key)
user_name
password
first_name
last_name
created_date
role_id_fk (foreign key)
USER_ROLES has below fields:
id (primary key)
role_name (e.g. ADMIN, TAB1_USER, TAB2_USER)
created_date
Here, the user having role_name "ADMIN" can see all the tabs, other users can access specific tabs only.
My question is do I need to create a table USER_PERMISSIONS having foreign key in USER_ROLES table with below fields:
id (primary key)
permission_type (ALL, TAB1, TAB2....)
or should I manage this at my code level? What would be the cons and pros of both approaches?
As krokodilko wrote in his comment, it depends on the level of flexibility you need.
I have implemented role based permissions for one of my clients as follows:
User (user id (PK), user name (unique), password (salted and hashed!), first name, last name, phone etc')
Role (role id (PK), role name (unique), role description)
Permission (permission id (PK), permission name (unique)) - the tabs / screens / actions goes here
User To Role (user id, role id) - PK is both columns combined
Role to Permission (role id, permission id) - PK is both columns combined
But my requirement was to be as flexible as possible, and it is a system that is still growing (6 years and counting).
I guess a lot of applications can have the user to role as a one to many relationship, instead of a many to many like in my case, but I wouldn't go hard coding permissions or role to permissions in any application.
Further explanation: Role based security database design on What the # do I know?

SQL many to many relation, multiple tables [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
Hey so i have been doing alot of reading and i am finding many conflicting ways to link my tables. hoping someone can help me put the foreign keys in the correct places.
tables -
customers: customer_id(primary), customer_name ;
employee: employee_id(primary), employee_name ;
appointments: appointment_id(primary), appointment_date ;
inventory: inventory_id(primary), item ;
so i have primary key for each table but i need to link 1 customer to the appointments table and multiple employees to the appointments tables.
the inventory table must link to the appointments tables.
Here is an example, customer sets an appointment then employee(s) fills appointment. during that appointment the customer buys an item(s).
it is sufficent enough to just show the item with the appointment. the item does not need to link to the customer.
thank you for help. i was thinking i needed to make a 5th table to fill with all the keys but im really unsure and the tables have alot more information in them than i posted above. would be alot of trial an error to then see if not work one of the ways i need.
You should add customer_id foreign key to the appointments table
You should create table appointment _employee:id (primary key),appointment_id(foreign key), employee_id(foreign key)
You should create table appointment_inventory:appointment_id, inventory_id

Does it make sense to create a uniqueID for this MySQL table in a database? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I’m currently trying to design my table structures for a made up database. The database will have three separate tables and track the populations of cities for 10 years. After every year, population figures will be added for each city in the database. Here is how I’ve laid out my three tables so far:
Cities (city_id, city_name, city_abbrv)
Year (year_id, year)
Stats (year_id, city_id, population)
I’m worried about not having a unique identifier in my Stats table. With 10 cities, the year data will be the same for 10 entries. Once I enter multiple years of data, the city_id will be reused. In my research on this site I’ve read that having a unique ID for every table is not required but the book I’m using to learn database design (while brief) never mentions that this is okay. I would like to know the best way to design a database that receivers data entries for the same group of things on a daily/weekly/monthly/yearly schedule. Should I add in a unique_id column to my Stats table? Or would this be a wasted column? Thanks for the help!
First of all you need each of those tables to have the column id as primary key.
A primary key is used to uniquely identify a table row. A primary key cannot be NULL since NULL is not a value. So basically those columns will be unique yes.
Question: Why you need primary keys in your tables?
Answer:
If you are looking to select data from different tables you are opting for join so you need keys to use for that join.
If you want your
table to be clustered, you need some kind of a primary key.
Side Note: You may need to get familiar with indexing columns, see advantages of indexing.
Cities (id, city_name, city_abbrv) //with id as primary key
Year (id, year) //with id as primary key
Stats (id, year_id, city_id, population) //with id as primary key
//And year_id and city_id as foregin key connected by their respective ids
If you are still beginner with MYSQL see the W3school tutorial for SQL primary keys.

Storing an array of arrays in mysql [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I am making a table of a products like different types of vehicles: cars, trucks etc. They are made by an array of countries and each country has an array of makers, like Japan has Toyota, Honda, etc and US has Ford, GM etc. Once the database is built, I need to do operations like "select all vehicles made by Toyota".
What is the best way to store that?
The data structure is not necessarily the same as your array structure. The key word is "normalisation": design your tables in a way that avoids redundancies and make sure that each record in a table contains exactly the information that is relevant to describe the subject of that particular table.
A possible structure could be:
create table vehicles(
vid int auto_increment primary key,
void int,
vname nvarchar(32),
vtype int,
vmotor varchar(32), ...)
create table oem (
oid int auto_increment primary key,
oname nvarchar(32),
countryid int, ... )
The column void of table vehicles references the primary key oid of the oem (original equipment manufacturers) table.
A typical query can be the following:
select * from vehicles where
exists (select 1 from oem where oid=void and countryid=7)
countryid is just an integer key referencing yet another table (not listed here) containing country names etc.. Assuming that record 7 of the country table contains 'Japan' then the above query will list all vehicles made in Japan.
Or - coming back to your original example -
select * from vehicles where
exists (select 1 from oem where oid=void and oname='Toyota')
would list all vehicles of that particular manufacturer.
This little example is just the starting point for you to understand `normalisation'. Like Marc B already said: Study the concept for yourself and you will be able to answer your question yourself. Here is another example based link that might be helpful: http://db.grussell.org/section008.html .
Why not just have a table called Automobiles,
and then rows like Car, Model, Company,Country
and then you can just
SELECT * FROM Automobiles WHERE Company = 'Toyota' AND Country = 'Japan'

How to store "wishlist" in database? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I am thinking of creating a "wishlist". Each user will be able to purchase wishes in a shop. How do I store the wishlist? I am thinking of creating an user table and a wishlist table.
The structure of user table:
Columns: id, username, password etc
Columns: id, wish, price, quantity etc
user id is the primary key for user table and its a foreign key in wishlist table. However, when I come to think of it, my wishlist table will have duplicate items for each user, won't it?
Is there a better way to store the wishlist things?
I am using mysql. Thanks
You should also have another table to store the purchases:
Purchases
Id
UserId
WishId
PurchaseDate
Users
Id
Name
Password
Wishes
Id
Wish
Price
Quantity
Every time a user purchases a wish, you create a record in the Purchases table, and decrement the Quantity count for that wish.
It is a 1 to n relationship.
Asuming you have a User table with a user_id as primary key then
create a table Wish which has user_id as foreign key.
Add the attributes you want to this second table and violá.
Luis
You should have another table for wishes (wish id, price, description etc), and you should have a joining table (user id, wish id, quantity, quoted price etc). This table will allow you to have a many-to-many relationship between users and wishes.
(Further to my comment... see? You have two different suggestions already. Ultimately you have to decide what is best, and ideally your business/project requirements should guide you.)
EDIT: make that three different suggestions
You essentially need to know just a few things to add information to a wishlist. Here's how I have it in one of my client projects:
Wishlist
wishlist_id
user_id
product_id
created_date_time
It would make sense to have wishlists only for authenticated users, so ensure that you are taking care of that.