How to create Relational Table in sql database(wamp) [closed] - sql

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
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
Closed 9 years ago.
Improve this question
I have two tables named student_table(student_id,student_name),teacher_table(teacher_id,teacher_name). Now I want to create a relational table named teacher_student_table which column will be (id,student_id,teacher_id).
In student_table field named student_id is auto increment and in teacher_table field named teacher_id is auto increment.
And in teacher_student_table field named id is also auto_increment.Now how I can make this type of relational table? need help. I don't know how to create this in sql(wamp localhost)

You also want to have foreign-keys to the other tables.
CREATE TABLE teacher_student_table
(
id int NOT NULL AUTO_INCREMENT,
student_id int NOT NULL,
teacher_id int NOT NULL,
PRIMARY KEY (ID),
FOREIGN KEY (student_id) REFERENCES student_table(student_id),
FOREIGN KEY (teacher_id) REFERENCES teacher_table(teacher_id)
)

This is very basic SQL and you should easily have been able to look it up in your course litterature.
CREATE TABLE teacher_student_table
(
id int NOT NULL AUTO_INCREMENT,
student_id int NOT NULL,
teacher_id int NOT NULL,
PRIMARY KEY (ID)
FOREIGN KEY (student_id) REFERENCES student_table(student_id),
FOREIGN KEY (teacher_id) REFERENCES teacher_table(teacher_id)
)

Related

