SQL Statement to write table?
working on a social networking project. just have to put together the sql statement to create the tables and need a little help with how this would be done, and how the relationships would work between the tables.
drop table users;
drop table intrest;
drop table friendships;
create table users(
id INT,
Fname char(15),
Lname char(15),
email char(20),
street char(15),
state char(2),
zip INT,
age INT,
gender char (2),
phone INT,
Password char(15),
primary key (id);
开发者_高级运维create table Instrests(
id INT,
create table friendships(
this can be done with just three tables
so far i have this done.
drop table users;
drop table intrest;
drop table friendships;
create table users(
id INT,
Fname char(15),
Lname char(15),
email char(20),
street char(15),
state char(2),
zip INT,
age INT,
gender char (2),
phone INT,
User_password char(15),
primary key (id)
foriegn key (intrest_id) references intrest(id)
);
create table Intrests(
id INT,
description char(30),
Primary key (id),
foreign key (users_id) references users(id)
);
create table User_intrest(
foreign key (user_id) references user(id),
foreign key (intrest_id) references intrest(id)
);
create friendships(
User_1_id INT,
User_2_id INT,
status Char(20),
foreign key (user_id) references user(id)
);
It looks like you're asking how to create the Interests and Friendships tables? And how they relate?
First, you'll need to spell Interests the same in the drop and create statement. You're right that Interests will link back to the Users by the ID. You'll probably want both an Interests Table, with an InterestID and description, and a linking table, with both the UserID and InterestID. Otherwise, you'll have lots of duplicate interests listed, one for each user.
The friendships table may be just a linking table, linking two userIDs together.
Try to imagine the data you need, and create the tables based on that:
User - 1, name - Joe, other info...
User - 2, name - Kris, other info..
User - 3, name - Lee, other info...
Interest - 1, name - reading
Interest - 2, name - parasailing
Interest - 3, name - skimboarding
UserInterest - User 1, Interest 2
UserInterest - User 1, Interest 3
UserInterest - User 2, Interest 2
Friendship - User 1, User 2
That tells you that Joe and Kris are friends and they both like parasailing, although Joe also likes skimboarding.
This doesn't tell you how to create the tables, but perhaps it will point you in the right direction. If this is a homework assignment, and it looks like it, you still want to do the work yourself.
You will need a an intermediate table between Users and Interests if you plan on having a many to many relationship between the two tables.
Also for Friendship, I would recomend a intermediate table between Friendship and Users as well, with that table having a link to a "Friendship Type" table to identify the friendship/relationship.
And if ThursdayGeek is correct and this is a homework assignment then I will stop here with my suggestions because there is more to the intermediate tables that you need to learn about before you implement them.
drop table users;
drop table intrest;
drop table friendships;
create table users(
id INT,
Fname char(15),
Lname char(15),
email char(20),
street char(15),
state char(2),
zip INT,
age INT,
gender char (2),
phone INT,
User_password char(15),
primary key (id)
foriegn key (intrest_id) references intrest(id)
);
create table Intrests(
id INT,
description char(30),
Primary key (id),
foreign key (users_id) references users(id)
);
create table User_intrest(
foreign key (user_id) references user(id),
foreign key (intrest_id) references intrest(id)
);
create friendships(
User_1_id INT,
User_2_id INT,
status Char(20),
foreign key (user_id) references user(id)
);
sorry not really good at this, anyways, this is what I have done for the sql statements so far.
精彩评论