I have a database consist of 3 tables each table indicate one user like this:
Admin table 'User 1'.
Staff table 'User 2'.
Student table 'user 3'.
I want to implement a common table between them called 'POST'
WHERE each user can post content to the post table content column,
however I need to Identify the type of posters ' the one who posted the post ' so I'm adding a column
user_type to the table POST
the column user_type should hold the ID of the USER ' Admin, Staff, Student '
I'm in a case where I want to be able to refer to this column ( user_type) in the POST table to multiple table columns Admin table, Staff table, Student table. So
I Can identify the USER who posted the post.
All these kind of users are persons that you need to store in one parent table called "Users" or "Persons" from which the parent's table are "User 1", "User 2", "User 3"...
This is called inheritance in data modeling.
Thus you can join the post table to the father table.
As an example :
CREATE TABLE T_PERSON_PRS (PRS_ID INT IDENTITY PRIMARY KEY, PRS_NAME...)
CREATE TABLE T_PERSON_ADMIN_PAM (PRS_ID INT PRIMARY KEY FOREIGN KEY T_PERSON_PRS (PRS_ID), ... )
CREATE TABLE T_PERSON_STAFF_PSF (PRS_ID INT PRIMARY KEY FOREIGN KEY T_PERSON_PRS (PRS_ID), ... )
CREATE TABLE T_PERSON_STUDENT_PSD (PRS_ID INT PRIMARY KEY FOREIGN KEY T_PERSON_PRS (PRS_ID), ... )
CREATE TABLE T_POST_PST (PST_ID INT IDENTITY PRIMARY KEY, PRS_ID INT PRIMARY KEY FOREIGN KEY T_PERSON_PRS (PRS_ID), ...)
Related
I made a database called users in which it has a name, password and phone number, but I want this user to have another table that stores current_capital, current_percentage and current_date of that user but I do not know how to do it, some advice ?
I have the telephone number in the user database as the primary key.
It is advisable to have a database for each user? or better to these attributes (save capital_actual, current_percent and current_date) I add an attribute identifier_user that would be the phone number and put everything in a second table? (as records table)
Thanks.
When using databases, you want to have a data model with a fixed set of tables and columns. You then add data by adding rows to tables. Not tables to databases, or columns to tables, but rows to tables.
From what I can tell, you have two tables connected using a foreign key constraint:
create table users (
user_id int auto_increment primary key,
phone_number varchar(255),
password varchar(255), -- should be encrypted
name varchar(255)
);
create table user_daily (
user_daily_id int auto_increment primary key,
user_id int,
current_date date,
current_capital numeric(10, 2),
current_percent numeric(5, 4),
constraint fk_user_daily_users foreign key (user_id) references users(user_id)
);
Note that both tables have auto-incrementing primary keys.
In SQL i have a table for users. On my website i want people be able to make groups consisting of 2 or more users, but i have no idea how to store that. People can be a part of multiple groups at the same time. If i'd make a table groups, how could i store it's members in that table?
You would have a table groups and one called userGroups. These would look like:
create table groups (
groupId int auto_increment primary key,
groupName varchar(255)
);
create table userGroups (
userGroupId int auto_increment primary key,
userId int not null,
groupId int not null,
foreign key (userId) references users(userId),
foreign key (groupId) references groups(groupId)
);
What you're describing is called a many-to-many relationship, and it requires a third table:
UserTable: UserID (unique), UserName, etc.
GroupTable: GroupID (unique), GroupName, etc.
UserGroups: UserGroupID (unique), UserID, GroupID
With a third table holding the primary key(pk)) of a group and a pk of a person. These columns are the pk of the table. You can have other to
Columns too, e.g. date joined or whatever. You have one row in this table per person/group.
It's a common concept/pattern so make sure you learn and understand it.
I have created 2 table in 2 different databases. First database name is user which contains userDetails table, which have id as a primary key and user_name, and my second database is customer which have 1 table called as customerDetails, which have 1 id as a primary key and customer name and one view of above user table which contains id of that user table and name.
So what i want to do is, creating a foreign key of that view in customerDetails table, so that i can access user table from customer database through view. I don't know how to achieve this, as i am new to database concepts please anyone can get me out of this.
Whole scenario is as follow,
> Database Name : user
> Table Name : userDetails
> Fields : id userName
>
> Database Name : customer
> View Name : user_view
> Fields : id userName
>
> Database Name : customer
> View Name : customerDetails
> Fields : id custName
i want in last table that is in customerDetails last column as a foreign key from view. How can i achieve this?
Views are not related to foreign keys as much as getting to your data as mentioned in comments by your peers. The below uses a Junction Table to intersect users and companies, enforcing a Foreign Key constraint between databases (not a bad idea for shared info between databases).
The Junction Table is many-to-many, and hooks users and companies together.
Schema:
create schema userDB;
create table userDB.userDetails
( id int auto_increment primary key,
userName varchar(100) not null
);
create schema customerDB;
create table customerDB.customerDetails
( id int auto_increment primary key,
custName varchar(100) not null
);
create table customerDB.userCustomerJunction
( -- a many-to-many mapping
id int auto_increment primary key,
userId int not null,
custId int not null,
unique key (userId,custId), -- no dupes allowed
foreign key `ucj_2_user` (userId) references userDB.userDetails(id),
foreign key `ucj_2_cust` (custId) references customerDb.customerDetails(id)
);
Test it:
insert customerDB.customerDetails(custName) values ('Exxon Mobil'); -- id 1
insert customerDB.userCustomerJunction(userId,custId) values (1,7); -- FK Failure
-- above line generates an error 1452 as expected
insert userDB.userDetails(userName) values ('Kelly'); -- id 1
insert customerDB.userCustomerJunction(userId,custId) values (1,1); -- success, FK's satisfied
Remember that the user and company are separate entities and to interface the two would require something that ties them together. A Junction table is a fantastic place to put a column such as effectiveRights or something. It would denote what the user can do, such as insert, update, delete, view, blacklist, etc.
Creating a view between user and company is simply like any join, but in this case it would be between databases with the whichDB. in front of the table name. The view is materialized and manifested in the physical tables. So as the physical rules, the physical has the FK's in force (data integrity). And the addition of an effectiveRights column will assist you in determining what each user and company can do together: such as, yes, this user has certain rights to this company info, etc. With a rights bitmark, or separate columns for rights, all in the Junction table. For an example of Junction tables, see this Answer of mine.
I have an access database which contains 23 tables.
There is one Main table which houses all test equipment and contains
columns headers 4-26. The other 22 tables are used to house the equipment related to a section (4-26).
What I've been trying to do is setup relationships between each section table to the Main table's section column header. If there is an "X" within column 4 of the main table then add that column automatically to the section 4 table as well.
This is an overkill since the data is already in the main table. However, I wanted to reframe from having to read through the main table and separate equipment to its section in my applications run time.
Problems:
I've already tried setting up the relationships, however, my "relationship" type is "indeterminate" which I believe is because the tables are not referencing each others primary key column.
I created a section column in each of the section tables to denote a value 4-26. I then set that as my primary key, but primary keys (as far as i know) must be unique so that will not work.
Any suggestions?
Any list of columns of a table can form a foreign key in it. The referencing table and list of colums just has to reference a list of columns that forms a candidate key in the refereced table.
In SQL a PRIMARY KEY declares a UNIQUE NOT NULL column list. This is called a superkey. A superkey that contains no smaller superkey is a candidate key. Primary keys don't matter to integrity & normalization, candidate keys do. You can call some candidate key the primary key. (So an SQL PRIMARY KEY actually declares a superkey, what you might call a primary superkey, which might be a candidate key.)
A foreign key says that if you replace the column names in the referencing column list by their values in a row of the referencing table then you have to be able to make the same list of values by replacing the column names in the the referenced column list by their values in a row of the referenced table. If that's the case and there isn't already a chain of foreign keys from referencing table to the referenced tables that says the same thing then declare a foreign key. (The DBMS will use that fact for integrity and optimization.)
PS Tables (whether the values of base tables or query results) represent application relationships/associations. Certain tools, methods and presentations call foreign keys "relationships".
Establish the primary key in the main table which will include the section values and use this in the section tables as both the primary key of that table and a foreign key back to the main table.
create table TestEquipment(
ID AutoNumber primary key,
Section int not null,
..., -- other fields common to test equipment
constraint CK_TestEquipmentSection( Section between 4 and 26 ),
constraint UQ_TestEquipmentLink unique( ID, Section )
);
As ID will be unique all by itself, why create a unique constraint with ID and the section value? So we can refer to the combination with FKs in the section tables:
create table Section4(
ID int not null primary key,
Section int not null,
..., -- other fields for section 4 entries
constraint CK_Section4 check( Section = 4 ),
constraint FK_Section4TE foreign key( ID, Section )
references TestEquipment( ID, Section )
};
create table Section5(
ID int not null primary key,
Section int not null,
..., -- other fields for section 5 entries
constraint CK_Section5 check( Section = 5 ),
constraint FK_Section5TE foreign key( ID, Section )
references TestEquipment( ID, Section )
};
The other section tables will be similarly defined. When a piece of test equipment is entered, the key value is generated and the section number established by creating an entry in TestEquipment. A entry can then only be made in the section table with the matching section value. That is, if any entry is made in TestEquipment with an ID value of 1001 and Section value of 4, the only section table that can contain an entry with an ID value of 1001 will be the Section4 table. Any attempt to enter it into any of the other section tables will be rejected.
This is about the simplest way to do what it would appear you are trying to do.
I'm having a table of user information.
Primary key: userID int
I want to map a contact list for each user so I build another table with 2 fields:
userId, contactId both int
Now I want that both fields will be together a primary key
so will be able to save several unique contacts id for each user Id referred to first table, and of course do an inner join with on contactID with userID
How do I do that in mysql(using phpMyAdmin)?
Query examples are also possible of course