ORA-00904: : invalid identifier in Oracle Life database [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
Even i run this as script, i got error. first i didnt use 'wrent_id' and still got error.
CREATE TABLE want_renting (
'wrent_id' int NOT NULL PRIMARY KEY,
'property_id' int,
'client_id' int,
'agent_id' int,
'wrent_date' date,
'expired_date' date,
CONSTRAINT 'fk_property' FOREIGN KEY ('property_id') REFERENCES 'property'('property_id'),
CONSTRAINT 'fk_client' FOREIGN KEY ('client_id') REFERENCES 'clients'('client_id'),
CONSTRAINT 'fk_agent' FOREIGN KEY ('agent_id') REFERENCES 'agents'('agent_id'),
);
ORA-00904: : invalid identifier
Remove quotes from your query. Quotes is used to define a string and not during table creation.
CREATE TABLE want_renting (
wrent_id int NOT NULL PRIMARY KEY,
property_id int,
client_id int,
agent_id int,
wrent_date date,
expired_date date,
CONSTRAINT fk_property FOREIGN KEY (property_id) REFERENCES
property(property_id),
CONSTRAINT fk_client FOREIGN KEY (client_id) REFERENCES clients(client_id),
CONSTRAINT fk_agent FOREIGN KEY (agent_id) REFERENCES agents(agent_id)
);
And make sure you have all other tables referencing the foreign keys.

Trying to create table with 3 foreign keys on ms access [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm trying to create database where I have 3 tables to keep the data and the forth table to store the actual operations that processes the data. On the operations table I am using 3 foreign keys that referred to the other table but somehow I am able to create the table only with 2 foreign keys but once I add the third foreign key I get an error:
The Sql code for the operations table:
CREATE TABLE Operations
(
operation_Id AUTOINCREMENT PRIMARY KEY,
User_Id INTEGER NOT NULL CONSTRAINT FK_user_ID REFERENCES Users (User_Id),
course_Id INTEGER NOT NULL CONSTRAINT FK_course_ID REFERENCES Courses (Course_Id)
college_Id INTEGER NOT NULL CONSTRAINT FK_college_ID REFERENCES Colleges (college_Id)
)
and the tables with the relationships:
The Error
Note, Once I receive the error it point at the college_Id on the operations table
As #sticky-bit said, There is a missing , in your query right after Courses (Course_Id) see the correct one :
CREATE TABLE Operations
(
operation_Id AUTOINCREMENT PRIMARY KEY,
User_Id INTEGER NOT NULL CONSTRAINT FK_user_ID REFERENCES Users (User_Id),
course_Id INTEGER NOT NULL CONSTRAINT FK_course_ID REFERENCES Courses (Course_Id),
college_Id INTEGER NOT NULL CONSTRAINT FK_college_ID REFERENCES Colleges (college_Id)
)

#1215 SQL Cannot add foreign key constraint error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Here is my code. I can't figure out why I can't add the constraint.
Create table customer(
UserId Integer
)
CREATE TABLE Date(
DateID INTEGER,
User1ID INTEGER NOT NULL,
User2ID INTEGER NOT NULL,
Date CHAR(20) NOT NULL,
GeoLocation CHAR(20) NOT NULL,
BookingFee INTEGER NOT NULL,
CustomerRepresentative CHAR(20) NOT NULL,
Comments CHAR(200),
User1Ratings CHAR(20),
User2Ratings CHAR(20),
Primary Key (DateID),
Check ( User1Ratings IN (‘Excellent’, ‘VeryGood’, ‘Good’, ‘Fair’, ‘Poor’) ),
Check ( User2Ratings IN (‘Excellent’, ‘VeryGood’, ‘Good’, ‘Fair’, ‘Poor’) ),
FOREIGN KEY (User1ID) REFERENCES customer(UserID)
)
The most likely explanation for the behavior is that UserID column is not defined as the PRIMARY KEY or a UNIQUE KEY in the customer table
One fix for this would be to re-write the create for the customer table
For SQL Server:
CREATE TABLE customer
( UserId Integer NOT NULL
, CONSTRAINT PK_customer PRIMARY KEY CLUSTERED (UserId)
);
For MySQL:
CREATE TABLE customer
( UserId INTEGER NOT NULL PRIMARY KEY
);

SQL Error: ORA-00904: "MEBERSHIPID": invalid identifier [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I am creating a table whereI'm currently trying to add multiple foreign keys in Oracle
The table scripts is as follows
Create table Member
(
memeberID int Not null Primary Key,
membershipID int Not null,
group_id int not NULL,
Dutycode int not null,
MemberRole varchar(255),
name varchar(255),
last_joined date,
DOB date,
address varchar(255),
CONSTRAINT fk_DutCode FOREIGN KEY (Dutycode)
REFERENCES RaceManagementDuty(Dutycode),
CONSTRAINT fk_GrMemeber FOREIGN KEY (group_id)
REFERENCES Group_member(group_id),
CONSTRAINT fk_Meberships FOREIGN KEY (mebershipID)
REFERENCES Membership(mebershipID)
)
Wandering where I'm going wrong?
Its just a typo error . Change mebershipID to membershipID
CONSTRAINT fk_Meberships FOREIGN KEY (membershipID)
REFERENCES Membership(membershipID)
There is no column mebershipID in table and so throws error

name already used by an existing constraint [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
Create Table Resources_user
(
Resources_userID INTEGER NOT NULL,
Resources_ID INTEGER NOT NULL,
User_ID INTEGER NOT NULL,
Data Accessed DATE,
CONSTRAINT PK_Resources_user PRIMARY KEY (Resources_userID),
constraint fk_Resources_user1 Foreign key (Resources_ID ) references Resources,
constraint fk_Resources_user2 Foreign key (User_ID) references User1);
Create table Staff_Position
(
Staff_Position_ID INTEGER NOT NULL,
Position_ID INTEGER NOT NULL,
User_ID INTEGER NOT NULL,
CONSTRAINT PK_Staff_Position PRIMARY KEY (Staff_Position_ID),
Constraint fk_Staff_Position1 foreign key (Position_ID) references position,
Constraint fk_Staff_Position2 foreign key (User_ID) references User1);
Thank you the problem been solved, I realized that i need to number my foreign keys for avoiding the error of repeating .
Your foreign key constraints are incomplete. You did not specify a field. This:
, constraint fk_Resources_user Foreign key (Resources_ID ) references Resources
should be something like this:
, constraint fk_Resources_user Foreign key (Resources_ID )
references Resources (resources_id)
Some Oracle error messages are misleading. This is one of them.
Edit Starts Here
You also have duplicate constraint names for your foreign keys. You will likely get a message indicating such once the other problems are resolved.