Create table A_15006977.vehicle. (
Vin varchar(20) primary key,
Vehicle_type char(20) not null,
Mileage number(20) not null,
Manufacturer char(20) not null
);
Insert all
Into A_15006977.vehicle(vin,vehicle_type,mileage,manufacturer)
values ('tf1bb2ve533093891','panel van',18 325,'man')
A_15006977.vehicle(vin,vehicle_type,mileage,manufacturer)
values
('tf1bb2ve533093822','standard van',79 885,'ford')
Select * from dual;
Create table A_15006977.vehicle (
Vin varchar(20) CONSTRAINT vehicle__vin__pk PRIMARY KEY,
Vehicle_type char(20) CONSTRAINT vehicle__vehicle_type__nn not null,
Mileage number(20) CONSTRAINT vehicle__mileage__nn not null,
Manufacturer char(20) CONSTRAINT vehicle__manufacturer__nn not null
);
Insert all
Into A_15006977.vehicle(vin,vehicle_type,mileage,manufacturer)
VALUES ( 'tf1bb2ve533093891', 'panel van', 18325, 'man' )
INTO A_15006977.vehicle (vin,vehicle_type,mileage,manufacturer)
values ( 'tf1bb2ve533093822', 'standard van', 79885, 'ford' )
SELECT 1 FROM DUAL;
Or:
Insert Into A_15006977.vehicle( vin,vehicle_type,mileage,manufacturer )
SELECT 'tf1bb2ve533093891','panel van', 18325, 'man' FROM DUAL UNION ALL
SELECT 'tf1bb2ve533093822','standard van', 79885, 'ford' FROM DUAL;
Note:
You had an extra . after the table name in the DDL statement and spaces in the mileage (18 325 and 79 885) which need removing and you needed an INTO keyword before the second insert.
It is also useful to name your constraints (then you can easily determine which constraint has been violated in later statements).
Related
HERE'S THE TABLE I CREATED:
CREATE TABLE personal_info (
Person_name VARCHAR(30) NOT NULL,
Date_of_Birth DATE,
Join_date DATE,
Join_year NUMBER,
Person_address VARCHAR(75),
Person_Post VARCHAR(15),
Person_id VARCHAR(9) NOT NULL UNIQUE,
Email_primary VARCHAR(30),
Phone_primary NUMBER,
Email_secondary VARCHAR(30),
Phone_secondary NUMBER,
Sal_grade CHAR(1) NOT NULL,
Empl_id NUMBER NOT NULL,
CONSTRAINT FK_Salary_Person FOREIGN KEY (Sal_grade) REFERENCES salary(Salary_grade) ON DELETE CASCADE,
CONSTRAINT FK_Employee_Person FOREIGN KEY (Empl_id) REFERENCES employee(Employee_id) ON DELETE CASCADE,
CONSTRAINT UC_Person_ID UNIQUE (Empl_id,Person_name)
);
HERE'S THE EMPLOYEE TABLE:
CREATE TABLE employee (
Employee_id NUMBER NOT NULL PRIMARY KEY,
Employee_job_description VARCHAR(200),
Proj_id NUMBER NOT NULL,
Dep_id NUMBER NOT NULL
);
ALTER TABLE employee
ADD CONSTRAINT FK_project_employee
FOREIGN KEY (Proj_id) REFERENCES PROJECTS(Project_id) ON DELETE CASCADE;
ALTER TABLE employee
ADD CONSTRAINT FK_dept_employee
FOREIGN KEY (Dep_id) REFERENCES dept(Dept_id) ON DELETE CASCADE;
CREATE SEQUENCE EMPID_SEQ1
MINVALUE 1
MAXVALUE 9999999
START WITH 10000
INCREMENT BY 4
CACHE 20;
I ALREADY INSERTED INTO THE EMPLOYEE TABLE, NO ISSUE.
INSERT INTO employee (Employee_id, Employee_job_description, Proj_id, Dep_id) VALUES(EMPID_SEQ1.NEXTVAL,'SENIOR VICE PRESIDENT',501,1);
BUT WHEN I TRY TO INSERT INTO THE PERSONAL_INFO TABLE:
/* Formatted on 19-Oct-22 11:58:19 AM (QP5 v5.256.13226.35538) */
INSERT INTO PERSONAL_INFO (Empl_id,
Person_name,
Date_of_Birth,
Join_date,
Join_year,
Person_address,
Sal_grade,
Actual_salary,
Person_Post,
PERSON_ID,
Email_primary,
Phone_primary,
Email_secondary,
Phone_secondary)
VALUES (EMPID_SEQ1.CURRVAL,
'Mr. FF',
TO_DATE ('1980/05/03 21:02:44', 'yyyy/mm/dd hh24:mi:ss'),
TO_DATE ('2000/05/03 21:02:44', 'yyyy/mm/dd hh24:mi:ss'),
TO_CHAR (Join_DATE, 'YYYY'),
'Banani,Dhaka.',
'D',
150000,
'SVP',
TO_CHAR(TO_CHAR(Join_YEAR) || TO_CHAR (EMPID_SEQ1.CURRVAL)),
'FF#bank.com',
01234567891,
'FFF#bank.com',
99998882222);
I GET THE AFORMENTIONED ERROR WHILE INSERTING THE PERSON_ID UNIQUE KEY VALUE. BASICALLY I WANTED THE PERSON_ID TO LOOK SOMETHING LIKE '200710016'. JOINING YEAR FOLLOWED BY EMPLOYEE ID.
BUT IT'S TELLING ME THAT THE JOIN_YEAR COLUMN IS NOT ALLOWED HERE.
You can't reference a column that's just being inserted - you'll have to "repeat" the same data again.
Also, target table should be modified (two datatypes - phone numbers aren't really "numbers" because of possible leading zeros; one column is missing).
As of the sequence, you first have to select nextval and then currval because - initially - currval doesn't exist.
When fixed:
SQL> CREATE TABLE personal_info
2 (
3 Person_name VARCHAR (30) NOT NULL,
4 Date_of_Birth DATE,
5 Join_date DATE,
6 Join_year NUMBER,
7 Person_address VARCHAR (75),
8 Person_Post VARCHAR (15),
9 Person_id VARCHAR (9) NOT NULL UNIQUE,
10 Email_primary VARCHAR (30),
11 Phone_primary VARCHAR (30), --> change datatype
12 Email_secondary VARCHAR (30),
13 Phone_secondary VARCHAR (30), --> change datatype
14 Sal_grade CHAR (1) NOT NULL,
15 Empl_id NUMBER NOT NULL,
16 actual_salary NUMBER --> add this column
17 );
Table created.
Insert:
SQL> INSERT INTO PERSONAL_INFO (Empl_id,
2 Person_name,
3 Date_of_Birth,
4 Join_date,
5 Join_year,
6 Person_address,
7 Sal_grade,
8 Actual_salary,
9 Person_Post,
10 PERSON_ID,
11 Email_primary,
12 Phone_primary,
13 Email_secondary,
14 Phone_secondary)
15 VALUES (
16 EMPID_SEQ1.nextval,
17 'Mr. FF',
18 TO_DATE ('1980/05/03 21:02:44', 'yyyy/mm/dd hh24:mi:ss'),
19 TO_DATE ('2000/05/03 21:02:44', 'yyyy/mm/dd hh24:mi:ss'),
20 TO_CHAR (
21 TO_DATE ('2000/05/03 21:02:44', 'yyyy/mm/dd hh24:mi:ss'),
22 'YYYY'),
23 'Banani,Dhaka.',
24 'D',
25 150000,
26 'SVP',
27 TO_CHAR (
28 TO_CHAR (
29 TO_DATE ('2000/05/03 21:02:44',
30 'yyyy/mm/dd hh24:mi:ss'),
31 'YYYY')
32 || TO_CHAR (EMPID_SEQ1.CURRVAL)),
33 'FF#bank.com',
34 '01234567891',
35 'FFF#bank.com',
36 '99998882222');
1 row created.
You should not have a join_year column in the table as the value can be calculated from the join_date column and if it is independent then the two columns can get out-of-sync. If you did want to have it in the table then it should be a virtual column. Similarly for person_id (although if there were business reasons to persist the person_id so that it could be changed later then you could set it with a BEFORE INSERT trigger rather than using a virtual column, but either way you should not need to supply a default value derived from other columns in the INSERT statement):
CREATE TABLE personal_info (
Person_name VARCHAR(30) NOT NULL,
Date_of_Birth DATE,
Join_date DATE,
Join_year NUMBER
GENERATED ALWAYS AS (EXTRACT(YEAR FROM join_date)),
Person_address VARCHAR(75),
Person_Post VARCHAR(15),
Person_id VARCHAR(9)
GENERATED ALWAYS AS (
CAST(
TO_CHAR(EXTRACT(YEAR FROM join_date), 'FM0000')
|| TO_CHAR(empl_id, 'FM00000')
AS VARCHAR2(9)
)
)
NOT NULL
UNIQUE,
Email_primary VARCHAR(30),
Phone_primary VARCHAR2(12),
Email_secondary VARCHAR(30),
Phone_secondary VARCHAR2(12),
Sal_grade /* CHAR(1) */ NOT NULL,
Actual_Salary NUMBER(10,2),
Empl_id /* NUMBER */ NOT NULL,
CONSTRAINT FK_Salary_Person FOREIGN KEY (Sal_grade)
REFERENCES salary(Salary_grade) ON DELETE CASCADE,
CONSTRAINT FK_Employee_Person FOREIGN KEY (Empl_id)
REFERENCES employee(Employee_id) ON DELETE CASCADE,
CONSTRAINT UC_Person_ID UNIQUE (Empl_id, Person_name)
);
Note: You do not need to include the data type when a column has a foreign key constraint; if you omit it then the column will take the same data type as the primary/unique key it is referencing and you can ensure consistency between the tables.
Note 2: The EMPID_SEQ1 sequence starts with 10000 and increases by 4 to a maximum of 9999999 and the Person_id column can take 9 characters, of which 4 are the year so once there are 22500 people in the table and the sequence gets to 100000 then it will be too large for the Person_id column. You should probably either limit the sequence to 99999 or make the Person_id column larger.
Note 3: While phone numbers are numeric, they often have leading zeroes and these will not be stored in a NUMBER column so you should store phone numbers as a VARCHAR2.
Note 4: You are missing the actual_salary column.
Then you can use:
INSERT INTO PERSONAL_INFO (
Empl_id,
Person_name,
Date_of_Birth,
Join_date,
Person_address,
Sal_grade,
Actual_salary,
Person_Post,
Email_primary,
Phone_primary,
Email_secondary,
Phone_secondary
) VALUES (
EMPID_SEQ1.CURRVAL,
'Mr. FF',
TO_DATE ('1980/05/03 21:02:44', 'yyyy/mm/dd hh24:mi:ss'),
TO_DATE ('2000/05/03 21:02:44', 'yyyy/mm/dd hh24:mi:ss'),
'Banani,Dhaka.',
'D',
150000,
'SVP',
'FF#bank.com',
'01234567891',
'FFF#bank.com',
'99998882222'
);
fiddle
I have been trying to get this thing working for a few day now (foreign key) and it just don't work, and feel like every solution I used don't work, so i'm asking here to learn what was the problem and how to fix it
Table creation :
CREATE TABLE CUSTOMER
(
Customer_ID varchar(255) NOT NULL,
Customer_Name varchar(50),
Customer_Gender varchar(10),
Customer_DOB varchar(20) ,
CONSTRAINT CUSTOMER_PK PRIMARY KEY(Customer_ID)
) ;
CREATE TABLE PAYMENT
(
Payment_ID varchar(255) NOT NULL,
Cust_ID varchar(255),
Payment_Method varchar(30),
Payment_Date varchar(20),
Payment_Total NUMBER(10,2) ,
CONSTRAINT PAYMENT_PK PRIMARY KEY(Payment_ID),
CONSTRAINT fk_customer FOREIGN KEY(Cust_ID) REFERENCES CUSTOMER(Customer_ID)
) ;
Inserting values :
INSERT INTO CUSTOMER VALUES ('1277','Jenny','Female', ( TO_Date ( '03/04/1988' , 'DD/MM/yyyy')));
INSERT INTO CUSTOMER VALUES ('3423','Bryan','Male', ( TO_Date ( '15/06/1990' , 'DD/MM/YYYY')));
INSERT INTO CUSTOMER VALUES ('4385','Mohd Shafik','Male',( TO_Date ( '20/08/1993' , 'DD/MM/YYYY')));
INSERT INTO PAYMENT VALUES ('24P','Cash', ( TO_Date ( '11/02/2022' , 'DD/MM/YYYY')),24.50);
INSERT INTO PAYMENT VALUES ('09p','Online Transfer', ( TO_Date ( '08/04/2022' , 'DD/MM/YYYY')),25.00);
INSERT INTO PAYMENT VALUES ('10P','Cash', ( TO_Date ( '08/07/2022' , 'DD/MM/YYYY')),22.50);
The foreign keys are now working , but just for life of me can't figure out why the it spits out ORA-00947: not enough values and
ORA-01400: cannot insert NULL into ("SQL_GUUNNGDQAOXJVYPBKNMILVXJR"."PAYMENT"."PAYMENT_ID") ORA-06512: at "SYS.DBMS_SQL", line 1721
Also, Please explain to me how references works, i read a few places but they use words that just confuse me. Please and thank you!
Use DATE data types to store date values (and VARCHAR2 instead of VARCHAR) in your tables:
CREATE TABLE CUSTOMER(
Customer_ID varchar2(255) NOT NULL,
Customer_Name varchar2(50),
Customer_Gender varchar2(10),
Customer_DOB DATE,
CONSTRAINT CUSTOMER_PK PRIMARY KEY(Customer_ID)
);
CREATE TABLE PAYMENT(
Payment_ID varchar2(255) NOT NULL,
Cust_ID varchar2(255),
Payment_Method varchar2(30),
Payment_Date DATE,
Payment_Total NUMBER(10,2) ,
CONSTRAINT PAYMENT_PK PRIMARY KEY(Payment_ID),
CONSTRAINT fk_customer FOREIGN KEY(Cust_ID) REFERENCES CUSTOMER(Customer_ID)
);
Then name the columns in your INSERT statements:
INSERT INTO CUSTOMER (
customer_id, customer_name, customer_gender, customer_dob
) VALUES (
'1277','Jenny','Female', TO_Date('03/04/1988', 'DD/MM/yyyy')
);
INSERT INTO CUSTOMER (
customer_id, customer_name, customer_gender, customer_dob
) VALUES (
'3423','Bryan','Male', TO_Date('15/06/1990', 'DD/MM/YYYY')
);
INSERT INTO CUSTOMER (
customer_id, customer_name, customer_gender, customer_dob
) VALUES (
'4385','Mohd Shafik','Male', TO_Date('20/08/1993' , 'DD/MM/YYYY')
);
Then for the PAYMENT inserts, you have 5 columns in the table but only 4 pieces of data being inserted:
INSERT INTO PAYMENT (
payment_id, payment_method, payment_date, payment_total
) VALUES (
'24P','Cash', TO_Date('11/02/2022', 'DD/MM/YYYY'),24.50
);
INSERT INTO PAYMENT (
payment_id, payment_method, payment_date, payment_total
) VALUES (
'09p','Online Transfer', TO_Date( '08/04/2022' , 'DD/MM/YYYY'),25.00
);
INSERT INTO PAYMENT (
payment_id, payment_method, payment_date, payment_total
) VALUES (
'10P','Cash', TO_Date('08/07/2022', 'DD/MM/YYYY'),22.50
);
You have not provided a Cust_ID value so it will default to NULL in those rows.
If you want to provide a Cust_ID then add it to the statement:
INSERT INTO PAYMENT (
payment_id, cust_id, payment_method, payment_date, payment_total
) VALUES (
'ABC', '1277', 'Cash', TO_Date('08/07/2022', 'DD/MM/YYYY'),22.50
);
db<>fiddle here
ALTER SESSION SET NLS_DATE_FORMAT = 'DD-MON-YYYY';
CREATE TABLE CUSTOMER
(
Customer_ID varchar(255) NOT NULL,
Customer_Name varchar(50),
Customer_Gender varchar(10),
Customer_DOB DATE ,
CONSTRAINT CUSTOMER_PK PRIMARY KEY(Customer_ID)
) ;
CREATE TABLE PAYMENT
(
Payment_ID varchar(255) NOT NULL,
Customer_ID varchar(255),
Payment_Method varchar(30),
Payment_Date DATE,
Payment_Total NUMBER(10,2) ,
CONSTRAINT PAYMENT_PK PRIMARY KEY(Payment_ID),
CONSTRAINT fk_customer FOREIGN KEY(Customer_ID) REFERENCES CUSTOMER(Customer_ID)
) ;
INSERT INTO CUSTOMER VALUES ('1277','Jenny','Female', ( TO_Date ( '03/04/1988' , 'DD/MM/yyyy')));
INSERT INTO PAYMENT VALUES ('24P','1277','Cash', ( TO_Date ( '11/02/2022' , 'DD/MM/YYYY')),24.50);
SELECT * FROM Customer;
CUSTOMER_ID CUSTOMER_NAME CUSTOMER_GENDER CUSTOMER_DOB
1277 Jenny Female 03-APR-1988
SELECT * FROM payment;
PAYMENT_ID CUSTOMER_ID PAYMENT_METHOD PAYMENT_DATE PAYMENT_TOTAL
24P 1277 Cash 11-FEB-2022 24.5
this one works
create table reservation(
reservation_num number(6) constraint reservation_num_pk primary key,
rdate date,
payment_status varchar(6) constraint payment_status_ck check(payment_status in('paid','unpaid')),
seat_num number(6),
flight_num number(3)
)
but this one fails
create table reservation(
reservation_num number(6) constraint reservation_num_pk primary key,
date date,
payment_status varchar(6) constraint payment_status_ck check(payment_status in('paid','unpaid')),
seat_num number(6),
flight_num number(3)
)
Date is a reserved word. If you really want to use it as column name put it in double quotes
create table reservation(
reservation_num number(6) constraint
reservation_num_pk primary key,
"date" date,
payment_status varchar(6) constraint payment_status_ck
check(payment_status in('paid','unpaid')),
seat_num number(6),
flight_num number(3)
)
CREATE TABLE XX_EMP_COL_CONST
(
EMP_ID NUMBER CONSTRAINT XX_EMP_COL_CONST_PK PRIMARY KEY,
ENAME VARCHAR2(100) CONSTRAINT XX_EMP_COL_CONST_UK1 UNIQUE,
SALARY NUMBER NOT NULL,
GENDER CHAR (1) CONSTRAINT XX_EMP_COL_CONST_CHQ CHECK (GENDER IN ('M','F')),
DEPT_ID NUMBER CONSTRAINT XX_EMP_COL_CONST_FK1 REFERENCE departments(department_id)
);
You can try (you made just a simple mistake: the S after REFERENCE):
CREATE TABLE XX_EMP_COL_CONST (
EMP_ID NUMBER CONSTRAINT XX_EMP_COL_CONST_PK PRIMARY KEY
,ENAME VARCHAR2(100) CONSTRAINT XX_EMP_COL_CONST_UK1 UNIQUE
,SALARY NUMBER NOT NULL
,GENDER CHAR(1) CONSTRAINT XX_EMP_COL_CONST_CHQ CHECK (GENDER IN ('M', 'F'))
,DEPT_ID NUMBER CONSTRAINT XX_EMP_COL_CONST_FK1 REFERENCES departments(department_id)
);
You can see it on http://sqlfiddle.com/#!4/4e896f/1
I'm stuck and can't figure out how to add the condition to the CHECK constraint in oracle...
Basically I have a table with the following structure:
CREATE TABLE TEST_TBL
(
COL_1 VARCHAR2(100) NOT NULL,
COL_2 VARCHAR2(100) NOT NULL,
COL_3 VARCHAR2(100) NOT NULL,
COL_4 VARCHAR2(100) NOT NULL,
DEF_COL CHAR(1) DEFAULT 'Y',
CONSTRAINT def_check_const
CHECK (???????)
);
There may be multiple values in this table, however I require that only 'Y' or 'N' can be entered in the DEF_COL column. Also i want to add a constraint to restrict the number of 'Y' in the DEF_COL column to only one. There may be multiple 'N' entries allowed for DEF_COL column. Please could someone guide me as to what must be done to add the constraint in the check section of the create table query?
Thanks in advance....
Oracle Setup:
CREATE TABLE TEST_TBL(
COL_1 VARCHAR2(100) NOT NULL,
COL_2 VARCHAR2(100) NOT NULL,
COL_3 VARCHAR2(100) NOT NULL,
COL_4 VARCHAR2(100) NOT NULL,
DEF_COL CHAR(1) DEFAULT 'Y'
CONSTRAINT def_check_const
CHECK ( DEF_COL IN ('Y', 'N' ) )
);
CREATE UNIQUE INDEX def_col_only_one_y__U
ON TEST_TBL( CASE DEF_COL WHEN 'Y' THEN 'Y' END );
Insert a row:
INSERT INTO TEST_TBL VALUES ( 'A1', 'A2', 'A3', 'A4', 'Y' );
1 rows inserted.
Insert a second column with DEF_COL = 'Y':
INSERT INTO TEST_TBL VALUES ( 'B1', 'B2', 'B3', 'B4', 'Y' )
Error report -
SQL Error: ORA-00001: unique constraint (TEST.DEF_COL_ONLY_ONE_Y__U) violated
00001. 00000 - "unique constraint (%s.%s) violated"
*Cause: An UPDATE or INSERT statement attempted to insert a duplicate key.
For Trusted Oracle configured in DBMS MAC mode, you may see
this message if a duplicate entry exists at a different level.
*Action: Either remove the unique restriction or do not insert the key.
Update:
To have only one Y for each unique combination of 3 columns then try:
CREATE UNIQUE INDEX C1_2_3__def_col_only_one_y__U
ON TEST_TBL( COL1, COL2, COL3, CASE DEF_COL WHEN 'Y' THEN 'Y' END );