CREATE TABLE Appointment
(
appointID INTEGER,
appoint_date DATE,
appoint_time TIME,
appoint_type VARCHAR(5),
primary key (appointID)
);
INSERT INTO Appointment VALUES(1, '15-Apr-2017', '10:00', 'long');
INSERT INTO Appointment VALUES(2, '15-Apr-2017', '10:30', 'short');
INSERT INTO Appointment VALUES(3, '28-May-2017', '14:00', 'long');
INSERT INTO Appointment VALUES(4, '20-May-2017', '15:00', 'short');
INSERT INTO Appointment VALUES(5, '11-May-2017', '10:30', 'long');
INSERT INTO Appointment VALUES(6, '26-Jun-2017', '9:30', 'short');
INSERT INTO Appointment VALUES(7, '30-Jun-2017', '14:00', 'long');
INSERT INTO Appointment VALUES(8, '30-Jun-2017', '15:30', 'short');
INSERT INTO Appointment VALUES(9, '28-Apr-2017', '16:00', 'short');
INSERT INTO Appointment VALUES(10,'30-Apr-2017', '13:00', 'short');
I keep getting this error when I try to add TIME:
Error starting at line : 24 in command -
CREATE TABLE Appointment(
appointID INTEGER,
appoint_date DATE,
appoint_time TIME,
appoint_type VARCHAR(5),
primary key (appointID)
)
Error report -
ORA-00902: invalid datatype
00902. 00000 - "invalid datatype"
*Cause:
*Action:
I'm also trying to add my DOCTOR TABLE, but I keep getting a Error report
ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis"
*Cause:
*Action:**
create table Doctor
(
appointID INTEGER not null,
regnum CHAR(6),
doc_name VARCHAR(40),
doc_gender CHAR(1),
qual VARCHAR(80),
foreign key (appointID) references Appointment
primary key (appointID, regnum)
);
Here's my suggestion: avoid CHAR datatype unless it makes sense (such as in gender, as you did), as well as VARCHAR >>> use VARCHAR2 instead (personally, I never use CHAR, and have never ever used VARCHAR).
DATE datatype contains both date and time component, so you're safe if you use it.
Columns that make the primary key constraint don't have to have the NOT NULL constraint specified, because primary keys don't allow nulls anyway.
So, here it is, a working example:
SQL> create table appointment
2 (appointid integer constraint pk_app primary key,
3 appoint_date date,
4 appoint_type varchar2(5)
5 );
Table created.
SQL>
SQL> insert into appointment values
2 (1, to_date('15.04.2017 10:00', 'dd.mm.yyyy hh24:mi'), 'long');
1 row created.
SQL>
SQL> create table doctor
2 (appointid integer constraint fk_doc_app references appointment (appointid),
3 regnum varchar2(6),
4 doc_name varchar2(40),
5 doc_gender char(1),
6 qual varchar2(80),
7 --
8 constraint pk_doc primary key (appointid, regnum)
9 );
Table created.
SQL>
There is no data type called 'TIME', you can use timestamp :
CREATE TABLE Appointment
(
appointID INT,
appoint_date DATE,
appoint_time timestamp ,
appoint_type VARCHAR(5),
primary key (appointID)
);
For insert you have to use TO_DATE and TO_TIMESTAMP functions, as what you are inserting is string.:
INSERT INTO Appointment VALUES(3, To_date('15-Apr-2017','DD-MON-YY'), TO_TIMESTAMP('10:00','HH24:MI'), 'long');
http://sqlfiddle.com/#!4/59f5ef
Related
the previous error was fixed. A group member was inserting the Salary amount into 0 precision-specified numbers.
Basically, I want to give an error when the age of an employee is less than 18 or more than 60
please suggest how I can do it.
Trigger statement
create or replace TRIGGER check_birth_date
BEFORE INSERT OR UPDATE ON employee
FOR EACH ROW
BEGIN
IF( months_between(sysdate,:new.DateOfBirth)/12 < 18 or
months_between(sysdate,:new.DateOfBirth)/12 > 60 )
THEN
RAISE_APPLICATION_ERROR(
-20001,
'EMPLOYEE MUST BE ABOVE 18 AND BELOW 60' );
END IF;
END;
The insert statement I am trying to insert
INSERT INTO EMPLOYEE VALUES(1002, 'DAVID', '21/apr/2011', 'M', '21/jun/2010', 28000.00, 'HR', 'HR MANAGER', 77845322, 'C');
The errors
- ORA-06512
- ORA-04088
- ORA-20001
CREATE TABLE STATEMENT
CREATE TABLE EMPLOYEE(
EmployeeID NUMBER(4),
Name VARCHAR2(20),
Hiredate DATE NOT NULL,
Gender VARCHAR(1),
DateOfBirth DATE NOT NULL,
Salary NUMBER(8,2),
DName VARCHAR(20),
PName VARCHAR(20),
Phone NUMBER(8) NOT NULL,
GLETTER VARCHAR(1),
CONSTRAINT EMPLOYEE_EMPLOYEEID_PK PRIMARY KEY(EMPLOYEEID),
CONSTRAINT EMPLOYEE_DNAME_FK FOREIGN KEY(DNAME) REFERENCES DEPARTMENT(DNAME),
CONSTRAINT EMPLOYEE_PNAME_FK FOREIGN KEY(PNAME) REFERENCES POSITION(PNAME),
CONSTRAINT EMPLOYEE_GLETTER_FK FOREIGN KEY(GLETTER) REFERENCES GRADE(GLETTER),
CONSTRAINT GENDER_CK CHECK (GENDER='M' or GENDER='F')
);
Your code is right, if you check your input data and run it by yourself you can see it. I run it and the result is:
You trigger is working fine. I have created employee table without any foreign key constraints (since no reference table is available to me) and the trigger is working as it should be.
It's denying entry of any record with age than 18 and more than 60 but it's allowing any record within age 18 to 60.
Please try to insert below line:
INSERT INTO EMPLOYEE VALUES(1002, 'DAVID', '21/apr/2000', 'M', '21/jun/2000', 28000.00, 'HR', 'HR MANAGER', 77845322, 'C');
I have an assignment due in a few days but cannot remember coming across this error before, im sure its something tiny that ive skimmed over but could someone take a look?
error is :
ORA-00984: column not allowed here ORA-06512: at
"SYS.WWV_DBMS_SQL_APEX_190100", line 590 ORA-06512: at
"SYS.DBMS_SYS_SQL", line 1658 ORA-06512: at
"SYS.WWV_DBMS_SQL_APEX_190100", line 576 ORA-06512: at
"APEX_190100.WWV_FLOW_DYNAMIC_EXEC", line 2033
ive tried rewriting single lines but it will not take any code that i put into it. The code i have written is in asterisks (bottom 14 lines) it works fine without these lines so i cannot figure out why. Im a newbie to this kind of stuff (first year uni) so any help would be appreciated!
CREATE TABLE vet(
vetno NUMBER(6) PRIMARY KEY,
vetname VARCHAR2(16) NOT NULL,
vetaddressln1 VARCHAR2(20)NOT NULL,
vetaddressln2 VARCHAR2(20),
vetpostcode VARCHAR2(8)NOT NULL);
DROP TABLE client cascade constraints;
CREATE TABLE client(
clientno NUMBER(6)PRIMARY KEY,
surname VARCHAR2(15)NOT NULL,
firstnames VARCHAR2(15)NOT NULL,
clientaddressln1 VARCHAR2(20)NOT NULL,
clientaddressln2 VARCHAR2(20),
clientpostcode VARCHAR2(8)NOT NULL);
DROP TABLE animal cascade constraints;
CREATE TABLE animal(
animalno NUMBER(6) PRIMARY KEY,
name VARCHAR2(15)NOT NULL ,
sex VARCHAR2(6),
dateofbirth DATE,
species VARCHAR2(30)NOT NULL ,
clientno NUMBER(6) REFERENCES client(clientno));
DROP TABLE consultation cascade constraints;
CREATE TABLE consultation(
consultationno NUMBER(6),
consultationdate DATE NOT NULL,
animalno NUMBER(6) REFERENCES animal(animalno),
vetno NUMBER(6) REFERENCES vet(vetno),
Outcomenote VARCHAR2(25) NOT NULL );
INSERT INTO vet VALUES (001,'James Herriot','Skeldale, High St', 'Yarm', 'YM3 6WP');
INSERT INTO vet VALUES (002,'Siegfried Farnon','61 Farnby Mount', 'Yarm', 'YM3 6WD');
INSERT INTO vet VALUES (003,'Danielle Chang','North St', 'Leeds', 'LS6 3NG');
INSERT INTO vet VALUES (004,'Emma Milne','87 Arncliffe Road', 'Harrogate', 'HG5 5HY');
INSERT INTO vet VALUES (005,'Harry Cooper','15, Coniston Avenue', 'Leeds', 'LS11 4PE');
INSERT INTO client VALUES (0034,'Wong','Judy','Flat 5, Victoria Av', 'Leeds', 'LS5 9PL');
INSERT INTO client VALUES (0035,'Spencer','Tom','4, Broad Lane', 'Harrogate', 'HG4 9DL');
INSERT INTO client VALUES (0036,'Hamza','Farhan','35A, Waterloo Cres', 'Harrogate', 'HG3 3FD');
INSERT INTO client VALUES (0037,'Cummins','Dominic','184, Queenswood Rd', 'Bradford', 'BR3 2GD');
INSERT INTO client VALUES (0038,'Stuart','Moira','77, Westgate', 'Leeds', 'LS1 4KL');
** INSERT INTO animal VALUES (00100,'Elsie','M', 01-FEB-13, 'PERSIAN CAT', '0034');
INSERT INTO animal VALUES (00101,'Thurston','M', 15-MAY-11, 'MAINE COON CAT', '0034');
INSERT INTO animal VALUES (00102,'Jeff','F', 11-SEP-19, 'NORWEIGAN FOREST CAT', '0035');
INSERT INTO animal VALUES (00103,'Monkey','M', 16-SEP-99, 'BOMBAY CAT', '0036');
INSERT INTO animal VALUES (00104,'Terry','M', 11-MAY-14, 'RED-FOOTED TORTOISE', '0037');
INSERT INTO animal VALUES (00105,'Emilia','F', 29-OCT-19, 'RED-KNEE TARANTULA', '0038');
INSERT INTO consultation VALUES (001001,14-jan-20,'00101','001','Given antibiotics');
INSERT INTO consultation VALUES (001002,11-JUL-20,'00101','002','Given respiratory tablets');
INSERT INTO consultation VALUES (001003,08-JUN-20,'00102','003','Given antibiotics');
INSERT INTO consultation VALUES (001004,11-AUG-20,'00103','005','Saline Drip');
INSERT INTO consultation VALUES (001005,11-SEP-20,'00104','001','Laid 3 eggs');
INSERT INTO consultation VALUES (001006,17-NOV-20,'00102','004','Given antibiotics');
INSERT INTO consultation VALUES (001007,11-DEC-20,'00105','004','Moult Assistance');
INSERT INTO consultation VALUES (001008,04-OCT-20,'00101','005','Given antibiotics');** ```
It is about invalid dates. The way you tried to do it is wrong - not just because it didn't work, but because you wanted to insert them as strings (only if they were enclosed into single quotes). You should insert dates.
One option is to use date literal which is always in format date 'yyyy-mm-dd':
SQL> INSERT INTO animal VALUES (00100,'Elsie','M', date '2013-02-01', 'PERSIAN CAT', '0034');
1 row created.
SQL>
Another is to use TO_DATE function with appropriate format mask:
SQL> INSERT INTO animal VALUES (00101,'Thurston','M', to_date('15-MAY-11', 'dd-mon-yy', 'nls_date_language = english'), 'MAINE COON CAT', '0034');
1 row created.
SQL>
The same goes for the consultation table.
I have to construct and populate a supertype-subtype relationship, but I cannot get it to work the way it is supposed to be. Basically PERSON table is the supertype of table STUDENT and TEACHER(subtypes). But a person can be either a student or a teacher.
Attributes:
PERSON(p_id, name, dob)
STUDENT (s_id, p_id, grade)
TEACHER(t_id, p_id, tel)
Both student and teacher should have names and DOB's along with the p_id as a foreign key, but if it exist on one table it shouldn't be on the other
CREATE TABLE PERSON ( -- SUPERTYPE
p_id NUMBER(2) CONSTRAINT c1 PRIMARY KEY,
name CHAR(15),
dob DATE
);
CREATE TABLE STUDENT ( -- SUBTYPE
s_id NUMBER(2) CONSTRAINT c2 PRIMARY KEY,
p_id_fk,
grade CHAR(1),
FOREIGN KEY (p_id_fk) REFERENCING PERSON (p_id)
);
CREATE TABLE TEACHER( -- SUBTYPE
t_id NUMBER(4) CONSTRAINT c3 PRIMARY KEY,
p_id_fk,
tel CHAR(8),
FOREIGN KEY (p_id_fk) REFERENCING PERSON (p_id)
);
INSERT INTO PERSON VALUES (11, 'John', to_date('12/12/12', 'dd/mm/yy'));
INSERT INTO PERSON VALUES (22, 'Maria', to_date('01/01/01', 'dd/mm/yy'));
INSERT INTO PERSON VALUES (33, 'Philip', to_date('02/02/02', 'dd/mm/yy'));
INSERT INTO STUDENT VALUES (98, 11, 'A');
INSERT INTO TEACHER VALUES (1234, 11, 14809510);
How to prevent Person 11 (John) from existing in both tables?
One option is to use database triggers, one for each table (STUDENT and TEACHER); they look the same:
Trigger on STUDENT:
SQL> create or replace trigger trg_bi_stu
2 before insert on student
3 for each row
4 declare
5 l_cnt number;
6 begin
7 -- inserting into STUDENT: check whether that person exists in TEACHER table
8 select count(*)
9 into l_cnt
10 from teacher
11 where p_id_fk = :new.p_id_fk;
12
13 if l_cnt > 0 then
14 raise_application_error(-20001, 'That person is a teacher; can not be a student');
15 end if;
16 end;
17 /
Trigger created.
Trigger on TEACHER:
SQL> create or replace trigger trg_bi_tea
2 before insert on teacher
3 for each row
4 declare
5 l_cnt number;
6 begin
7 -- inserting into TEACHER: check whether that person exists in STUDENT table
8 select count(*)
9 into l_cnt
10 from student
11 where p_id_fk = :new.p_id_fk;
12
13 if l_cnt > 0 then
14 raise_application_error(-20001, 'That person is a student; can not be a teacher');
15 end if;
16 end;
17 /
Trigger created.
SQL>
Testing:
SQL> INSERT INTO STUDENT VALUES (98, 11, 'A');
1 row created.
SQL>
SQL> INSERT INTO TEACHER VALUES (1234, 11, 14809510);
INSERT INTO TEACHER VALUES (1234, 11, 14809510)
*
ERROR at line 1:
ORA-20001: That person is a student; can not be a teacher
ORA-06512: at "SCOTT.TRG_BI_TEA", line 11
ORA-04088: error during execution of trigger 'SCOTT.TRG_BI_TEA'
SQL>
Use a materialized view with an appropriate constraint to check complex requirements such as this.
For example, let's create the tables and a materialized view, add data to the tables, and refresh the MV:
CREATE TABLE PERSON ( -- SUPERTYPE
p_id NUMBER(2) CONSTRAINT c1 PRIMARY KEY,
name CHAR(15),
dob DATE
);
CREATE TABLE STUDENT ( -- SUBTYPE
s_id NUMBER(2) CONSTRAINT c2 PRIMARY KEY,
p_id_fk,
grade CHAR(1),
FOREIGN KEY (p_id_fk) REFERENCING PERSON (p_id)
);
CREATE TABLE TEACHER( -- SUBTYPE
t_id NUMBER(4) CONSTRAINT c3 PRIMARY KEY,
p_id_fk,
tel CHAR(8),
FOREIGN KEY (p_id_fk) REFERENCING PERSON (p_id)
);
CREATE MATERIALIZED VIEW PERSON_MV
REFRESH COMPLETE
AS SELECT p.P_ID,
s.S_ID,
t.T_ID
FROM PERSON p
LEFT OUTER JOIN STUDENT s
ON s.P_ID_FK = p.P_ID
LEFT OUTER JOIN TEACHER t
ON t.P_ID_FK = p.P_ID;
-- Add constraint to the table underlying the MV
ALTER MATERIALIZED VIEW PERSON_MV
ADD CONSTRAINT PERSON_MV_CK1
CHECK( (S_ID IS NULL AND -- either both are NULL
T_ID IS NULL) OR
( (S_ID IS NULL OR -- or only one is NULL
T_ID IS NULL) AND
(S_ID IS NOT NULL OR
T_ID IS NOT NULL)));
INSERT ALL
INTO PERSON (P_ID, NAME, DOB) VALUES (11, 'John', to_date('12/12/2012', 'dd/mm/yyyy'))
INTO PERSON (P_ID, NAME, DOB) VALUES (22, 'Maria', to_date('01/01/2001', 'dd/mm/yyyy'))
INTO PERSON (P_ID, NAME, DOB) VALUES (33, 'Philip', to_date('02/02/2002', 'dd/mm/yyyy'))
SELECT * FROM DUAL;
COMMIT;
INSERT INTO STUDENT VALUES (98, 11, 'A');
COMMIT;
BEGIN
DBMS_MVIEW.REFRESH('PERSON_MV', 'C', '', TRUE, FALSE, 0, 0, 0, FALSE, FALSE);
END;
/
SELECT *
FROM PERSON_MV;
Note the constraint added to the materialized view:
ALTER MATERIALIZED VIEW PERSON_MV
ADD CONSTRAINT PERSON_MV_CK1
CHECK( (S_ID IS NULL AND -- either both are NULL
T_ID IS NULL) OR
( (S_ID IS NULL OR -- or only one is NULL
T_ID IS NULL) AND
(S_ID IS NOT NULL OR
T_ID IS NOT NULL)));
This constraint allows data to exist where:
a PERSON row exists, but neither a related STUDENT or TEACHER row exists
a PERSON row exists along with either a related STUDENT or TEACHER row, but not both
So when we execute the final SELECT from the materialized view we get:
P_ID S_ID T_ID
11 98 -
33 - -
22 - -
Now let's modify the script above, adding the following just after the INSERT INTO STUDENT:
INSERT INTO TEACHER VALUES (1234, 11, 14809510);
COMMIT;
If we re-run the entire script, we find that when DBMS_MVIEW.REFRESH is called to refresh the materialized view we get:
ORA-12008: error in materialized view or zonemap refresh path ORA-06512: at "SYS.DBMS_SNAPSHOT_KKXRCA", line 3012
ORA-06512: at "SYS.DBMS_SNAPSHOT_KKXRCA", line 2424
ORA-06512: at "SYS.DBMS_SNAPSHOT_KKXRCA", line 88
ORA-06512: at "SYS.DBMS_SNAPSHOT_KKXRCA", line 253
ORA-06512: at "SYS.DBMS_SNAPSHOT_KKXRCA", line 2405
ORA-06512: at "SYS.DBMS_SNAPSHOT_KKXRCA", line 2968
ORA-06512: at "SYS.DBMS_SNAPSHOT_KKXRCA", line 3255
ORA-06512: at "SYS.DBMS_SNAPSHOT_KKXRCA", line 3287
ORA-06512: at "SYS.DBMS_SNAPSHOT", line 16
ORA-06512: at line 2
ORA-06512: at "SYS.DBMS_SQL", line 1721
This is Oracle's rather long-winded way to say that the constraint was violated.
See this LiveSQL Oracle session
Following on from #Bob's materialized view approach, a version that catches the issue on commit might look like this:
CREATE MATERIALIZED VIEW LOG ON STUDENT WITH PRIMARY KEY, ROWID;
CREATE MATERIALIZED VIEW LOG ON TEACHER WITH PRIMARY KEY, ROWID;
CREATE MATERIALIZED VIEW PERSON_HACK (p_id_fk, marker, rid)
BUILD IMMEDIATE
REFRESH ON COMMIT AS
SELECT p_id_fk, 1, ROWID FROM STUDENT
UNION ALL
SELECT p_id_fk, 2, ROWID FROM TEACHER;
ALTER MATERIALIZED VIEW PERSON_HACK
ADD CONSTRAINT PERSON_HACK_PK PRIMARY KEY (p_id_fk);
That should act similarly to a deferred constraint, erroring when the MV is refreshed (and its primary key violated) on commit. With concurrent inserts the second session to commit would see the error.
Live SQL, though it seems to have some issues with MVs (reported elsewhere). It should report the constraint violation, not throw ORA-12008. But I don't currently have access to test elsewhere - neither SQL Fiddle or db<>fiddle allow MVs to be created.
Of course, if you haven't been taught about MVs yet then using one in an assignment might look a bit odd. It seems a bit more likely that you've been taught about objects and the assignment is expecting those - even though they are very rarely used in the real world (within the DB), they seem to be taught anyway, along with old join syntax and other bad practices...
There exists four ways how to map an inheritance to relational database.
You are using the most exotic third option, which is causing the problem. Consider to switch to one of the other options as a solution.
The first three are well understood and documented, this page provides usefull links.
Basically you can map
1) all classes in one table
2) use one table for all concrete classes
3) define one table for aech class
All this options have some problems either with excesive joins or with deactivated constraints (e.g. you can't define non nullable columns in option 1), so for completness the option 4) is do not use inheritance in relational database.
You have tried to implement the option 3), but the problem is that all tables must inherite the same primary key (to enfornce 1:1 relation) and use this primary key as a foreign key.
Here the overview of all options for your example
-- 1) single table
CREATE TABLE PERSON (
p_id NUMBER(2) CONSTRAINT pers_pk PRIMARY KEY,
name CHAR(15),
dob DATE,
grade CHAR(1),
tel CHAR(8),
person_type VARCHAR2(10) CONSTRAINT pers_type CHECK (person_type in ('STUDENT','TEACHER'))
);
-- 2) table per concrete class
CREATE TABLE STUDENT (
p_id NUMBER(2) CONSTRAINT stud_pk PRIMARY KEY,
name CHAR(15),
dob DATE,
grade CHAR(1)
);
CREATE TABLE TEACHER(
p_id NUMBER(2) CONSTRAINT tech_pk PRIMARY KEY,
name CHAR(15),
dob DATE,
tel CHAR(8)
);
-- 3) table per class
CREATE TABLE PERSON (
p_id NUMBER(2) CONSTRAINT pers_pk PRIMARY KEY,
name CHAR(15),
dob DATE
);
CREATE TABLE STUDENT (
p_id NUMBER(2) CONSTRAINT stud_pk PRIMARY KEY,
grade CHAR(1),
FOREIGN KEY (p_id) REFERENCING PERSON (p_id)
);
CREATE TABLE TEACHER(
p_id NUMBER(2) CONSTRAINT tech_pk PRIMARY KEY,
tel CHAR(8),
FOREIGN KEY (p_id) REFERENCING PERSON (p_id)
);
INSERT INTO PERSON (P_ID, NAME, DOB) VALUES (11, 'John', to_date('12/12/2012', 'dd/mm/yyyy'));
INSERT INTO PERSON (P_ID, NAME, DOB) VALUES (22, 'Maria', to_date('01/01/2001', 'dd/mm/yyyy'));
INSERT INTO PERSON (P_ID, NAME, DOB) VALUES (33, 'Philip', to_date('02/02/2002', 'dd/mm/yyyy'));
INSERT INTO STUDENT (P_ID, GRADE) VALUES (11, 'A');
INSERT INTO TEACHER (P_ID, TEL) VALUES (11, 14809510);
I am trying to create a new table and insert values into it however I keep receiving "#1241 - Operand should contain 1 column(s)".
Please could someone help to identify what is wrong with my code as I am unsure what this error is referencing?
The code I am inserting into the phpMyAdmin database under the SQL tab. I have tried to remove the auto increment attributes and have tried looking at other examples to check my syntax, but I can't spot the issue. Some guidance on this would be greatly appreciated.
The code I entered begins like this:
# AppSoft Project - Greg Roberts
DROP table if exists Department;
DROP table if exists Role;
DROP table if exists User;
DROP table if exists Appraisal;
DROP table if exists Question;
DROP table if exists Answer;
CREATE table Department(
DeptID int NOT NULL AUTO_INCREMENT,
DeptName varchar(30) NOT NULL,
primary key (DeptID));
INSERT into Department values(
(00, 'SuperAdmin'),
(01, 'Support Staff'),
(02, 'Teaching Staff'),
(03, 'SLT'));
CREATE table Role(
RoleID int NOT NULL AUTO_INCREMENT,
RoleTitle varchar(30) NOT NULL,
primary key (RoleID));
INSERT into Role values(
(00, 'SuperAdmin'),
(01, 'Office Administrator'),
(02, 'Teaching Partner'),
(03, 'Mid Day Supervisor'),
(04, 'Cooks'),
(05, 'Cleaners'),
(06, 'Maintenance'),
(07, 'Teacher'),
(08, 'Department Head'),
(09, 'SENCO'),
(10, 'Head Teacher'),
(11, 'Executive Head'));
Error Code that Occurs
You dont need to insert primary keys, if they are set to auto_increment.
DeptID int NOT NULL AUTO_INCREMENT
Just insert the department name, there are no additional braces required for Values.
INSERT into Department( DeptName) values
('SuperAdmin'),
('Support Staff'),
('Teaching Staff'),
('SLT');
You would need to do the same for Role table as well.
Also if you try to insert 0 into the primary key, it will actually insert 1 you can read about it in the Standard Docs
you seem to be getting the error because your first record inserts 1 into the table and then your second record tries to insert 1 again in the primary key column
drop table Employee;
CREATE TABLE Employee
( EmployeeID integer,
FirstName varchar(24),
LastName varchar(24),
Email varchar(48),
PhoneNumber varchar(12),
HotelID integer
PRIMARY KEY (EmployeeID),
);
INSERT INTO Employee VALUES (1, ‘James’, ‘Jenkins’, ‘jj#gmail.com’, ’0412181111’, 1);
INSERT INTO Employee VALUES (22, ‘Roy’, ‘Bond’, ‘jb#gmail.com’, ‘0418246192’, 1);
INSERT INTO Employee VALUES (14, ‘Rachel’, ‘Green’, ‘rg#gmail.com’, ‘0468129367’, 1);
INSERT INTO Employee VALUES (96, ‘Eddard’, ‘Stark’, ‘es#gmail.com’, ‘0458192716’, 1);
INSERT INTO Employee VALUES (77, ‘Anna’, ‘Nguyen’, ‘an#gmail.com’ , ‘0418238694’, 1);
Error: "psql:employee:1: ERROR: table "employee" does not exist"
What is the way that the error can be fixed?
Link to the entire doc if anyone wants to take a look: https://docs.google.com/document/d/1r4E7yz4XJxLmO3rmkH4YBVOGfYN5PkhcDSJUyuy7qxw/edit?usp=sharing
Your create statement has a comma(,) missing before declaring the primary key. Therefore the table is not getting created. This this:
CREATE TABLE Employee
( EmployeeID integer,
FirstName varchar(24),
LastName varchar(24),
Email varchar(48),
PhoneNumber varchar(12),
HotelID integer,
PRIMARY KEY (EmployeeID),
FOREIGN KEY (HotelID) REFERENCES Hotel
);
The table Employee might not be already created in your database when you are trying to run above snippet. Always have an IF EXISTS check before dropping a table or stored procedure or function.
Your drop query should be.
drop table if exists `Employee`;