CHECK constraint on a table [duplicate] - sql

This question already has answers here:
adding CHECK to impose constraints on more than one table
(4 answers)
Closed 8 years ago.
I want to modify the following DDL to add CHECK constraints so that the manager of a store(FK employee_number is store table) works at the same store (FK store_code in employee table table) and a store supplies all the products if its type is 'local'.
Can anyone help?
CREATE TABLE employee(
employee_number CHAR(5) NOT NULL,
name VARCHAR(30),
store_code CHAR(5)
PRIMARY KEY(employee_number),
FOREIGN KEY(store_code) REFERENCES store
)
CREATE TABLE store(
store_code CHAR(5) NOT NULL,
type VARCHAR(15),
employee_number CHAR(5),
PRIMARY KEY(store_code),
FOREIGN KEY(employee_number) REFERENCES employee
)
CREATE TABLE product(
product_code CHAR(5) NOT NULL,
description VARCHAR(150),
cost DEC(10,2),
PRIMARY KEY(product_code)
)
CREATE TABLE stocks(
store_code CHAR(5) NOT NULL,
product_code CHAR(5) NOT NULL,
PRIMARY KEY(product_code, store_code),
FOREIGN KEY(product_key) REFERENCES product,
FOREIGN KEY(store_code) REFERENCES store
)

you could use a trigger to realize this
you can look at this question for an example Throw an error in a MySQL trigger

Related

How can I write simple data mining queries for the following schemas?

For the following given schema, can I please guided on how to write simple Data mining queries using oracle with sqlplus?
CREATE TABLE Location(Location_id NUMBER(5) NOT NULL, Location_name varchar(15) NOT NULL,PRIMARY KEY (Location_id));
CREATE TABLE Customer(customer_id NUMBER(5) NOT NULL, customer_name varchar(15) NOT NULL, customer_credit NUMBER(6) NOT NULL, PRIMARY KEY (customer_id));
CREATE TABLE Product(product_id NUMBER(5) NOT NULL, product_name varchar(10) NOT NULL, product_price NUMBER(6) NOT NULL, product_quantity NUMBER(6) NOT NULL, PRIMARY KEY (product_id));
CREATE TABLE Supplier(supplier_id NUMBER(5) NOT NULL, supplier_name varchar(15) NOT NULL, supplier_quantity NUMBER(5) NOT NULL,PRIMARY KEY (supplier_id));
CREATE TABLE Store (customer_id NUMBER(5) NOT NULL, supplier_id NUMBER(5) NOT NULL, product_id NUMBER(5) NOT NULL, location_id NUMBER(5) NOT NULL,sale NUMBER(9) NOT NULL,category varchar(9) NOT NULL);
ALTER TABLE Store ADD CONSTRAINT Store_fk0 FOREIGN KEY (customer_id) REFERENCES Customer(customer_id);
ALTER TABLE Store ADD CONSTRAINT Store_fk1 FOREIGN KEY (supplier_id) REFERENCES Supplier(supplier_id);
ALTER TABLE Store ADD CONSTRAINT Store_fk2 FOREIGN KEY (product_id) REFERENCES Product(product_id);
ALTER TABLE Store ADD CONSTRAINT Store_fk3 FOREIGN KEY (location_id) REFERENCES Location(Location_id);
I have the following OLAP query which I wrote which I don't see different from a Data mining query. Also can I be guided on how to write more complex queries for the following:
SELECT CUSTOMER.CUSTOMER_NAME,PRODUCT.PRODUCT_NAME,SALE FROM STORE INNER JOIN CUSTOMER ON STORE.CUSTOMER_ID = CUSTOMER.CUSTOMER_ID INNER JOIN PRODUCT ON STORE.PRODUCT_ID = PRODUCT.PRODUCT_ID WHERE STORE.SALE = (SELECT MAX(SALE) FROM STORE);
I will appreciate any help.
This question is too vague to have a good answer. Please refer to Oracle Data Mining for more info about Data Mining.
Data Mining start from a simple query like:
BEGIN
DBMS_PREDICTIVE_ANALYTICS.EXPLAIN(
data_table_name => 'mining_data_build_v',
explain_column_name => 'affinity_card',
result_table_name => 'ai_explain_output');
END;
/
And the skies are the limit...

ORA-01748: only simple column names allowed here in Oracle

