I would like to save user data in my database.
There is common data about the user account (nickname, password, etc.) but also data like firstname, name, age, location, ...
How can I manage my data base? Should I create different tables? One containing common user data and another containing all the other data?
This is a design choice, and it basically depends on how much information you usually need, and how many extra fields you have.
Option 1: Keep them in the same table, if its not too much or you usually need all the data.
Option 2: Create a User Profile table, that contains the user data that its related to the person and not the account.
create one single table.
CREATE TABLE `admin`
(
`User_Name` varchar(60) NOT NULL,
`Password` varchar(60) NOT NULL,
'firstname' varchar(60) not null,
'Age' int(11) Not null,
'Location' varchar(50) NOT NULL,
PRIMARY KEY (`User_Name`)
)
ENGINE=InnoDB DEFAULT CHARSET=latin1;
Create 2 tables:
1. userProfile
create table userProfile(
UserID int primary key auto_increment(1,1),
firstname varchar(50),
age int(11),
location varchar(50));
2.userAccounts:
create table userAccounts(
ID int primary key auto_increment(1,1),
UserID int(11),
UserName varchar(50),
Password varchar(50));
there is a relation between Table1(UserID) and Table2(UserID).
Related
I am new to MySQL, so I'll explain by example.
I have 2 tables:
Admins(
id int auto_increment not null,
primary key(id)
);
Users(
admin_id int not null,
id varchar(255) not null,
password varchar(255) not null
);
I basically create an entry for a admin, then I want the admin to be able to add users that are tied to his ID, so then I can also read all users tied to that certain admin (the id and password parameters, to be exact)
I cannot figure out the syntax for how to do this, could anyone provide some help? Maybe there is a way to just write all that data straight in the admin table somehow so I don't have to use 2 tables?
I use PHP to do everything, by the way
I have two tables:
CREATE TABLE users(
userID int primary key not null auto_increment,
username varchar(16),
passcode varchar(16),
email varchar(50) not null
);
CREATE TABLE favorites(
userID int not null,
favID1 varchar(50) not null,
favID2 varchar(50) not null,
favID3 varchar(50) not null,
favID4 varchar(50) not null,
favID5 varchar(50) not null,
favID6 varchar(50) not null,
favID7 varchar(50) not null,
favID8 varchar(50) not null,
favID9 varchar(50) not null,
favID10 varchar(50) not null,
favID11 varchar(50) not null,
favID12 varchar(50) not null,
FOREIGN KEY fk1(userID) REFERENCES users(userID)
);
And I would like to get the contents of the favorites table with just the username from the users table, what would the statement for it look like? I'm fairly new to SQL and databases, so apologies if this is trivial. Every other resource I've looked at doesn't seem to relate to what I want to do.
Your database design has some problems. Instead of maintaining separate columns for each favorite, you should modify the favorites table such that a single record stores one, and only one, user-favorite relationship:
CREATE TABLE favorites (
userID int not null,
favID varchar(50) not null,
FOREIGN KEY fk1(userID) REFERENCES users(userID)
);
Now, if you want to report the favorite IDs for a given user, you need only use a basic join, e.g.
SELECT
u.userID,
u.username,
GROUP_CONCAT(f.favID ORDER BY f.favID) AS favIDs
FROM users u
LEFT JOIN favorites f
ON u.userID = f.userID
GROUP BY
u.userID,
u.username;
Perhaps the biggest problem with your current design of the favorites table is that it only admits up to 12 favorite IDs. Should your system ever have the need to support more than that, the table itself would have to be modified, i.e. you would need a DDL change. With my suggested design, you would only need to add more records/data, which is a DML change.
lets say I have an account object in my application, which currently represented as:
CREATE TABLE Account (
accountId int NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
PRIMARY KEY (accountId)
);
Now, Account object need to also have Solution field...and Status have 4 different possible values:
Solution1, Solution2, Solution3, Solution4
What would be the right way to represent it in the database?
Account can have few statuses, and status can have few accounts...
So at first I thought create in the db table of Solutions and than have another table to hold the relationship, but its seems too complicated for a field that have only 4 possible values...
Create a junction table to represent the relationships between accounts and solutions:
CREATE TABLE account_solution (
accountId int NOT NULL,
solutionId int NOT NULL
PRIMARY KEY (accountId, solutionId)
)
For your solution table, since there are only 4 values, you might be able to take advantage of MySQL's enum type, e.g.
CREATE TABLE solution
solutionId int NOT NULL PRIMARY KEY,
status ENUM('Solution1', 'Solution2', 'Solution3', 'Solution4')
);
You can use set Mysql SET type
CREATE TABLE Account (
accountId int NOT NULL AUTO_INCREMENT,
name varchar(255) NOT NULL,
status set('Solution1','Solution2','Solution3','Solution4') NOT NULL,
PRIMARY KEY (accountId)
);
And if you want to select a specific status
SELECT *
FROM `Account`
WHERE FIND_IN_SET( 'Solution2', `status` ) >0
I need to create a huge table called worker (id_worker is the primary key) and lots of other columns like name, date_of_birth, etc.
I also need to create several other tables that inherit this one (all the different kinds of workers from the company). They all have the attributes from worker (name, date_of_birth) and also the same primary key (id_worker).
I read that I cannot do this in MySQL because it does not support inheritance. How do I do it then?
CREATE TABLE workers(id INT(11) NOT NULL, name VARCHAR(64), date_of_birth DATE);
CREATE TABLE worker_type(id INT(11) NOT NULL, name VARCHAR(64));
CREATE TABLE workers_types(worker_id INT(11) NOT NULL, woker_type_id INT(11) NOT NULL);
SELECT workers.name, worker_type.name FROM workers
LEFT JOIN workers_types ON workers_types.worker_id = workers.id
LEFT JOIN worker_type ON worker_type.id = workers_types.worker_type_id;
Sorry not proofed and bad naming...
So first up I'm not sure if this is a double post or not because I don't know how the exact approach or feature is called and if it even exist.
I know that MySQL has a feature called joins
My plan is to link two MySQL tables in relation 1:n one is t_user the other one t_event.
t_user:
CREATE TABLE t_user (
uId INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(25) NOT NULL,
...
)
t_event:
CREATE TABLE t_event (
eId INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(40) NOT NULL,
date DATETIME NOT NULL,
members ???,
...
)
I want the users to "subscribe" to the events and get stored in the members column as a list (?). This would be no problem if only one user would subscribe to one event. But I have no idea how to setup the t_event table to store more than one user and how to query for all the events a user has "subscribed" for.
This is usually done via third table:
CREATE TABLE t_eventsusers (
eId INT(6),
uId INT(6)
)