Oracle SQL crazy subqueries across 4 tables - sql

Here are the relations:
CREATE TABLE employee (
fname varchar2(15) not null,
minit varchar2(1),
lname varchar2(15) not null,
ssn char(9),
bdate date,
address varchar2(30),
sex char,
salary number(10,2),
superssn char(9),
dno number(4),
primary key (ssn),
foreign key (superssn) references employee(ssn),
foreign key (dno) references department(dnumber)
);
CREATE TABLE department (
dname varchar2(15) not null,
dnumber number(4),
mgrssn char(9) not null,
mgrstartdate date,
primary key (dnumber),
unique (dname),
foreign key (mgrssn) references employee(ssn)
);
CREATE TABLE dept_locations (
dnumber number(4),
dlocation varchar2(15),
primary key (dnumber,dlocation),
foreign key (dnumber) references department(dnumber)
);
CREATE TABLE project (
pname varchar2(15) not null,
pnumber number(4),
plocation varchar2(15),
dnum number(4) not null,
primary key (pnumber),
unique (pname),
foreign key (dnum) references department(dnumber)
);
CREATE TABLE works_on (
essn char(9),
pno number(4),
hours number(4,1),
primary key (essn,pno),
foreign key (essn) references employee(ssn),
foreign key (pno) references project(pnumber)
);
Q: Find the names and addresses of all employees who work on at least one project located in Houston but whose department has no location in Houston.
Here is my query:
select fname, minit, lname, address
from employee
where dno=
(select dnumber from dept_locations where dlocation<>'Houston' and dnumber=
(select dno from employee where ssn=
(select essn from works_on where pno=
(select pnumber from project where plocation='Houston'))))
But it did not compile and returned this error:
ORA-01427: single-row subquery returns more than one row
Please help!

select fname, minit, lname, address
from employee
where dno in
(select dnumber from dept_locations where dlocation<>'Houston') and ssn in
(select essn from works_on where pno in
(select pnumber from project where plocation='Houston'))))

Your call "where dno=" expects a single result.
try
where dno in (select...)

select
fname,
minit,
lname,
address
from
employee
where
exists (
select null
from works_on join project on (works_on.pno = project.pnumber)
where works_on.essn = employee.ssn and
not exists (
select null
from dept_locations
where dept_locations.dlocation = project.plocation))
Other comments:
The lack of consistency in table and column naming is very irritating to work with. DNO and DNUMBER?
Using a social security number as a key value is a stupid error. It's stupid because people's SSN's are private and should be protected, and it's an error because it is not immutable.

Related

SQL create 2 tables with constraints

Starting with first table: create the two tables with constraints
CREATE TABLE department
(
depid varchar2(3) CONSTRAINT PKdepid PRIMARY KEY,
dname varchar2(10) NOT NULL
);
First table is created.
Starting second table:
CREATE TABLE employee
(
eid number CONSTRAINT PKEID PRIMARY KEY,
ename varchar2(10),
depid varchar2(3)
CONSTRAINT FKDEPID
FOREIGN KEY REFERENCE dep(depid),
designation varchar2(10),
salary number CHECK (salary > 10000),
doj date
);
CREATE TABLE employee
(
eid number CONSTRAINT PKEID PRIMARY KEY,
ename varchar2(10),
depid varchar2(3)
CONSTRAINT FKDEPID
FOREIGN KEY REFERENCE dep(depid),
designation varchar2(10),
salary number CHECK (salary > 10000),
doj date
)
ERROR at line 1:
ORA-02253: constraint specification not allowed here
And in second condition applied
CREATE TABLE employee
(
eid number CONSTRAINT PKEID PRIMARY KEY,
ename varchar2(10),
depid varchar2(3)
CONSTRAIN FKDEPID
FOREIGN KEY REFERENCE dep(depid),
designation varchar2(10),
salary number CHECK (salary > 10000),
doj date
);
CREATE TABLE employee
(
eid number CONSTRAINT PKEID PRIMARY KEY,
ename varchar2(10),
depid varchar2(3)
CONSTRAIN FKDEPID
FOREIGN KEY REFERENCE dep(depid),
designation varchar2(10),
salary number CHECK (salary > 10000),
doj date
)
ERROR at line 1:
ORA-00907: missing right parenthesis
I just wanted to create table
You shouldn't specify foreign key in an inline foreign key definition, just references (not reference as you currently have). Also, note your statement references the table dep while your other table is in fact called department:
CREATE TABLE employee (
eid NUMBER CONSTRAINT pkeid PRIMARY KEY,
ename VARCHAR2(10),
depid VARCHAR2(3) CONSTRAINT fkdepid REFERENCES department(depid), -- Here!
designation VARCHAR2(10),
salary NUMBER CHECK(salary>10000),
doj DATE);