What I am trying to do ?
I am trying to create two tables and at the same time i am trying to link them together using foreign and primary keys. However I successfully create my parent table ( Student with primary key ) but failed to create child table ( Attendence with foreign key ).
What is the problem ?
I get the following error while creating Attendence table:
ERROR at line 5: ORA-01748: only simple column names allowed here
My code:
Student table:
create table Student (
ST_ROLLNO NUMBER(6) constraint s_pk primary key,
ST_NAME VARCHAR(30) not null,
ST_ADDRESS varchar(35) not null
);
Attendence table:
create table Attendence (
ST_ROLLNO NUMBER(6),
ST_DATE VARCHAR(30) not null,
ST_PRESENT_ABSENT varchar(1) not null,
constraint f_pk Attendence.ST_ROLLNO foreign key references Student(ST_ROLLNO)
);
Your foreign key constraint syntax is wrong; it should be:
constraint f_pk foreign key (ST_ROLLNO) references Student(ST_ROLLNO)
You are preceding the FK column name with the table name, which is wrong in itself, but also have it in the wrong place.
create table Student (
ST_ROLLNO NUMBER(6) constraint s_pk primary key,
ST_NAME VARCHAR(30) not null,
ST_ADDRESS varchar(35) not null
);
Table STUDENT created.
create table Attendence (
ST_ROLLNO NUMBER(6),
ST_DATE VARCHAR(30) not null,
ST_PRESENT_ABSENT varchar(1) not null,
constraint f_pk foreign key (ST_ROLLNO) references Student(ST_ROLLNO)
);
Table ATTENDENCE created.
According to oracle documentation,
ORA ERR
ORA-01748 only simple column names allowed here
The following is the cause of this error:
This SQL statement does not allow a qualified column name, such as
username.table.column or table.column.
Action you can take to resolve this issue: Remove the qualifications
from the column and retry the operation.
In your case, you are trying to refer to the table name while defining a constraint -
Attendence.ST_ROLLNO - WRONG.
It must contain a simple name without the table name or schema name.

Simple SQL issues, cannot figure out what I'm doing wrong to get these errors - ORA-00942 / 907 / 922

