Related
CREATE TABLE collections
(
id INTEGER IDENTITY(1,1) PRIMARY KEY,
name VARCHAR(50) NOT NULL
);
CREATE TABLE posts
(
id INTEGER IDENTITY(1, 1) PRIMARY KEY,
title VARCHAR(50) NOT NULL,
image_URL VARCHAR(120) NOT NULL,
author_id INTEGER NOT NULL,
collection_id INTEGER,
FOREIGN KEY(author_id) REFERENCES users(id),
FOREIGN KEY(collection_id) REFERENCES collections(id)
);
INSERT INTO collections (name)
VALUES ('Travel'), ('Nature'), ('Great Outdoors');
INSERT INTO posts (title, image_url, author_id, collection_id)
VALUES ('White Mountains', 'cloudinary.com/snp/balhwpq.jpg', 3, NULL),
('My new car!', 'cloudinary.com/snp/qpcnalpw.jpg', 1, NULL),
('Great weekend for camping', 'cloudinary.com/snp/nmclapa.png', 5, 3),
('Train Ride to France', 'cloudinary.com/snp/rjaptpalw.jpg', 4, 1),
('Small Town in Italy', 'cloudinary.com/snp/aqypqoua.jpg', 4, 1),
('Nice Day At The Air Show', 'cloudinary.com/snp/aswpqlsaq.jpg', 7, NULL),
('Barcelona Beach', 'cloudinary.com/snp/quapwnla.jpg', 9, NULL),
('Gorgeous City of Prague', 'cloudinary.com/snp/anvmapw.png', 3, NULL),
('Nothing like fresh Seafood!', 'cloudinary.com/snp/uqnqhapla.jpg', 8, 4),
('Bought a new Swiss army knife', 'cloudinary.com/snp/qanvpauaq.jpg', 6, NULL),
('Delicious meal in Boston', 'cloudinary.com/snp/malvpqlsa.jpg', 10, NULL),
('Swimming at Ursa Beach', 'cloudinary.com/snp/poqlanoqlav.jpg', 12, NULL),
('Chilly day in Norway', 'cloudinary.com/snp/vqwlnqposp.jpg', 11, NULL),
('Hiking through beautiful Vermont!', 'cloudinary.com/snp/qpoplnalqi.jpg', 5, 3);
Every time I try executing this, it completes the table creation and the first two insert statements and then I get an error
The INSERT statement conflicted with the FOREIGN KEY constraint
I believe the problem is that in the posts table Insert statement I'm setting some of the collection_id values to NULL which is causing the error. I never said that this column couldn't be NULL, so what's the problem?
I have to prepare and execute a script file to create and populate a relational database in Oracle with data about customers, current orders, and products. These are the relationships: Each customer can place any number of current orders and each current order is placed by one customer.Each current order is for one product but each product can be in any number of
current orders.
I can't figure out the errors when running this script. The Customer and Products table are successfully filled, but the Current_Orders table is not. There are a few error codes that appear when I run it but I am not understanding them as I am fairly new to SQL. Any help would be appreciated. Thanks! ORA-00001, ORA-00955, ORA-02449.
drop table Customer;
drop table Current_Orders;
drop table Products;
create table Customer
(Customer_Number integer not null,
Customer_Name char(20) not null,
Customer_City char(20) not null,
PRIMARY KEY(Customer_Number));
create table Products
(Product_Number integer not null,
Product_Description char(20) not null,
Unit_Price integer not null,
PRIMARY KEY (Product_Number));
create table Current_Orders
(Order_Number integer not null,
Order_Date date not null,
Shipping_Method char(20) not null,
Product_Number integer not null,
Quantity_Ordered integer not null,
Customer_Number integer not null,
PRIMARY KEY(Order_Number), FOREIGN KEY(Customer_Number)
references Customer(Customer_Number) ON DELETE CASCADE,
FOREIGN KEY(Product_Number) references Products(Product_Number)
ON DELETE CASCADE);
insert into Customer
values (1, 'Khizur Sheikh', 'Milpitas');
insert into Customer
values (2, 'Fatima Sheikh', 'Fremont');
insert into Customer
values (3, 'Syed Sheikh', 'Davis');
insert into Customer
values (4, 'Mohammed Sheikh', 'Oakland');
insert into Customer
values (5, 'Ali Sheikh', 'Dublin');
insert into Products
values (6, 'iphone', 300);
insert into Products
values (7, 'ipad', 400);
insert into Products
values (8, 'imac', 500);
insert into Products
values (9, 'ipod', 600);
insert into Products
values (10, 'ihome', 700);
insert into Products
values (11, 'apple', 800);
insert into Products
values (12, 'banana', 900);
insert into Products
values (13, 'orange', 1000);
insert into Products
values (14, 'grape', 1100);
insert into Products
values (15, 'avocado', 1200);
insert into Products
values (16, 'bread', 1300);
insert into Products
values (17, 'muffin', 1400);
insert into Products
values (18, 'cheese', 1500);
insert into Products
values (19, 'milk', 1600);
insert into Products
values (20, 'brownies', 1700);
insert into Products
values (21, 'candy', 1800);
insert into Products
values (22, 'soup', 1900);
insert into Products
values (23, 'strawberry', 11000);
insert into Products
values (24, 'cookies', 50);
insert into Products
values (25, 'chocolate', 10);
insert into Current_Orders
values (26, '22-oct-2017', 'truck', 6, 1, 1);
insert into Current_Orders
values (27, '22-nov-2017', 'ship', 7, 1, 2);
insert into Current_Orders
values (28, '22-dec-2017', 'train', 8, 3, 3);
insert into Current_Orders
values (29, '22-jan-2017', 'truck', 9, 2, 4);
insert into Current_Orders
values (30, '22-feb-2017', 'train', 10, 1, 5);
insert into Current_Orders
values (31, '22-mar-2017', 'truck', 12, 4, 2);
insert into Current_Orders
values (32, '22-apr-2017', 'plane', 17, 7, 4);
insert into Current_Orders
values (33, '22-may-2017', 'train', 19, 1, 5);
insert into Current_Orders
values (34, '22-jun-2017', 'ship', 22, 3, 2);
insert into Current_Orders
values (35, '22-jan-2017', 'ship', 21, 4, 3);
commit;
All the commands in your script work fine while running for the first time. However Issue seems to occur when you run the DROP TABLE statements on second and successive executions when table exists and has data.
drop table Customer;
drop table Current_Orders;
Error report - ORA-02449: unique/primary keys in table referenced by
foreign keys
02449. 00000 - "unique/primary keys in table referenced by foreign keys"
*Cause: An attempt was made to drop a table with unique or
primary keys referenced by foreign keys in another table.
*Action: Before performing the above operations the table, drop the
foreign key constraints in other tables.
I see you have defined a FOREIGN KEY on the column Customer_Number
FOREIGN KEY(Customer_Number)
references Customer(Customer_Number) ON DELETE CASCADE
So, when you try to DROP from Table Customer before Current_Orders , you are basically deleting parent record before deleting the child record which is not allowed. ORA-00955 and ORA-00001 occur as a consequence.
So, follow this order to drop tables in your script.
drop table Current_Orders;
drop table Products;
drop table Customer;
Please help with SQL query. I've got a table:
CREATE TABLE PCDEVUSER.tabletest
(
id INT PRIMARY KEY NOT NULL,
name VARCHAR2(64),
pattern INT DEFAULT 1 NOT NULL,
tempval INT
);
Let's pretend it was filled with values:
INSERT INTO TABLETEST (ID, NAME, PATTERN, TEMPVAL) VALUES (1, 'A', 1, 10);
INSERT INTO TABLETEST (ID, NAME, PATTERN, TEMPVAL) VALUES (2, 'A', 1, 20);
INSERT INTO TABLETEST (ID, NAME, PATTERN, TEMPVAL) VALUES (3, 'A', 2, 10);
INSERT INTO TABLETEST (ID, NAME, PATTERN, TEMPVAL) VALUES (5, 'A', 2, 20);
INSERT INTO TABLETEST (ID, NAME, PATTERN, TEMPVAL) VALUES (4, 'A', 2, 30);
And I need to update all records (grouped by pattern) with NO MAX value TEMPVALUE. So as result I have to update records with Ids (1, 3, 5). Records with IDs (2, 4) has max values in there PATTERN group.
HELP PLZ
This select statement will help you get the IDs you need :
SELECT
*
FROM
(SELECT
id
,name
,pattern
,tempval
,MAX(tempval) OVER (PARTITION BY pattern) max_tempval
FROM
tabletest
)
WHERE 1=1
AND tempval != max_tempval
;
You should be able to build an update statement around that easily enough
Something like this:
update tabletest t
set ????
where t.tempval < (select max(tempval) from tabletest tt where tt.pattern = t.pattern);
It is unclear what values you want to set. The ???? is for the code that sets the values.
I have two tables
CREATE TABLE REFERENCE_VALUES
(
REFERENCE_ID SERIAL,
REFERENCE_OBJ_NAME TEXT,
REFERENCE_VALUE_CODE BIGINT,
DISPLAY_NAME TEXT,
CREATED_ON TIMESTAMP,
MODIFIED_ON TIMESTAMP
);
ALTER TABLE REFERENCE_VALUES
ADD PRIMARY KEY (ID);
ALTER TABLE REFERENCE_VALUES
ADD FOREIGN KEY (REFERENCE_VALUE_CODE)
REFERENCES CATEGORY(ID);
CREATE TABLE CATEGORY
(
ID BIGSERIAL NOT NULL,
CODE TEXT NOT NULL,
NAME TEXT NOT NULL,
PARENT_ID BIGINT,
PATH TEXT,
COMP_ID BIGINT,
CREATED_ON TIMESTAMP,
MODIFIED_ON TIMESTAMP,
);
ALTER TABLE CATEGORY
ADD PRIMARY KEY (ID);
ALTER TABLE CATEGORY
ADD FOREIGN KEY (COMP_ID)
REFERENCES COMPANY_ID(ID);
I have the display names as DISPLAY_NAME in REFERENCE_VALUES table I want to SELECT all values from table CATEGORY but the value of NAME (In CATEGORY table) replaced by value from DISPLAY_NAME in REFERENCE_VALUES and if the value of of DISPLAY_NAME is NULL OR EMPTY i would keep the value of NAME (CATEGORY table).
I have been able to do that with the query below
SELECT C.ID, C.CODE, COALESCE(R.DISPLAY_NAME, C.NAME) as NAME, C.PARENT_ID, C.PATH, C.COMP_ID, C.CREATED_ON, C.MODIFIED_ON
FROM CATEGORY C
LEFT JOIN REFERENCE_VALUES R
ON C.ID = R.REFERENCE_VALUE_CODE
WHERE R.REFERENCE_OBJ_NAME = 'CATEGORY';
but i am getting only two records. How can i get all the records from the category table?
sample data to populate the tables
INSERT INTO CATEGORY VALUES (1, 'CVB', 'COMM VEH', NULL, 'CVB', 1, '2016-05-13 15:50:19.985', NULL);
INSERT INTO CATEGORY VALUES (2, 'LVB', 'AUTO', NULL, 'LVB', 1, '2016-05-13 15:50:19.994', NULL);
INSERT INTO CATEGORY VALUES (3, 'INB', 'INF', NULL, 'INB', 1, '2016-05-13 15:50:19.997', NULL);
INSERT INTO CATEGORY VALUES (4, 'OHB', 'OFF', NULL, 'OHB', 1, '2016-05-13 15:50:20', NULL);
INSERT INTO CATEGORY VALUES (5, 'LUB', 'LUB', NULL, 'LUB', 1, '2016-05-13 15:50:20.002', NULL);
INSERT INTO CATEGORY VALUES (52, 'TRA', 'TIE', 32, 'CVB.HA.TRA', 1, '2016-05-13 15:51:32.605', NULL);
INSERT INTO CATEGORY VALUES (68, 'PF', 'PER', 42, 'LVB.LA.PF', 1, '2016-05-13 15:51:33.117', NULL);
INSERT INTO CATEGORY VALUES (73, 'CE', 'CAR', 32, 'CVB.HA.CE', 1, '2016-05-13 15:51:33.733', NULL);
INSERT INTO CATEGORY VALUES (74, 'KP', 'KP', 32, 'CVB.HA.KP', 1, '2016-05-13 15:51:33.958', NULL);
INSERT INTO CATEGORY VALUES (26, 'RP', 'RING', 11, 'OHB.OH.RP', 1, '2016-05-13 15:51:30.149', NULL);
INSERT INTO CATEGORY VALUES (47, 'CP', 'COMP', 9, 'CVB.CV.CP', 1, '2016-05-13 15:51:31.903', NULL);
INSERT INTO CATEGORY VALUES (48, 'TB', 'TUB', 9, 'CVB.CV.TB', 1, '2016-05-13 15:51:31.905', NULL);
INSERT INTO CATEGORY VALUES (18, 'FB', 'FILT', 11, 'OHB.OH.FB', 1, '2016-05-13 15:51:30.002', NULL);
INSERT INTO REFERENCE_VALUES (ID, REFERENCE_OBJ_NAME, REFERENCE_VALUE_CODE, DISPLAY_NAME) VALUES (1, 'CATEGORY', 6, INDU CHANGED);
INSERT INTO REFERENCE_VALUES (ID, REFERENCE_OBJ_NAME, REFERENCE_VALUE_CODE) VALUES (2, 'CATEGORY', 7);
The WHERE condition filters out rows where R.REFERENCE_OBJ_NAME is null, which is all rows for which there are no matching REFERENCE_VALUE.
Maybe what you are trying to do is only join REFERENCE_VALUES where REFERENCE_OBJ_NAME is 'CATEGORY'. If so you could do it like this:
SELECT C.ID, C.CODE, COALESCE(R.DISPLAY_NAME, C.NAME) as NAME, C.PARENT_ID, C.PATH, C.COMP_ID, C.CREATED_ON, C.MODIFIED_ON
FROM CATEGORY C
LEFT JOIN REFERENCE_VALUES R
ON R.REFERENCE_OBJ_NAME = 'CATEGORY' AND C.ID = R.REFERENCE_VALUE_CODE
Query :
INSERT INTO "Track"
SELECT "Leonard Collections" AS "Album",
"Instrumental" AS "Artist",
"00:02:59.3800000" AS "Duration",
"1/1/0001 12:00:00 AM" AS "ReleasedDate",
"If You Love Me" AS "Title",
"False" AS "IsPlayableOnLocal"
UNION
SELECT "Leonard Collections",
"Instrumental",
"00:02:56.6930000",
"1/1/0001 12:00:00 AM",
"Espoir",
"False",
UNION
SELECT "Leonard Collections",
"Instrumental",
"00:03:51.6770000",
"1/1/0001 12:00:00 AM",
"Don't Cry For My Argentina",
"False"
Error :
SQL logic error or missing database
near "UNION": syntax error
Table :
CREATE TABLE Track
(
ID INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL ,
Album VARCHAR(100) NULL ,
Artist VARCHAR(255) NOT NULL DEFAULT "Artist Unknown",
Duration VARCHAR(255) NOT NULL ,
LocalPath VARCHAR(255) NULL ,
ReleasedDate DATE NOT NULL ,
Title VARCHAR(255) NULL ,
IsPlayableOnLocal INTEGER NOT NULL ,
Rating VARCHAR(255) NULL
)
What is wrong with my query?
Since you mention latest version of SQLite, you should use multi-valued insert (supported by SQLite since version 3.7.11), like this:
INSERT INTO mytable (col1, col2, col3) VALUES
(1, 2, "abc"),
(2, 4, "xyz"),
(3, 5, "aaa"),
(4, 7, "bbb");
This is shorter, faster and less prone to errors. This syntax is also supported by some other databases (at least MySQL and PostgreSQL).
In your second union statement you have superflous ',' character after "False". That is most likely the problem.
Successfully tested this into DB Browser for SQLite Execute SQL tab > SQL1 text editor field:
CREATE TABLE "FOODS" (
"id" INTEGER NOT NULL,
"fruits" TEXT,
"tastes" INTEGER UNIQUE,
PRIMARY KEY("id" AUTOINCREMENT)
);
INSERT INTO "FOODS" (id, fruits) VALUES
(1, "apple", 1),
(2, "banana", 3),
(3, "mango", 5),
(4, "pear", 2),
(5, "cherry", 4),
(6, "pomegranate", 6);
https://pastebin.com/naXNRQEk
We must Create the table at the same time as the insertion (as with both statements above together).
Failed tests with trying to execute the INSERT statement alone on previously created from the Database Structure > Create Table button.
(That (alone) does not work in DB Browser for SQLite:)
INSERT INTO "FOODS" (id, fruits) VALUES
(1, "apple", 1),
(2, "banana", 3),
(3, "mango", 5),
(4, "pear", 2),
(5, "cherry", 4),
(6, "pomegranate", 6);