Database Design in PostgreSQL

I am designing a database for school.
Requirements
Professors have an SSN, a name, an age, gender, a rank, and a research specialty
Professors work in exactly one department
Departments have a department number, a department name, and a main office
Departments must have one professor (known as the chairman) who runs the department
My current schema for "Professors" and "Departments"
CREATE DOMAIN SSN AS CHAR(11) CONSTRAINT validSSN CHECK (VALUE ~* "^\d{3}-\d{2}-\d{4}$");
CREATE TABLE Persons (
person_id INT PRIMARY KEY AUTO_INCREMENT,
ssn SSN NOT NULL CONSTRAINT mustBeDifferent UNIQUE,
name VARCHAR(255) NOT NULL,
age INT NOT NULL CONSTRAINT nonnegativeAge CHECK(age >= 0),
gender CHAR(1) CONSTRAINT mustBeMaleOrFemale CHECK(gender = "M" OR gender = "F") NOT NULL,
);
CREATE TABLE Professors (
ranking INT NOT NULL CONSTRAINT positiveRanking CHECK(ranking > 0),
research_specialty VARCHAR(255) NOT NULL,
department_id INT NOT NULL REFERENCES Departments(department_number)
) INHERITS(Persons);
CREATE TABLE Departments (
department_number INT PRIMARY KEY AUTO_INCREMENT,
department_name VARCHAR(255) NOT NULL,
main_office VARCHAR(255) NOT NULL,
chairman_id INT REFERENCES Professors(person_id)
);
I have a foreign key in the "Professors" table that references the "Departments" table and a foreign key in the "Departments" table that references the "Professors" table.
Is there any way that I could go about resolving this issue?
I see a few problems:
professors should not inherit from persons, but reference it with a foreign key.
Otherwise you cannot guarantee uniqueness of the primary key.
Don't store the age in persons, but the birthday.
Otherwise your data will grow invalid pretty quickly.
If there is no length limit dictated by the application, don't use varchar(255), but use text.

Foreign key missing parenthesis

CREATE TABLE EMPLOYEE (
EID CHAR(3) NOT NULL PRIMARY KEY,
ENAME VARCHAR2(50) NOT NULL,
JOB_TYPE VARCHAR2(50) NOT NULL,
MANAGER CHAR(3) FOREIGN KEY REFERENCES EMPLOYEE(EID),
HIRE_DATE DATE NOT NULL,
DNO INTEGER FOREIGN KEY REFERENCES DEPARTMENT(DNO),
COMMISSION DECIMAL(10,2),
SALARY DECIMAL(7,2) NOT NULL,
);
CREATE TABLE DEPARTMENT (
DNO INT NOT NULL PRIMARY KEY,
DNAME VARCHAR(50),
LOCATION VARCHAR(50) DEFAULT('NEW DELHI')
);
in creation of the employee table
this is giving me an error of right parenthesis
and the department table is already created
You have an extra comma in the line
SALARY DECIMAL(7,2) NOT NULL,
Delete that comma and the Employee table should be created.
You need to Create the Department Table first to use one of its
Columns as FOREIGN KEY.
Also, check your database. There might already be a Department Table. To avoid getting that error when the table needed is already created, use the keyword IF NOT EXISTS
CREATE TABLE IF NOT EXISTS Department(
DNO INT NOT NULL PRIMARY KEY,
DNAME VARCHAR(50),
LOCATION VARCHAR(50) DEFAULT('NEW DELHI')
);
The error is caused by the trailing comma, but you have other issues:
The FOREIGN KEY is not needed for an inline reference.
You need to define the tables in the right order.
So . . .
CREATE TABLE DEPARTMENT (
DNO INT NOT NULL PRIMARY KEY,
DNAME VARCHAR(50),
LOCATION VARCHAR(50) DEFAULT('NEW DELHI')
);
CREATE TABLE EMPLOYEE (
EID CHAR(3) NOT NULL PRIMARY KEY,
ENAME VARCHAR2(50) NOT NULL,
JOB_TYPE VARCHAR2(50) NOT NULL,
MANAGER CHAR(3) REFERENCES EMPLOYEE(EID),
HIRE_DATE DATE NOT NULL,
DNO INTEGER REFERENCES DEPARTMENT(DNO),
COMMISSION DECIMAL(10,2),
SALARY DECIMAL(7,2) NOT NULL
);
Here is an example of it working.

