用SQL语句写表? [英] SQL Statement to write table?

查看:61
本文介绍了用SQL语句写表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从事社交网络项目.只需将sql语句放在一起以创建表,并需要一些帮助,以了解如何完成这些表以及表之间的关系如何工作.

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(

只需三个表即可完成

到目前为止,我已经做到了.

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?

首先,您需要在drop和create语句中拼写兴趣相同.没错,兴趣爱好将通过ID链接回用户".您可能既需要带有兴趣ID和描述的兴趣表,又需要带有用户ID和兴趣ID的链接表.否则,您将列出很多重复的兴趣,每个用户一个.

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.

友谊表可能只是一个链接表,将两个用户ID链接在一起.

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

这告诉您Joe和Kris是朋友,他们俩都喜欢滑翔伞,尽管Joe也喜欢滑水.

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.

这篇关于用SQL语句写表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