Putting a UPR page into a set of tables with SQL - sql

I am trying to put a UPR pages into a set of tables for a database, so far I have come up with the following tables
UPR:
CODE,
NAME,
STRUCTURES,
APPENDICES,
DOCUMENTS
STRUCTURES:
STR_NAME,
STR_CODE
APPENDICES:
APP_NAME,
APP_CODE
DOCUMENTS:
DOC_NAME,
DOC_CODE
these tables are going to be used to store each part of the UPR's how ever I am having trouble trying to work out how to get all of the tables to link up so that they will all work together and have one way of pulling all relavent appendices, documents and structures together for one UPR code. I have a feeling I might be being blind here but cannot see to work out a way of doing this, any help would be hugely appreciated. Many thanks.

CREATE TABLE UPR
(
CODE NUMBER(9) NOT NULL,
NAME VARCHAR2(50)
)
;
ALTER TABLE UPR ADD CONSTRAINT PK_UPR
PRIMARY KEY (CODE)
USING INDEX
;
-- -------------------------------------
CREATE TABLE STRUCTURES
(
ID NUMBER(9) NOT NULL,
UPR NUMBER(9) NOT NULL,
SEQUENCE NUMBER(9) NOT NULL,
NAME VARCHAR2(50) NOT NULL
)
;
ALTER TABLE STRUCTURES ADD CONSTRAINT PK_STRUCTURES
PRIMARY KEY (ID)
USING INDEX
;
ALTER TABLE STRUCTURES ADD CONSTRAINT FK_STRUCTURES_UPR
FOREIGN KEY (UPR) REFERENCES UPR (CODE)
;
The structure of 'Appendix' and 'Document' would then follow the same pattern as 'Structures.
Does that help ?
edit
To build a complete UPR you would then do something like
SELECT S.NAME, D.SEQUENCE FROM STRUCTURES S WHERE CODE = :SomeCode
UNION SELECT A.NAME, D.SEQUENCE FROM APPENDIX A WHERE A.UPR = S.CODE
UNION SELECT D.NAME, D.SEQUENCE FROM DOCUMENT D WHERE D.UPR = S.CODE
ORDER BY 2
By the way ? What is a UPR? :)

Related

Insert into table1 using data from staging_table1 and table2, while using staging_table1 to get the data from table2

Goal: Insert all data into a table from staging table. Each piece of data in the staging table has 2 names which can be found in a separate table. By using the 2 two names, I want to find their respective IDs and insert them into the foreign keys of the main table.
Question: How do I insert the data from a staging table into a table while using data from the staging to query IDs from a separate table?
Example tables:
TABLE location:
id int PRIMARY KEY,
location varchar(255) NOT NULL,
person_oneID int FOREIGN KEY REFERENCES people(person_id),
person_twoID int FOREIGN KEY REFERENCES people(person_id)
TABLE staging_location:
id int PRIMARY KEY,
location varchar(255) NOT NULL,
p1_full_name varchar(255) NOT NULL,
p2_full_name varchar(255) NOT NULL
TABLE people:
person_id int PRIMARY KEY,
first_name varchar(255) NOT NULL,
last_name varchar(255) NOT NULL,
full_name varchar(255) NOT NULL,
This question was the closest example to what I have been looking for. Though I haven't been able to get the query to work. Here is what I've tried:
INSERT INTO location(id,location,person_oneID,person_twoID)
SELECT (l.id,l.location,p1.person_oneID,p2.person_twoID)
FROM staging_location AS l
INNER JOIN people p1 ON p1.full_name = l.p1_full_name
INNER JOIN people p2 ON p2.full_name = l.p2_full_name
Additional info: I would like to do this in the same insert statement without using an update because of the number of locations being inserted. I'm using staging tables as a result of importing data from csv files. The csv file with people didn't have an ID field, so I created one for each person by following steps similar to the first answer from this question. Please let me know if any additional information is required or if I can find the answer to my question somewhere I haven't seen.
Use this code even though I do not know what your data structure is and a duplicate field may be inserted
INSERT INTO location(id,location,person_oneID,person_twoID)
SELECT (l.id,l.location,p1.person_id as person_oneID,p2.person_id as person_twoID)
FROM staging_location AS l
INNER JOIN people p1 ON p1.full_name = l.p1_full_name
INNER JOIN people p2 ON p2.full_name = l.p2_full_name

Delete a record based on multiple table choices SQL