How can I display the number of pledges made by each donor in ORACLE sql?

Display the number of pledges made by each donor. Include the donor ID, first name, last name, and number of pledges.
Below is what I have so far, but it isn't working. It counts the total donors, which makes sense, but I need to count how many pledges each donor made.
select iddonor, firstname, lastname, (select distinct count(iddonor) from dd_pledge) as pledges from dd_donor;
Below are the tables needed:
CREATE TABLE DD_Donor (
idDonor number(4),
Firstname varchar2(15),
Lastname varchar2(30),
Typecode CHAR(1),
Street varchar2(40),
City varchar2(20),
State char(2),
Zip varchar2(9),
Phone varchar2(10),
Fax varchar2(10),
Email varchar2(25),
News char(1),
dtentered date DEFAULT SYSDATE,
CONSTRAINT donor_id_pk PRIMARY KEY(idDonor) );
CREATE TABLE DD_Pledge (
idPledge number(5),
idDonor number(4),
Pledgedate DATE,
Pledgeamt number(8,2),
idProj number(5),
idStatus number(2),
Writeoff number(8,2),
paymonths number(3),
Campaign number(4),
Firstpledge char(1),
CONSTRAINT pledge_id_pk PRIMARY KEY(idPledge),
CONSTRAINT pledge_idDonor_fk FOREIGN KEY (idDonor)
REFERENCES dd_donor (idDonor),
CONSTRAINT pledge_idProj_fk FOREIGN KEY (idProj)
REFERENCES dd_project (idProj),
CONSTRAINT pledge_idStatus_fk FOREIGN KEY (idStatus)
REFERENCES dd_status (idStatus));
You should join the tables and count.
select d.idDonor, d.Firstname, d.Lastname, count(*) count
from DD_Donor d
join DD_Pledge p
on d.idDonor=p.idDonor
group by (d.idDonor, d.Firstname, d.Lastname)
Please note that this only shows a count for donors with a pledge. Those without a pledge will not be shown.
Does this work better?
select iddonor, firstname, lastname, count(dd_pledge.iddonor)
from dd_donor
inner join dd_pledge on dd_doner.iddoner = dd_pledge.iddoner
group by firstname,lastname;

SQL Server - Adding Primary and Foreign Keys