This is a supposed to be a simple SQL project, but I'm stuck on the first step. I've never worked with SQL before, so I'm pretty lost. Can someone please tell me what I did wrong that's causing these errors?
Here is the file:
SPOOL output.log;
DROP TABLE rental CASCADE CONSTRAINTS;
DROP TABLE movie CASCADE CONSTRAINTS;
DROP TABLE customer CASCADE CONSTRAINTS;
DROP TABLE distributor CASCADE CONSTRAINTS;
DROP TABLE rental_store CASCADE CONSTRAINTS;
CREATE TABLE rental (
inventory_id CHAR(10) PRIMARY KEY,
transaction_id CHAR(10) NOT NULL UNIQUE,
late DECIMAL(6,2),
damaged DECIMAL(6,2),
fail_rewind DECIMAL(6,2),
taxes DECIMAL(6,2) NOT NULL,
discount DECIMAL(6,2),
customer_id CHAR(10) NOT NULL UNIQUE,
CONTRAINT rental_FK FOREIGN KEY (customer_id) REFERENCES customer (customer_id));
CREATE TABLE movie (
title_id_number CHAR(10) PRIMARY KEY,
genre VARCHAR2(20) NOT NULL,
actor VARCHAR2(30) NOT NULL,
director VARCHAR2(30) NOT NULL,
awards VARCHAR2(30),
running_length INTEGER NOT NULL,
rating VARCHAR2(5) NOT NULL,
year_released INTEGER NOT NULL,
media_type CHAR(5) NOT NULL,
inventory_id CHAR(10) NOT NULL UNIQUE,
title VARCHAR2(30) NOT NULL,
distrib_serial INTEGER NOT NULL UNIQUE,
cat_mov_id CHAR(10) NOT NULL UNIQUE));
CREATE TABLE customer (
customer_id CHAR(10) PRIMARY KEY,
name VARCHAR2(30) NOT NULL,
address VARCHAR2(50) NOT NULL,
tele_number CHAR(10) NOT NULL UNIQUE,
rent_history INTEGER NOT NULL));
CREATE TABLE distributor (
distributor_name VARCHAR2(30) PRIMARY KEY,
catalog VARCHAR2(30) NOT NULL,
genres_offered VARCHAR2(50) NOT NULL));
CREATE TABLE rental_store (
store_name VARCHAR2(30) PRIMARY KEY,
address VARCHAR2(50) NOT NULL,
owner VARCHAR2(30) NOT NULL));
SPOOL OFF;
The errors for the DROP statements are to be expected because the tables do not yet exist. You can safely ignore them.
Most of your problems come from the second closing parenthesis, e.g.
CREATE TABLE movie (
...
cat_mov_id CHAR(10) NOT NULL UNIQUE));
^ ---- here
You need to remove them. It is only needed in the first statement because of the column list of the foreign key.
The first (real) error you get is because you misspelled CONSTRAINT (you wrote CONTRAINT. The full clause needs to be
CONSTRAINT rental_FK FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
^ -- the "s" was missing here
However you can't create a foreign key constraint to a table that does not exist. So you need to change the order of the create statements to first create the customer table, then you can create the rental table.
Putting all that together, your script should look like this:
DROP TABLE rental CASCADE CONSTRAINTS;
DROP TABLE movie CASCADE CONSTRAINTS;
DROP TABLE customer CASCADE CONSTRAINTS;
DROP TABLE distributor CASCADE CONSTRAINTS;
DROP TABLE rental_store CASCADE CONSTRAINTS;
CREATE TABLE customer (
customer_id CHAR(10) PRIMARY KEY,
name VARCHAR2(30) NOT NULL,
address VARCHAR2(50) NOT NULL,
tele_number CHAR(10) NOT NULL UNIQUE,
rent_history INTEGER NOT NULL
);
CREATE TABLE rental (
inventory_id CHAR(10) PRIMARY KEY,
transaction_id CHAR(10) NOT NULL UNIQUE,
late DECIMAL(6,2),
damaged DECIMAL(6,2),
fail_rewind DECIMAL(6,2),
taxes DECIMAL(6,2) NOT NULL,
discount DECIMAL(6,2),
customer_id CHAR(10) NOT NULL UNIQUE,
CONSTRAINT rental_FK FOREIGN KEY (customer_id) REFERENCES customer (customer_id)
);
CREATE TABLE movie (
title_id_number CHAR(10) PRIMARY KEY,
genre VARCHAR2(20) NOT NULL,
actor VARCHAR2(30) NOT NULL,
director VARCHAR2(30) NOT NULL,
awards VARCHAR2(30),
running_length INTEGER NOT NULL,
rating VARCHAR2(5) NOT NULL,
year_released INTEGER NOT NULL,
media_type CHAR(5) NOT NULL,
inventory_id CHAR(10) NOT NULL UNIQUE,
title VARCHAR2(30) NOT NULL,
distrib_serial INTEGER NOT NULL UNIQUE,
cat_mov_id CHAR(10) NOT NULL UNIQUE
);
CREATE TABLE distributor (
distributor_name VARCHAR2(30) PRIMARY KEY,
catalog VARCHAR2(30) NOT NULL,
genres_offered VARCHAR2(50) NOT NULL
);
CREATE TABLE rental_store (
store_name VARCHAR2(30) PRIMARY KEY,
address VARCHAR2(50) NOT NULL,
owner VARCHAR2(30) NOT NULL
);
Unrelated, but: you do not want to use the CHAR data type. It's more efficient and will give you less headaches if you use VARCHAR for every character column.
Of course the first time you run the script your DROP TABLEs will fail because you haven't created them yet. That's to be expected and is acceptable in such a script.
Beyond that, if you are going to create FK relationships, you need to create the parent table before you create the child, which you will see after you correct your misspelled key word.
I will leave it as an exercise for the student to take that clue and locate the mis-spelled key word in the first CREATE TABLE statement.

SQL Constraints Confusion

I'm in a bit of a mix right now, I have a Table called Avatars, which has a foreign key called Family. Now, in the family table, I have two Foreign Keys called Mother and Father - Now this is the confusing bit, in both the Mother and Father Tables, there is a Foreign key called Avatar_ID which is of course the Primary Key to the Avatars table. I'm not sure if that's even allowed in SQL PLUS.
Whenever I try and enter the tables 'Family, Mother or Father', I keep getting the error:ORA-02291: integrity constraint (SG304.FK_FATHER_ID) violated - parent key not found.
Is there any way around this? Or will I have to change my code completely? A sample of the code is below.
CREATE TABLE Avatars (
Avatar_ID VARCHAR(10) NOT NULL,
Avatar_Name VARCHAR(30),
AvA_DOB DATE,
Age VARCHAR(30),
Gender VARCHAR(30),
Strength_Indicated INT,
Hoard INT,
Avatar_Level VARCHAR(30),
Skill VARCHAR(30),
Original_Owner VARCHAR(30),
Family_ID VARCHAR(10) NOT NULL,
Species_ID VARCHAR(10) NOT NULL,
Inventory_ID VARCHAR(30) NOT NULL,
Weapon_ID VARCHAR(10),
Player_ID VARCHAR(10) NOT NULL,
PRIMARY KEY (Avatar_ID));
CREATE TABLE Family (
Family_ID VARCHAR(10) NOT NULL,
Mother_ID VARCHAR(10) NOT NULL,
Father_ID VARCHAR(10) NOT NULL,
primary key(Family_ID)
);
CREATE TABLE Mother (
Mother_ID VARCHAR(10) NOT NULL,
Avatar_ID VARCHAR(10) NOT NULL,
primary key(Mother_ID)
);
CREATE TABLE Father (
Father_ID VARCHAR(10) NOT NULL,
Avatar_ID VARCHAR(10) NOT NULL,
primary key(Father_ID)
);
ALTER TABLE Avatars
ADD CONSTRAINT fk_Family_ID
FOREIGN KEY (Family_ID)
REFERENCES Family(Family_ID);
ALTER TABLE Family
ADD CONSTRAINT fk_Mother_ID
FOREIGN KEY (Mother_ID)
REFERENCES Mother(Mother_ID);
ALTER TABLE Family
ADD CONSTRAINT fk_Father_ID
FOREIGN KEY (Father_ID)
REFERENCES Father(Father_ID);
ALTER TABLE Father
ADD CONSTRAINT fk_Avatar_ID
FOREIGN KEY (Avatar_ID)
REFERENCES Avatars(Avatar_ID);
ALTER TABLE Mother
ADD CONSTRAINT fk_Avatars_ID
FOREIGN KEY (Avatar_ID)
REFERENCES Avatars(Avatar_ID);
INSERT INTO Avatars (Avatar_ID,Avatar_Name,AvA_DOB,Age,Gender,Strength_Indicated,Hoard,Avatar_Level, Skill, Original_Owner, Family_ID,Species_ID,Inventory_ID,Player_ID) VALUES
('Ava01','Verda','20-JAN-2014','1 year 2 months','Female','100','20','Master','Leader',' - ',' - ','DRA1','MasterInventory','Player07');
Thanks in advance for any help given! (:
Foreign key refers to a record in parent table. In your INSERT statement you are inserting value ' - ' into a column parent_id. In this error message oracle informs you that there is no record with value ' - ' in a column family_id of a table family. As I can understand, you are trying to use ' - ' as 'absence of value'. There is special value for that - NULL. So you need to write your statement as:
INSERT INTO Avatars (Avatar_ID, Avatar_Name, AvA_DOB,
Age, Gender, Strength_Indicated, Hoard, Avatar_Level, Skill,
Original_Owner, Family_ID, Species_ID, Inventory_ID, Player_ID)
VALUES ('Ava01', 'Verda', '20-JAN-2014', '1 year 2 months' ,'Female',
'100', '20', 'Master', 'Leader', NULL, NULL, 'DRA1',
'MasterInventory', 'Player07');
Also I can recommend some changes to your schema. First of all, use number data type for primary keys - it allows you to use sequences to generate unique values. Also, I don't know details of the problem, but "family relations" in your tables look a bit complicated. You can describe it in a single table:
create table family_tree (
person_id number primary key,
father_id number,
mother_id number,
sex char(1),
name varchar2(50),
family_name varchar2(50));
add constraint fk_mother_id foreign key (mother_id)
references family_tree (person_id);
add constraint fk_father_id foreign key (father_id)
references family_tree (person_id);

I cannot create a simple table

I try to Write the SQL code to create the table named ‘EMP_1’. This table is a subset of the EMPLOYEE table, and the structure of the table is summarized as shown below.
This is the information:
Attribute Name Data Type Remarks
EMP_NUM CHAR(3) PK
EMP_LNAME VARCHAR(15) Not Null
EMP_FNAME VARCHAR(15) Not Null
EMP_INITIAL CHAR(1)
EMP_HIREDATE DATE
JOB_CODE CHAR(3) FK (from JOB table)
My code:
CREATE TABLE EMP_1
(
EMP_NUM CHAR(3) PRIMARY KEY,
EMP_LNAME VARCHAR(15) Not Null,
EMP_FNAME VARCHAR(15) Not Null,
EMP_INITIAL CHAR(1) ,
EMP_HIREDATE DATETIME,
JOB_CODE CHAR(3) FOREIGN KEY (JOB_CODE) REFERENCES JOB(JOB_CODE)
);
I keep getting CONSTRAINT error
I think you might be missing a comma before the constraint. This worked when I tried it:
CREATE TABLE EMP_1
(
EMP_NUM CHAR(3) PRIMARY KEY,
EMP_LNAME VARCHAR(15) Not Null,
EMP_FNAME VARCHAR(15) Not Null,
EMP_INITIAL CHAR(1),
EMP_HIREDATE DATETIME,
JOB_CODE CHAR(3),
CONSTRAINT FK_JOBS FOREIGN KEY (JOB_CODE) REFERENCES JOB(JOB_CODE)
);
Make sure your PRIMARY KEY and FOREIGN KEY have the same DATA TYPE.
I've not scrpited a FK for a while, but my recollection is that you can EITHER use:
JOB_CODE CHAR(3) FOREIGN KEY REFERENCES JOB(JOB_CODE)
which will create an FK named JOB_CODE,
OR:
split the line so you create the field and add the constraint separately as per JPW's response.
Your original code tries to combine the 2 approaches which will throw the error you describe.