CREATE TABLE login(
name varchar(200),
email varchar(200) primary key,
count int,
depart varchar(200),
type varchar(1),
password varchar(40));
CREATE TABLE qsstudent(
department varchar(200),
qs int,
email varchar(200),
type varchar(1),
qscorrect int,
qsdone int,
qswrong int ,
FOREIGN KEY (email) REFERENCES login(email));
CREATE TABLE qsteacher(
email varchar(200),
department varchar(200),
qsentered int,
type varchar(1),
FOREIGN KEY (email) REFERENCES login(email));
When I run the query below, it cannot find the login.depart column. I have tried doing this several times but it is not running. I don't think that there is an error in the foreign key
SELECT qsteacher.qsentered
FROM login,qsteacher
WHERE login.depart=qsteacher.department
AND qsstudent.email=login.email;
but still the error persists.
cannot find the login.depart column
This is the exact thing your query can't find:
SELECT qsteacher.qsentered
FROM login,qsteacher
WHERE login.depart=qsteacher.department
AND qsstudent.email=login.email; //<-- you didn't select qsstudent
If you want to use qsstudent on this query you need add it to the FROM
FROM login,qsteacher,qsstudent
I recommend to do something like this with INNER JOIN:
SELECT T3.qsentered
FROM login as T1
INNER JOIN qsstudent as T2 on T2.email=T1.email
INNER JOIN qsteacher as T3 on T1.depart=T3.department
and If you want to select by specific user just add this WHERE to the end:
WHERE T1.email = 'user_email'
Firstly your primary key should be an id, not an email, for multiple reasons, like uniqueness and index. So I will add an id to all of your tables which should auto increment with every record. And Your foreign key should be an id as well so I will change that.
CREATE TABLE login(
id int NOT NULL AUTO_INCREMENT primary key,
name varchar(200),
email varchar(200),
count int,
depart varchar(200),
type varchar(1),
password varchar(40));
CREATE TABLE qsstudent(
id int NOT NULL AUTO_INCREMENT primary key,
department varchar(200),
qs int,
email varchar(200),
type varchar(1),
qscorrect int,
qsdone int,
qswrong int ,
FOREIGN KEY (login_id) REFERENCES login(id));
CREATE TABLE qsteacher(
id int NOT NULL AUTO_INCREMENT primary key,
email varchar(200),
department varchar(200),
qsentered int,
type varchar(1),
FOREIGN KEY (login_id) REFERENCES login(id));
Now for the query that you are trying to run from what I can tell:
SELECT q.qsentered FROM qsteacher q
JOIN login l ON l.id = q.login_id
JOIN qsstudent s ON s.login_id = l.id
WHERE s.department = q.department
Please comment exactly what it is you are trying to query so I can better answer your question. It is not clear what you are looking for.
Related
I had made a student table link with report table by foreign key to join them.
CREATE TABLE Students (
st_id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(st_id),
student_name varchar(100),
student_id varchar(9),
email varchar(100),
pass varchar(100),
tp_id INT,
FOREIGN KEY (tp_id) REFERENCES Training_programs(tp_id)
);
CREATE TABLE Report (
id INT NOT NULL AUTO_INCREMENT,
PRIMARY KEY(id),
st_id INT,
FOREIGN KEY (st_id) REFERENCES Students(st_id),
status VARCHAR(100)
);
But my problem is when i insert status that i can't insert st_id in the same time because i just insert status in my web. So that i try to auto insert st_id from Students to Report after insert status report:
CREATE TRIGGER thistrigger BEFORE INSERT
ON Students FOR EACH ROW
UPDATE Report, Students SET Report.st_id = Students.st_id WHERE Report.st_id IS NULL;
But it can't work. So that do every ones have any solution with my problem?
Hi guys so i dont know how to make it as varchar because is want to input BSIT as my primary key and i cant figure it out how to make it as characters please help me this is my code btw
create table CourseTBL
(
CourseID int primary key,
Course varchar(50),
Description varchar(50),
)
It's better to create a different CODE column to use WHERE condition on it, because key is something meaningless and is intended for referential integrity only. For PK add a constraint as an another element of CREATE TABLE statement. Like this:
create table CourseTBL
(
CourseID varchar(50),
Course varchar(50),
Description varchar(50),
primary key( CourseID )
)
My preference is to add course code column
Create table CourseTBL
(
CourseID int primary key,
CourseName varchar(50),
CourseCode varchar(10),
CourseDescription varchar(50),
)
I have two table in a database. First one is
CREATE TABLE persons
(
P_Id int NOT NULL,
LastName varchar(30) NOT NULL,
FirstName varchar(30),
Address varchar(200),
City varchar(20),
PRIMARY KEY (P_Id)
)
and the second one is
CREATE TABLE Orders
(
O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
PRIMARY KEY (O_Id),
FOREIGN KEY (P_Id) REFERENCES Persons(P_Id)
)
My question is there any way to update value of FOREIGN KEY (P_Id) automatically in Orders table once primary key (P_Id) in Persons table get updated.
Thank You.
your second table should be
CREATE TABLE Orders
(
O_Id int NOT NULL,
OrderNo int NOT NULL,
P_Id int,
PRIMARY KEY (O_Id),
FOREIGN KEY (P_Id) REFERENCES Persons(P_Id),
ON UPDATE CASCADE
)
This means that "ON UPDATE CASCADE" will do the same thing when id of the parent is updated.
The mentioned can be easily achieved by Using After Update Trigger on the Table persons.
Please do..
Create Trigger trgUpdatePersonsPIDinOrders On dbo.persons After Update
AS Begin Declare #OP_ID Int,#NP_ID Int Select #OP_ID =
Deleted.P_ID From Deleted Select #NP_ID = Inserted.P_ID From
Inserted If #OP_ID <> #NP_ID Then
Update DBO.Orders Set P_ID = #NP_ID Where P_ID = #OP_ID End
May It will Help You
Thanks
This may be confusing, but basically what I have is the following tables:
CREATE TABLE Employee(
eid int NOT NULL,
fname varchar(20),
lname varchar(20),
zip int,
PRIMARY KEY (eid));
CREATE TABLE Customer(
cid int NOT NULL,
fname varchar(20),
lname varchar(20),
street varchar(20),
city varchar(20),
zip int,
PRIMARY KEY (cid));
CREATE TABLE Orders(
oid int NOT NULL,
rdate date,
sdate date,
cid int NOT NULL,
eid int NOT NULL,
PRIMARY KEY (oid),
FOREIGN KEY (cid) REFERENCES Customer(cid),
FOREIGN KEY (eid) REFERENCES EMPLOYEE(eid));
One of the questions for my assignment is the following:
Create and execute a query that lists employee information together with the number or orders they have processed
Can someone help get me started? I literally don't even know where to begin...
SELECT ???
FROM ???
WHERE ???
SELECT e.fname, e.lname, e.zip,
OrdersProcessed = COUNT(o.oid)
FROM Orders o
INNER JOIN Employee e ON o.eid = e.eid
GROUP BY e.fname, e.lname, e.zip
So I'm having a small problem with creating a table. The problem is in my attempt to create a foreign key to another table. I'm currently using MYSQL2008 Management R2 express, so no designer. Heres my two tables
use teckDB;
CREATE TABLE inventory
(
primId int NOT NULL PRIMARY KEY,
prodName VarChar(255),
quantity int,
prodCost MONEY,
prodDesc VARCHAR(255)
);
CREATE TABLE orderTB
(
primId INT NOT NULL PRIMARY KEY,
orderId INT NOT NULL,
created date,
prodId INT,
);
Those two tables ran without a problem. When create the thrid one however causes this error message.
Msg 1769, Level 16, State 1, Line 3 Foreign key 'orderTB' references
invalid column 'orderTB' in referencing table 'CustomerTB'. Msg 1750,
Level 16, State 0, Line 3 Could not create constraint. See previous
errors.
On the thrid table of....
CREATE TABLE CustomerTB
(
primId INT NOT NULL PRIMARY KEY,
orderId INT, FOREIGN KEY (orderTB) REFERENCES orderTB(orderId),
fName VARCHAR(50),
lName VARCHAR(50),
addLN1 VARCHAR(255),
addLN2 VARCHAR(255),
addCity VARCHAR(255),
addPro VARCHAR(255),
addPST VARCHAR(7)
);
try this
FOREIGN KEY (iparent_id) REFERENCES innodb_parent (iparent_id)
I think this should help to solve your query
http://blog.sqlauthority.com/2008/09/08/sql-server-%E2%80%93-2008-creating-primary-key-foreign-key-and-default-constraint/
You have an extra comma after "orderId INT", and you have got the foreign key syntax wrong.
This should work:
CREATE TABLE CustomerTB
(
primId INT NOT NULL PRIMARY KEY,
orderId INT REFERENCES orderTB(orderId),
fName VARCHAR(50),
lName VARCHAR(50),
addLN1 VARCHAR(255),
addLN2 VARCHAR(255),
addCity VARCHAR(255),
addPro VARCHAR(255),
addPST VARCHAR(7)
);
tested on SQLFiddle here