I'm trying to wrap my head around how to accomplish this Delete query. The goal is I'm trying to delete a client record (main table) based on if they don't have an insurance policy (another table) and if their needs description is "transportation" and importance values is LESS than 5. The needs is another table. They are all connected with foreign keys and SSN as the connector and Delete cascade is working properly. The query is partially working as is. If there is no insurance policy, the Client is being deleted correctly. However, the need description and importance value factors are not currently working. It will still delete if I have no insurance policy, but my importance description is another value other than transportation.
It's almost like I need 2 subqueries compare both Needs table and Insurance_Policy table for deletion, but I don't know how to do that.
The database I'm using is Azure Data Studio
Here is my current Procedure code:
DROP PROCEDURE IF EXISTS Option17;
GO
CREATE PROCEDURE Option17
AS
BEGIN
DELETE FROM Client
WHERE Client.SSN NOT IN (SELECT I.SSN
FROM Insurance_Policy I, Needs N
WHERE Client.SSN = I.SSN
AND Client.SSN = N.SSN
AND N.need_description = 'transportation'
AND N.importance_value < 5)
END
Also, here are my table structures:
CREATE TABLE Client
(
SSN VARCHAR(9),
doctor_name VARCHAR(60),
doctor_phone_no VARCHAR(10),
lawyer_name VARCHAR(60),
lawyer_phone_no VARCHAR(10),
date_assigned DATE,
PRIMARY KEY (SSN),
FOREIGN KEY (SSN) REFERENCES Person
ON DELETE CASCADE
);
CREATE TABLE Insurance_Policy
(
policy_id VARCHAR(10),
provider_id VARCHAR(10),
provider_address VARCHAR(100),
insurance_type VARCHAR(10),
SSN VARCHAR(9),
PRIMARY KEY (policy_id),
FOREIGN KEY (SSN) REFERENCES Client,
);
CREATE TABLE Needs
(
SSN VARCHAR(9),
need_description VARCHAR(60),
importance_value INT CHECK(importance_value > 0 and importance_value <11),
PRIMARY KEY(SSN,need_description),
FOREIGN KEY(SSN) REFERENCES Client
ON DELETE CASCADE
);
Here is a screenshot if the formatting didn't hold up on procedure.
enter image description here
Based on your answers, I believe this is the code you are looking for. If this is not working, let me know.
To explain a little, using an INNER join will eliminate the need for a couple of those WHERE conditions. INNER JOIN only returns records where it exists in both tables. Also there is no need to link to the Client table from within the subquery.
Also you want where it does not have a description of transportation with an importance of less than 5. Since you are pulling a list to leave alone, you do not want to include these records.
DROP PROC IF EXISTS Option17;
GO
Create proc Option17
AS
BEGIN
DELETE FROM Client
WHERE SSN NOT IN (
SELECT
N.SSN
FROM Needs N
INNER JOIN Insurance_Policy I ON N.SSN = I.SSN
WHERE NOT (N.need_description = 'transportation' AND N.importance_value < 5)
);
END
GO
I think you want separate conditions on Needs and Insurance_Policy. And I recommend NOT EXISTS, because it better handles NULL values:
DELETE c
FROM Client c
WHERE NOT EXISTS (SELECT 1
FROM Insurance i
WHERE c.SSN = i.SSN
) AND
EXISTS (SELECT 1
FROM Needs n
WHERE c.SSN = n.SSN AND
n.need_description = 'transportation' AND
n.importance_value < 5
);

I am trying to VIEW 4 columns from 2 different tables I have already created in Oracle Live SQL

I want to use the VIEW command to display these 4 columns in one single schema. I have tried making a single VIEW with the first 3 columns, because they are from the the same table. Adding the one other column is where I'm struggling. I have tried the ALTER function but a VIEW schema doesn't seem to have the same edit privileges as a table would. I hope that makes sense.
create table PATIENTINFO (
PatientID number not null,
FirstName varchar2(50) not null,
LastName varchar2(50) not null,
Address varchar2(50),
City varchar2(50),
State varchar2(50),
ZipCode number(5),
Phone number(10) not null ,
Email varchar2(50),
MemberID number not null,
constraint pk_departments primary key (PatientID)
)
create table LABORDER (
LabOrderNumber number not null,
OrDate date not null,
ReqBloodTest varchar2(15) not null,
Reason varchar(50),
PatientID number not null,
constraint pk_laborder primary key (LabOrderNumber),
constraint fk_laborder_patientid foreign key (PatientID)
references PATIENTINFO (PatientID)
)
CREATE VIEW PatientBlood AS
SELECT FirstName, LastName, PatientID
FROM PATIENTINFO
Write the query you want and then create a view out of it. I started by writing the query below and then prefixed it with CREATE OR REPLACE VIEW. The example below has some randomly selected columns, change it to whatever columns you need. I chose to name my columns in the view definition, you can omit that but also do a million other things as stated in the docs
Side note: don't use mixed case for identifiers like column names/table names. It is confusing. In your case it didn't matter since you didn't use quotes, so they're case insensitive and the view below will work even though the identifiers are all lower case.
CREATE OR REPLACE VIEW laborder_v (
labordernumber,
patientid,
lastname
) AS
SELECT o.labordernumber,
p.patientid,
p.lastname
FROM laborder o
JOIN patientinfo p ON o.patientid = p.patientid;

