sqlplus using of sub query - sql

I have 3 tables, and have to find out the applicant number, name and total number of position each applicant applied for.
CREATE TABLE APP (
appNum varchar2(10) not null,
appName varchar2(70),
constraint applicant_Pkey primary key (appNum)
);
CREATE TABLE POS (
posNum varchar2(10) not null,
posStartOfferDt date not null,
constraint pos_Pkey primary key (posNum, posStartOfferDt)
);
CREATE TABLE APPLICATION (
appcnPosNum varchar2(10) not null,
appcnPosStOffrDt date not null,
appcnAppNum varchar2(10) not null,
appcnDt date,
constraint application_Pkey primary key (appcnPosNum, appcnPosStOffrDt, appcnAppNum),
constraint application_Fkey1 foreign key (appcnPosNum, appcnPosStOffrDt) references POSITION(posNum, posStartOfferDt),
constraint application_Fkey2 foreign key (appcnAppNum) references APPLICANT(appNum)
);
I have tried using sub query, natural join but all not working out for me.

SELECT appNum, appName, COUNT(*)
FROM applicant
JOIN application ON applicant.appNum = application.appcnAppNum
GROUP BY appNum, appName

Related

"Incorrect syntax near 'DESCRIBE'. [41,1]" No other context, can't find syntax error

New to SQL, can't figure out what is wrong in my given code. all it says is:
Incorrect syntax near 'DESCRIBE'. [41,1]
I have tried taking off the semi-colons. I really just don't know what it wants from me.
Here is my code. Anything helps, thank you!
-- Write the query to create the 4 tables below.
CREATE TABLE client (
id INT NOT NULL IDENTITY(1,1),
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
dob DATE NOT NULL,
PRIMARY KEY (id),
CONSTRAINT (full_name) UNIQUE (first_name, last_name)
);
CREATE TABLE employee (
id INT NOT NULL IDENTITY(1,1),
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
dob DATE NOT NULL,
date_joined DATE NOT NULL,
CONSTRAINT (full_name) UNIQUE (first_name, last_name),
PRIMARY KEY (id)
);
CREATE TABLE project (
id INT NOT NULL IDENTITY(1,1),
cid INT NOT NULL,
name VARCHAR(255) NOT NULL,
notes TEXT,
UNIQUE (name),
FOREIGN KEY (cid) REFERENCES client(id)
);
CREATE TABLE works_on (
eid INT NOT NULL,
pid INT NOT NULL,
start_date DATE NOT NULL,
PRIMARY KEY (eid, pid),
FOREIGN KEY (eid) REFERENCES employee(id),
FOREIGN KEY (pid) REFERENCES project(id)
);
-- Leave the queries below untouched. These are to test your submission correctly.
-- Test that the tables were created
DESCRIBE client;
DESCRIBE employee;
DESCRIBE project;
DESCRIBE works_on;
-- Test that the correct foreign keys were created
SELECT TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME,REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME
FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE REFERENCED_TABLE_SCHEMA = 'grade';
For MariaDB (and MySQL) the correct syntax for IDENTITY(1,1), is AUTO_INCREMENT, and CONSTRAINT names are not enclosed in (). Any column that is defined as AUTO_INCREMENT must also be declared as a PRIMARY KEY (this is only an issue with the project table). So your CREATE TABLE commands should look like this:
CREATE TABLE client (
id INT NOT NULL AUTO_INCREMENT,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
dob DATE NOT NULL,
PRIMARY KEY (id),
CONSTRAINT full_name UNIQUE (first_name, last_name)
);
CREATE TABLE employee (
id INT NOT NULL AUTO_INCREMENT,
first_name VARCHAR(255) NOT NULL,
last_name VARCHAR(255) NOT NULL,
dob DATE NOT NULL,
date_joined DATE NOT NULL,
CONSTRAINT full_name UNIQUE (first_name, last_name),
PRIMARY KEY (id)
);
CREATE TABLE project (
id INT NOT NULL AUTO_INCREMENT,
cid INT NOT NULL,
name VARCHAR(255) NOT NULL,
notes TEXT,
PRIMARY KEY (id),
UNIQUE (name),
FOREIGN KEY (cid) REFERENCES client(id)
);
CREATE TABLE works_on (
eid INT NOT NULL,
pid INT NOT NULL,
start_date DATE NOT NULL,
PRIMARY KEY (eid, pid),
FOREIGN KEY (eid) REFERENCES employee(id),
FOREIGN KEY (pid) REFERENCES project(id)
);
Demo on dbfiddle