I have one query that creates several tables, listed below, and a second query that is supposed to add Primary and Foreign keys to each. However, when I try executing my query, I'm greeted with the following error messages:
Msg 1776, Level 16, State 0, Line 2
There are no primary or candidate keys in the referenced table 'DEPARTMENT' that match the referencing column list in the foreign key 'FK__EMPLOYEE__dno__25869641'.
Msg 1750, Level 16, State 0, Line 2
Could not create constraint. See previous errors.
I'm not sure what this means, as I have researched both primary and foreign keys. Can anyone lead me in the right direction as to how to solve this issue? Thanks! :)
CREATE TABLE EMPLOYEE
(
fname varchar(100),
minit char(1),
lname varchar(100),
ssn char(9),
bdate date,
addr varchar(100),
sex char(1),
salary int,
super_ssn char(9),
dno int
);
CREATE TABLE DEPARTMENT
(
dname varchar(100),
dnumber int,
mgr_ssn char(9),
mgr_start_date date
);
CREATE TABLE DEPENDENTS
(
essn char(9),
dependent_name varchar(100),
sex char(1),
bdate date,
relationship varchar(100)
);
CREATE TABLE DEPT_LOCATIONS
(
dnumber int,
dlocation varchar(100)
);
CREATE TABLE PROJECT
(
pname varchar(100),
pnumber int,
plocation varchar(100),
dnum int
);
CREATE TABLE WORKS_ON
(
essn char(9),
pno int,
hrs float
);
ALTER TABLE EMPLOYEE ADD PRIMARY KEY(ssn);
ALTER TABLE EMPLOYEE ADD FOREIGN KEY(super_ssn) REFERENCES EMPLOYEE(ssn);
ALTER TABLE EMPLOYEE ADD FOREIGN KEY(dno) REFERENCES DEPARTMENT(dnumber);
ALTER TABLE DEPARTMENT ADD PRIMARY KEY(dnumber);
ALTER TABLE DEPARTMENT ADD FOREIGN KEY(mgr_ssn) REFERENCES EMPLOYEE(ssn);
ALTER TABLE DEPT_LOCATIONS ADD PRIMARY KEY(dnumber, dlocation);
ALTER TABLE DEPT_LOCATIONS ADD FOREIGN KEY(dnumber) REFERENCES DEPARTMENT(dnumber);
ALTER TABLE PROJECT ADD PRIMARY KEY(pnumber);
ALTER TABLE PROJECT ADD FOREIGN KEY(dnum) REFERENCES DEPARTMENT(dnumber);
ALTER TABLE WORKS_ON ADD PRIMARY KEY(essn, pno);
ALTER TABLE WORKS_ON ADD FOREIGN KEY(essn) REFERENCES EMPLOYEE(ssn);
ALTER TABLE WORKS_ON ADD FOREIGN KEY(pno) REFERENCES PROJECT(pnumber);
ALTER TABLE DEPENDENTS ADD PRIMARY KEY(essn, dependent_name);
ALTER TABLE DEPENDENTS ADD FOREIGN KEY(essn) REFERENCES EMPLOYEE(ssn);
Primary key column should be non null able. Table definition should have not null for all PKs.
CREATE TABLE EMPLOYEE (
fname varchar(100),
minit char(1),
lname varchar(100),
ssn char(9) not null,
bdate date,
addr varchar(100),
sex char(1),
salary int,
super_ssn char(9),
dno int
);
CREATE TABLE DEPARTMENT (
dname varchar(100),
dnumber int not null,
mgr_ssn char(9),
mgr_start_date date
);
CREATE TABLE DEPENDENTS (
essn char(9) not null,
dependent_name varchar(100) not null,
sex char(1),
bdate date,
relationship varchar(100)
);
CREATE TABLE DEPT_LOCATIONS (
dnumber int not null,
dlocation varchar(100) not null
);
CREATE TABLE PROJECT (
pname varchar(100),
pnumber int not null,
plocation varchar(100),
dnum int
);
CREATE TABLE WORKS_ON (
essn char(9) not null,
pno int not null,
hrs float
);
ALTER TABLE EMPLOYEE ADD PRIMARY KEY(ssn);
ALTER TABLE DEPARTMENT ADD PRIMARY KEY(dnumber);
ALTER TABLE EMPLOYEE ADD FOREIGN KEY(super_ssn) REFERENCES EMPLOYEE(ssn);
ALTER TABLE EMPLOYEE ADD FOREIGN KEY(dno) REFERENCES DEPARTMENT(dnumber);
ALTER TABLE DEPARTMENT ADD FOREIGN KEY(mgr_ssn) REFERENCES EMPLOYEE(ssn);
ALTER TABLE DEPT_LOCATIONS ADD PRIMARY KEY(dnumber, dlocation);
ALTER TABLE DEPT_LOCATIONS ADD FOREIGN KEY(dnumber) REFERENCES DEPARTMENT(dnumber);
ALTER TABLE PROJECT ADD PRIMARY KEY(pnumber);
ALTER TABLE PROJECT ADD FOREIGN KEY(dnum) REFERENCES DEPARTMENT(dnumber);
ALTER TABLE WORKS_ON ADD PRIMARY KEY(essn, pno);
ALTER TABLE WORKS_ON ADD FOREIGN KEY(essn) REFERENCES EMPLOYEE(ssn);
ALTER TABLE WORKS_ON ADD FOREIGN KEY(pno) REFERENCES PROJECT(pnumber);
ALTER TABLE DEPENDENTS ADD PRIMARY KEY(essn, dependent_name);
ALTER TABLE DEPENDENTS ADD FOREIGN KEY(essn) REFERENCES EMPLOYEE(ssn);