Microsoft Access - Enter Parameter Value why?

I am encountering a problem for my database.
And tried to do the query for how many transactions have movie "Harry_Potter"?
so I used SQL query:
SELECT
COUNT(td.movie) AS number_of_occurrence,
td.transaction_number
FROM
TransactionDetails td,
MovieDetails md
WHERE
md.movie = Harry_Potter
But it asks for Harry_Potter enter parameter value why?
The relevant SQL statements are
CREATE TABLE TransactionDetails
(
transaction_number INTEGER PRIMARY KEY,
movie VARCHAR(30) NOT NULL,
date_of_transaction DATE NOT NULL,
member_number INTEGER NOT NULL
)
CREATE TABLE MovieDetails
(
movie VARCHAR(30) PRIMARY KEY,
movie_type VARCHAR(3) NOT NULL,
movie_genre VARCHAR(10) NOT NULL
)
ALTER TABLE TransactionDetails
ADD CONSTRAINT member_number_fk FOREIGN KEY (member_number) REFERENCES LimelightMemberDetails(member_number);
ALTER TABLE TransactionDetails
ADD CONSTRAINT transaction_number_drink_fk FOREIGN KEY (transaction_number) REFERENCES DrinkTransactionDetails(transaction_number);
ALTER TABLE TransactionDetails
ADD CONSTRAINT transaction_number_food_fk FOREIGN KEY (transaction_number) REFERENCES FoodTransactionDetails(transaction_number);
ALTER TABLE TransactionDetails
ADD CONSTRAINT movie_fk FOREIGN KEY (movie) REFERENCES MovieDetails (movie);
Thank you for your help! If there is anything wrong with my database design please let me know! thank you!
Change the query to something like
SELECT
COUNT(td.movie) AS number_of_occurrence,
td.transaction_number
FROM
TransactionDetails td,
MovieDetails md
WHERE
md.movie = "Harry_Potter"
Seeing as movie is a string, you need quotes around the value you are looking for.
If I am not mistaken MS Access takes " and SQL SERVER takes '
try this
md.movie = "Harry_Potter"
I guess, you are simply missing the quotation marks around the string you are comparing.

Deployment errors, "There is already an object named 'PK_***' in the database. Could not create constraint."

When I try to run my app, I get the following errors:
There is already an object named 'PK_***' in the database. Could not create constraint."
That's actually two errors combined for brevity. Note: the asterisks are my own; that is not the actual name of the key.
I've scoured what seems like every post on here, but I can't seem to get any further in finding a solution. The worst part? No one else in the team is experiencing these errors when they run, nor can they determine why I am. We are all using the same environment, VS 2012 Premium RC. I certainly have latest from TFS.
I am wondering if anyone else has come across an issue similar to this where the problems/errors occurred in only one person's environment? I can continue and run the app. It seems to run as expected, but I am the only one getting those errors.
In SQL Server constraints such as primary keys or foreign keys are objects in their own right, even though they are dependent upon the "containing" table.
That means that their names must be unique within the owning schema. So, just as executing DDL along the lines of
create table some_schema.foo
(
id int not null
)
go
create table some_schema.foo
(
id int not null
)
go
will raise an error when the second create table is [attempted to be] executed, executing ddl like this will likewise raise an error:
create table some_schema.foo
(
id int not null ,
description varchar(200) not null ,
constraint PK primary key clustered ( id ) ,
constraint AK01 unique nonclustered ( description ) ,
)
go
create table some_schema.bar
(
id int not null ,
description varchar(200) not null ,
constraint PK primary key clustered ( id ) ,
constraint AK01 unique nonclustered ( description ) ,
)
go
will likewise raise an error as the constraint that you're trying to create have duplicate names. You need to qualify them with the table name, thusly:
create table some_schema.foo
(
id int not null ,
description varchar(200) not null ,
constraint foo_PK primary key clustered ( id ) ,
constraint foo_AK01 unique nonclustered ( description ) ,
)
go
create table some_schema.bar
(
id int not null ,
description varchar(200) not null ,
constraint bar_PK primary key clustered ( id ) ,
constraint bar_AK01 unique nonclustered ( description ) ,
)
go
and you problem will go away.
It does seem to me that dependent objects that don't exist outside the context of an owing object should be namespaced within the owning object's scope, but that's not the way the SQL standard works.
Good luck!