Database Create table Primary key not found

I have two tables i'm trying to create with foreign keys.
The statements are below
Book_Copy Table
CREATE TABLE book_copy (
bid NUMBER(15) NOT NULL,
isbn VARCHAR(15) NOT NULL,
firstavaib VARCHAR(9) NOT NULL,
outservice VARCHAR(9) NULL,
CONSTRAINT primary_key PRIMARY KEY ( bid,isbn ),
FOREIGN KEY ( isbn )
REFERENCES book_catalog ( isbn )
);
History table
CREATE TABLE history (
bid NUMBER(15) NOT NULL,
mid NUMBER(10) NOT NULL,
FOREIGN KEY ( mid )
REFERENCES member ( mid ),
datetaken VARCHAR(9) NOT NULL,
datereturn VARCHAR(9) NULL,
FOREIGN KEY ( bid )
REFERENCES book_copy ( bid ),
CONSTRAINT primary_key PRIMARY KEY ( bid, datetaken )
);
Now when I run it the first one says table created but i get the following for the second.
CREATE TABLE history (
bid NUMBER(15) NOT NULL,
mid NUMBER(10) NOT NULL,
FOREIGN KEY ( mid )
REFERENCES member ( mid ),
datetaken VARCHAR(9) NOT NULL,
datereturn VARCHAR(9) NULL,
FOREIGN KEY ( bid )
REFERENCES book_copy ( bid ),
CONSTRAINT primary_key PRIMARY KEY ( datetaken )
)
*
ERROR at line 1:
ORA-02270: no matching unique or primary key for this column-list
There are several errors in the statements above and also not all the relevant information was made available.
Your table book_copy has a foreign key reference to the table book_catalog via the isbn column:
CREATE TABLE book_copy (
...
FOREIGN KEY ( isbn )
REFERENCES book_catalog ( isbn )
To proceed, I took the liberty to add a simple one:
CREATE TABLE book_catalog (
isbn VARCHAR(15) NOT NULL,
CONSTRAINT pk_ct PRIMARY KEY (isbn));
And similar, they history table also references another table member via the mid column:
CREATE TABLE history (
...
FOREIGN KEY ( mid )
REFERENCES member ( mid ),
I also create that one in order to proceed:
CREATE TABLE MEMBER (
mid NUMBER(10) NOT NULL,
CONSTRAINT pk_mem PRIMARY KEY (mid));
The error that you get above means what it says: There is no such primary key or unique key on the other table that you try to reference, hence a value cannot be uniquely identified and hence the integrity of the data not verified. So the database stops you from doing so in the first place.
The culprit is that you specify the PRIMARY KEY of your table book_copy to be bid,isbn while your foreign key reference in the history table only references the bid:
CREATE TABLE history (
...
FOREIGN KEY ( bid )
REFERENCES book_copy ( bid ),
So you ask the database to check the integrity on something that is not uniquely identifiable. In order to solve this you have to expand your foreign key of the history table to include the isbn column as well:
So the full DDL looks like this:
CREATE TABLE book_catalog (
isbn VARCHAR(15) NOT NULL,
CONSTRAINT book_catalog_pk PRIMARY KEY (isbn));
CREATE TABLE MEMBER (
mid NUMBER(10) NOT NULL,
CONSTRAINT member_pk PRIMARY KEY (mid));
CREATE TABLE book_copy (
bid NUMBER(15) NOT NULL,
isbn VARCHAR(15) NOT NULL,
firstavaib VARCHAR(9) NOT NULL,
outservice VARCHAR(9) NULL,
CONSTRAINT book_copy_pk PRIMARY KEY ( bid,isbn ),
FOREIGN KEY ( isbn )
REFERENCES book_catalog ( isbn )
);
CREATE TABLE history (
bid NUMBER(15) NOT NULL,
mid NUMBER(10) NOT NULL,
isbn VARCHAR2(15) NOT NULL,
FOREIGN KEY ( mid )
REFERENCES member ( mid ),
datetaken VARCHAR(9) NOT NULL,
datereturn VARCHAR(9) NULL,
FOREIGN KEY ( bid, isbn )
REFERENCES book_copy ( bid, isbn ),
CONSTRAINT history_pk PRIMARY KEY ( bid, datetaken )
);
There is another problem in your DDL above. In Oracle, constraint names are globally unique! That means that whatever you put after CONSTRAINT has to be unique. In the case above you use the same name primary_key for both your tables which will fail with an ORA-02264: name already used by an existing constraint error on creating the history table. You will see that I have given the primary key constraints on the tables more meaningful names.
Err: Foreign key(BID) references Book_Copy(BID)
The primary key of Book_Copy is (BID, DateTaken) not just (BID)
You need a primary or unique constraint in the target table referenced in a FK of another table.
1) Add DateTaken to you FK definition on HISTORY or replace the FK with a CHECK constraint something like (EXISTS (select * from book_copy c where c.BID = BID)

ORA-00957 duplicate column name error, when trying to reference the same primary key with 3 foreign keys

I'm having problems with creating tables:
CREATE TABLE EMPLOYEE
(
employee_id NUMBER(5) NOT NULL UNIQUE,
position VARCHAR2(100) NOT NULL,
name VARCHAR2(255) NOT NULL,
salary NUMBER(6) NOT NULL
CONSTRAINT employee_pk PRIMARY KEY (employee_id)
);
CREATE TABLE PROJECT
(
project_id NUMBER(5) NOT NULL UNIQUE,
name VARCHAR(100) NOT NULL,
budget NUMBER(6) NOT NULL,
consultant_leader NUMBER(5) NOT NULL,
developer_leader NUMBER(5) NOT NULL,
project_leader NUMBER(5) NOT NULL,
CONSTRAINT project_pk PRIMARY KEY (PROJECT_ID),
CONSTRAINT fk_leader
FOREIGN KEY (consultant_leader, developer_leader, project_leader)
REFERENCES EMPLOYEE (employee_id, employee_id, employee_id)
);
In the last section, when I try to reference the employee's table employee_id, I'm getting ORA-00957. I think it's because the 3 different leader type foreign key references the same employee_id, but as far as I know, it should not be a problem. Is the syntax wrong?
Your immediate problem is that you need three foreign key relationships, not one with three columns.
But, there is no need to declare a primary key as being unique. So, I would recommend:
CREATE TABLE EMPLOYEE (
employee_id NUMBER(5) NOT NULL PRIMARY KEY,
position VARCHAR2(100) NOT NULL,
name VARCHAR2(255) NOT NULL,
salary NUMBER(6) NOT NULL
);
CREATE TABLE PROJECT (
project_id NUMBER(5) NOT NULL PRIMARY KEY,
name VARCHAR(100) NOT NULL,
budget NUMBER(6) NOT NULL,
consultant_leader NUMBER(5) NOT NULL,
developer_leader NUMBER(5) NOT NULL,
project_leader NUMBER(5) NOT NULL,
CONSTRAINT fk_leader FOREIGN KEY (consultant_leader)
REFERENCES EMPLOYEE (employee_id),
CONSTRAINT fk_leader FOREIGN KEY (developer_leader)
REFERENCES EMPLOYEE (employee_id),
CONSTRAINT fk_leader FOREIGN KEY (project_leader)
REFERENCES EMPLOYEE (employee_id)
);
You don't need to put the PRIMARY KEY constraint in-line, of course. The advantage of declaring it separately is that you can give the constraint a name to your liking.
I think you should create three distinct FK: FK_Consultant, FK_developer, FK_projleader

SQL ORA-02256: number of referencing columns must match referenced columns

these are my tables :
CREATE TABLE EMPLOYEE(
Emp_id number(4),
Emp_name varchar2(30),
Emp_gender varchar2(1),
Status varchar2(30),
Years_service number(4),
Primary Key (emp_id)
);
CREATE TABLE ACTIVITY(
Act_id number(4),
Description varchar2(30),
Category_code varchar2(1),
Primary Key(Act_id)
);
CREATE TABLE ALLOCATION(
Emp_id number(4) NOT NULL,
Act_id number(4) NOT NULL,
Hourly_rate number(5,2) NOT NULL,
Primary Key (Act_id, Emp_id),
Foreign Key (Act_id) REFERENCES ACTIVITY,
Foreign Key (Emp_id) REFERENCES EMPLOYEE,
CONSTRAINT CK_ALLOCATION_RATE CHECK(Hourly_rate > 0 and Hourly_rate<300)
);
CREATE TABLE ACTION(
Week_no number(2) NOT NULL,
Hrs_worked number(4,1) NOT NULL,
Act_id number(4) NOT NULL,
emp_id number(4) NOT NULL,
Primary Key (Week_no, Act_id, emp_id),
Foreign Key (Act_id) References Allocation,
Foreign Key (emp_id) References Allocation
);
Table employee, activity and allocation are created perfectly. but when i try to create table action and referencing the foreign key to table allocation, it says: ORA-02256: number of referencing columns must match referenced columns.
When using references I think it is a good idea to include the columns in both tables, even if the syntax makes this unnecessary (the default is to the primary key).
You have a composite primary key, so you need a composite key for the foreign reference:
CREATE TABLE ACTION (
Week_no number(2) NOT NULL,
Hrs_worked number(4,1) NOT NULL,
Act_id number(4) NOT NULL,
emp_id number(4) NOT NULL,
Primary Key (Week_no, Act_id, emp_id),
Foreign Key (Act_id, emp_id) References Allocation(Act_id, emp_id)
);

Strange problems while setting foreign key

Database: Oracle 10.1 on os:xp2002
I am setting foreign key in employee table but it I get an error
no matching unique or primary key for this column-list
I am using following queries for creating the tables.
Department table:
create table department(
d_name varchar2(10) not null,
d_no_of_employees number(4));
Employee table:
create table employee(
e_id number(4) ,
e_name varchar2(30) not null,
e_f_name varchar2(30) not null,
e_desg varchar2(20) not null,
e_address varchar2(50) not null,
e_phone_no number(12) not null,
e_salary number(10) not null,
e_house_rent number(6) not null,
e_conv_allow number(6) not null,
e_email varchar2(50) not null unique,
d2_name varchar2(10) not null,
e_hire_month number(2) not null,
e_hire_year number(4) not null,
constraint e_id_pk primary key(e_id),
constraint d2_name_fk foreign key(d2_name) references department(d_name))
;
Any solution please.
the foreign key must be a primary or unique key in the other table.
Make d_name PRIMARY KEY of Department table.
create table department(
d_name varchar2(10) not null,
d_no_of_employees number(4),
constraint d_name_pk primary key(d_name));
And then create the Employee table.
From what you've posted you don't seem to have a primary key on the department table. Something like:
create table department(
d_name varchar2(10) not null,
d_no_of_employees number(4)
constraint department_pk primary key(d_name));
Or after creation:
alter table department
add constraint department_pk primary key(d_name)
...