I am trying to test my queries based on this table:
--Create Person Table
CREATE TABLE Person
(
Person_ID NUMBER(10) PRIMARY KEY,
First_Name NUMBER(15) NULL,
Last_Name VARCHAR2(15) NULL,
Middle_Name VARCHAR2(15) NULL,
Street_Address VARCHAR(35) NULL,
City VARCHAR2(10) NULL,
State VARCHAR2(2) NULL,
Zipcode VARCHAR2(5) NULL,
Country CHAR(2) NOT NULL,
Birth_Date DATE NOT NULL,
Gender CHAR(1) NOT NULL,
Phone VARCHAR2(10) NULL,
Email VARCHAR2(25) NULL,
Is_Patient CHAR(1) NULL,
Is_Physician CHAR(1) NULL,
Is_Employee CHAR(1) NULL,
Is_Volunteer CHAR(1) NULL
);
And this is what I input:
INSERT INTO PERSON (Person_ID, First_Name, Last_Name, Middle_Name, Street_Address,
City, State, Zipcode, Country, Birth_Date, Gender, Phone,
Email, Is_Patient, Is_Physician, Is_Employee, Is_Volunteer)
VALUES
(1234, 'Jill', 'Palmer', 'Wendy', '123 Apple Court',
'Bronx', 'NY', '10472', 'USA', date '1956-09-15', 'F', '9178546215',
'heygirl5#gmail.com', 'Y', 'N', 'N','N');
VALUES
(2345, 'Charles', 'Connor', 'Vanny', '87455 Lemon Drive',
'Queens', 'NY', '12047', 'USA', date '1953-10-08', 'M', '9178546200',
'sugahsugah#gmail.com', 'N', 'N', 'Y','N');
VALUES
(3456, 'Chaniece', 'Boone', 'Pia', '94102 Grape Meaows',
'Brooklyn', 'NY', '12047', 'USA', date '1983-03-31', 'F', '3479845102',
'byenow#gmail.com', 'N', 'Y', 'N','N');
VALUES
(4567, 'James', 'Boone', 'Wayck', '2010 Orange Place',
'Manhattan', 'NY', '10026', 'USA', date '1981-08-03', 'M', '7187884411',
'howyoudoin#gmail.com', 'Y', 'N', 'N','N');
VALUES
(5678, 'Charlton', 'Connor', 'Ethan', '94165 Mango Lane',
'Staten Island', 'NY', '30124', 'USA', date '1988-12-10', 'M', '6469820145',
'youcrazyboy#gmail.com', 'Y', 'N', 'N','Y');
SELECT *
FROM PERSON;
I am getting an error of invalid number and I don't see why. Nothing is coming up on my test also.
You defined First_Name as NUMBER(15). You probably meant VARCHAR2(15).
Also, you have a mix of CHAR, VARCHAR and VARCHAR2. These should all be VARCHAR2. (CHAR will seem to work, but it just causes problems for no added benefit).
create table person
( person_id number(10) primary key
, first_name varchar2(15) -- Not number
, last_name varchar2(15)
, middle_name varchar2(15)
, street_address varchar2(35)
, city varchar2(30) -- Increased from 10
, state varchar2(2)
, zipcode varchar2(5)
, country varchar2(3) not null -- increased size to 3 to fit 'USA'
, birth_date date not null
, gender varchar2(1)
, phone varchar2(10)
, email varchar2(25)
, is_patient varchar2(1)
, is_physician varchar2(1)
, is_employee varchar2(1)
, is_volunteer varchar2(1)
);
insert into person
( person_id
, first_name, last_name, middle_name
, street_address, city, state, zipcode, country
, birth_date
, gender, phone, email
, is_patient, is_physician, is_employee, is_volunteer )
values
( 1234 -- Not '1234'
, 'Jill', 'Palmer', 'Wendy'
, '123 Apple Court', 'Bronx', 'NY', '10472', 'USA'
, to_date('09/15/1956', 'MM/DD/YYYY') -- Corrected format mask (alternatively: date '1956-09-15')
, 'F'
, '9178546215'
, 'heygirl5#gmail.com'
, 'Y', 'N', 'N', 'N' -- Not 'YES'/'NO' because the columns are 1 character
);
Maybe some more of the columns should be declared NOT NULL. Is it really OK for a person not to have a name or address or anything else besides a country and birth date? (You don't need to specify NULL for a nullable column, and doing so makes it harder to tell which ones are mandatory.)
Probably most of the name columns should be longer.
Ideally Y/N columns should be mandatory and enforced by check constraints, e.g.
is_patient varchar2(1) not null constraint is_patient_yn_chk check (is_patient in ('Y','N'))
The exact error you are seeing is probably being caused by that you are trying to insert a string value into the Person_ID column, which you have defined as numeric. Beyond this, you should always explicitly list the columns for the insert:
INSERT INTO PERSON (Person_ID, First_Name, Last_Name, Middle_Name, Street_Address,
City, State, Zipcode, Country, Birth_Date, Gender, Phone,
Email, Is_Patient, Is_Physician, Is_Employee, Is_Volunteer)
VALUES
(1234, 'Jill', 'Palmer', 'Wendy', '123 Apple Court',
'Bronx', 'NY', '10472', 'USA', date '1956-09-15', 'F', '9178546215',
'heygirl5#gmail.com', 'Y', 'N', 'N','N');
Also note that for the Is_xxx columns, you should only be inserting a 'Y' or 'N' value, as you have defined these columns to be CHAR(1).
Related
I have created a table 'USER' in Oracle SQL Developer.
CREATE TABLE USER(
USER_ID INTEGER NOT NULL,
FIRST_NAME VARCHAR2(50) NOT NULL,
LAST_NAME VARCHAR2(50) NOT NULL,
ID_CODE INTEGER NOT NULL,
DATE_OF_BIRTH TIMESTAMP NOT NULL,
TRAINING_CLUB VARCHAR2(50) NOT NULL,
HAVE_LICENCE NUMBER(1) NOT NULL,
AGE_GROUP VARCHAR2(3) NULL,
SPECIALITY VARCHAR2(100) NOT NULL,
COMMENT VARCHAR2(200) NULL,
CONSTRAINT USER_PK PRIMARY KEY (USER_ID)
);
Now I'd like to insert data in it, but it will only accept date which is 2000 or more and when it's less than 2000, for example '1998-13-07', it shows not a valid month.
INSERT INTO USER(USER_ID, FIRST_NAME, LAST_NAME, ID_CODE, DATE_OF_BIRTH, TRAINING_CLUB, HAVE_LICENCE, AGE_GROUP, SPECIALITY) VALUES (1, 'Mari', 'Mets', 4990713134, '1999-13-07', 'Erki Noole Athletics club', 1, 'U23', 'Sprint, hurdle race' );
INSERT INTO USER(USER_ID, FIRST_NAME, LAST_NAME, ID_CODE, DATE_OF_BIRTH, TRAINING_CLUB, HAVE_LICENCE, AGE_GROUP, SPECIALITY) VALUES (2, 'Meelis', 'Valgepea', 39704230213, '1997-23-04', 'Runningpartner', 1, 'M', 'Middle- ja long distance running');
INSERT INTO USER(USER_ID, FIRST_NAME, LAST_NAME, ID_CODE, DATE_OF_BIRTH, TRAINING_CLUB, HAVE_LICENCE, AGE_GROUP, SPECIALITY) VALUES (3, 'Karina', 'Justinov', 6020330872, '2002-30-03', 'SK Fortis', 1,'U20','Long and high jump');
INSERT INTO USER(USER_ID, FIRST_NAME, LAST_NAME, ID_CODE, DATE_OF_BIRTH, TRAINING_CLUB, HAVE_LICENCE, AGE_GROUP, SPECIALITY) VALUES (4, 'Lewis', 'Kordon', 38711120678, '1987-12-11', 'Niidupargi Athletics club', 1, 'M', 'Shot put');
INSERT INTO USER(USER_ID, FIRST_NAME, LAST_NAME, ID_CODE, DATE_OF_BIRTH, TRAINING_CLUB, HAVE_LICENCE, AGE_GROUP, SPECIALITY) VALUES (5, 'Getter', 'Tihhikov', 49802280417, '1998-28-02', 'Trainingpartner', 1, 'W','Long distance running');
INSERT INTO USER(USER_ID, FIRST_NAME, LAST_NAME, ID_CODE, DATE_OF_BIRTH, TRAINING_CLUB, HAVE_LICENCE, AGE_GROUP, SPECIALITY) VALUES (6, 'Andres', 'Allikvee', 50603212165, '2006-21-03', 'SK Lindon', 1, 'U16', 'Pole vault');
It shows that row 3 and 6 will be added and 1, 2, 4, 5 have not valid month.
create table users(
user_id integer not null,
date_of_birth timestamp not null
);
insert into users values (1, '1999-13-07');
-- ORA-01843: not a valid month
The problem is with the date literal. We are giving a literal string for a timestamp column, so how this is interpreted depends on database settings.
We can either explictly turn the strings to timestamps, providing the proper format specification:
into users values (1, to_timestamp('1999-13-07', 'YYYY-DD-MM'));
-- 1 rows affected
Or we can change the default timestamp format for our current session, so strings are correctly implicitly converted:
alter session set nls_timestamp_format = 'YYYY-DD-MM';
insert into users values (2, '1999-13-07');
-- 1 rows affected
Demo on DB Fiddle
I keep getting the message "ORA-02290: check constraint violated" whenever I try to insert values into my table.
Here's the code for the table STORE:
CREATE TABLE STORE
(
StoreID INT PRIMARY KEY,
StoreName VARCHAR2(30) NOT NULL,
City VARCHAR2(30) NOT NULL,
Country VARCHAR2(30) NOT NULL CHECK (Country IN('Singapore','India','United States','Peru','Phillippines','People''s Republic of China','Canada')),
Phone VARCHAR2(30) NOT NULL,
Fax VARCHAR2(30),
Email VARCHAR2(50) UNIQUE,
Contact VARCHAR2(30) NOT NULL,
UNIQUE (StoreName, City)
);
And here's the INSERT statements I am trying to accomplish:
INSERT INTO STORE(StoreID, StoreName, City, Country, Phone, Fax, Email, Contact)
VALUES (1, 'Eastern Sales', 'Singapore', 'Singapore', '65-543-1233', '65-543-1239', 'Sales#EasternSales.com.sg', 'Jeremy');
INSERT INTO STORE(StoreID, StoreName, City, Country, Phone, Fax, Email, Contact)
VALUES (2, 'Eastern Treasures', 'Manila', 'Philippines', '63-2-654-2344', '63-2-654-2349', 'Sales#EasternTreasures.com.ph', 'Gracielle');
INSERT INTO STORE(StoreID, StoreName, City, Country, Phone, Fax, Email, Contact)
VALUES (3, 'Jade Antiques', 'Singapore', 'Singapore', '65-543-3455', '95-543-3459', 'Sales#JadeAntiques.com.sg', 'Swee Lai');
INSERT INTO STORE(StoreID, StoreName, City, Country, Phone, Fax, Email, Contact)
VALUES (4, 'Andes Treasures', 'Lima', 'Peru', '51-14-765-4566', '51-14-765-4569', 'Sales#AndesTreasures.com.pe', 'Juan Carlos');
INSERT INTO STORE(StoreID, StoreName, City, Country, Phone, Fax, Email, Contact)
VALUES (5, 'Eastern Sales', 'Hong Kong', 'People''s Republic of China', '852-876-5677', '852-876-5679', 'Sales#EasternSales.com.hk', 'Sam');
INSERT INTO STORE(StoreID, StoreName, City, Country, Phone, Fax, Email, Contact)
VALUES (6, 'Eastern Treasures', 'New Delhi', 'India', '91-11-987-6788', '91-11-987-6789', 'Sales#EasternTreasures.com.in', 'Deepinder');
INSERT INTO STORE(StoreID, StoreName, City, Country, Phone, Fax, Email, Contact)
VALUES (7, 'European Imports', 'New York City', 'United States', '800-432-8766', '800-432-8769', 'Sales#EuropeanImports.com.sg', 'Marcello');
It has created the 4th, 6th, and 7th values, but not the rest. What is going wrong?
You are violating the CHECK constraint as the values allowed doesn't match with the value you are trying to insert:
CHECK (Country IN('Singapore','India','United States','Peru','Phillippines','People''s Republic of China','Canada'))
VALUES (2, 'Eastern Treasures', 'Manila', 'Philippines', '63-2-654-2344', '63-2-654-2349', 'Sales#EasternTreasures.com.ph', 'Gracielle')
'Phillippines' in the constraint doesn't match with 'Philippines' in the insert statement.
Correct the country name in the CHECK constraint to 'Philippines' and it will work fine:
CREATE TABLE STORE
(
StoreID INT PRIMARY KEY,
StoreName VARCHAR2(30) NOT NULL,
City VARCHAR2(30) NOT NULL,
Country VARCHAR2(30) NOT NULL
CHECK (Country IN(
'Singapore','India','United States','Peru','Philippines','People''s Republic of China','Canada')),
Phone VARCHAR2(30) NOT NULL,
Fax VARCHAR2(30),
Email VARCHAR2(50) UNIQUE,
Contact VARCHAR2(30) NOT NULL,
UNIQUE (StoreName, City)
);
CREATE TABLE DBD_D_155150401111052.STUDENT(
STUDENT_ID VARCHAR(20) NOT NULL,
PRIMARY KEY (STUDENT_ID),
STATE_ID CHAR(3),
CONSTRAINT STATE_ID FOREIGN KEY (STATE_ID) REFERENCES DBD_D_155150401111052.PROPINSI (STATE_ID),
CITY_ID CHAR(3),
CONSTRAINT CITY_ID FOREIGN KEY (CITY_ID) REFERENCES DBD_D_155150401111052.KOTA (CITY_ID),
NAME VARCHAR(45),
BIRTH_DATE DATE,
COURSE VARCHAR(10)
);
INSERT INTO DBD_D_155150401111052.STUDENT (STUDENT_ID, NAME, BIRTH_DATE, CITY_ID, STATE_ID, COURSE)
VALUES ('1111171', 'DEMILOV', '1997-05-01', 'A01', '001', 'SI-A'),
('1111172', 'MICHAEL', '1996-03-20', 'A01', '001', 'SI-A'),
('1111181', 'IRVING', '1998-10-14', 'C01', '003', 'IF-B'),
('1111182', 'JHON', '1998-08-15', 'B01', '002', 'IF-C'),
('1111191', 'THEODORE', '1996-01-02', 'A03', '001', 'IF-A'),
('1111192', 'SALLY', '1995-12-31', 'A02', '001', 'SI-B'),
('1111212', 'JASON', '1997-07-28', 'A02', '001', 'IF-B'),
('1111213', 'DAVID', '1998-11-11', 'B02', '002', 'IF-C'),
('1111214', 'JAMES', '1997-12-12', 'C01', '003', 'SI-B'),
('1111215', 'HELENA', '1996-06-08', 'C01', '003', 'IF-B');
UPDATE TABLE DBD_D_155150401111052.STUDENT
SET COURSE = 'SI-B'
WHERE STUDENT_ID = '1111171';
I can't execute the last query.
The warning says "JOIN" was expected instead of ".", but I have no idea how to fix it.
The table value can't updated because table name include schema, is there any way to update the value?
TABLE is not part of UPDATE syntax. I would suggest you try:
UPDATE DBD_D_155150401111052.STUDENT
SET COURSE = 'SI-B'
WHERE STUDENT_ID = '1111171';
Table
is a Reserved word try this :
UPDATE [DBD_D_155150401111052.STUDENT]
SET COURSE = 'SI-B'
WHERE STUDENT_ID = '1111171';
Using Table aliases, list the first name, last name and start date of students enrolled on the java fundamentals module:
I am having some trouble when running the query below.
SELECT stu.StudFName, stu.StudLName, enrol.StartDate
From Student stu
INNER JOIN Enrolment enrol
ON stu.StudID = enrol.StudID
INNER JOIN Module mod
ON enrol.ModCode = mod.ModCode
WHERE mod.ModName = 'Java Fundamentals'
Structure:
CREATE TABLE Student
(StudID INTEGER PRIMARY KEY,
StudFName VARCHAR(10) NOT NULL,
StudLName VARCHAR(10) NOT NULL,
DoB DATE NOT NULL,
Sex CHAR(1) NOT NULL CHECK (Sex IN ('M', 'F')),
Email VARCHAR(30) UNIQUE);
CREATE TABLE Staff
(StaffID INTEGER PRIMARY KEY,
Title VARCHAR(4) CHECK (Title IN ('Prof', 'Dr', 'Mr', 'Mrs', 'Miss')),
StaffFName VARCHAR(10) NOT NULL,
StaffLName VARCHAR(10) NOT NULL,
Email VARCHAR(30) UNIQUE,
Department VARCHAR(25) DEFAULT 'Not Assigned',
Extension INTEGER CHECK (Extension BETWEEN 0001 AND 9999));
CREATE TABLE Module
(ModCode CHAR(4) PRIMARY KEY,
ModName VARCHAR(25) NOT NULL,
ModCredits INTEGER NOT NULL CHECK (ModCredits IN (15, 30, 45, 60)),
ModLevel CHAR(3) NOT NULL CHECK (ModLevel IN ('UG1', 'UG2', 'UG3', 'MSc')),
ModLeader INTEGER NOT NULL,
Foreign Key (ModLeader) REFERENCES Staff (StaffID));
CREATE TABLE Enrolment
(ModCode CHAR(4) NOT NULL,
StudID INTEGER NOT NULL,
StartDate DATE NOT NULL,
PRIMARY KEY (ModCode, StudID),
Foreign Key (StudID) REFERENCES Student (StudID),
Foreign Key (ModCode) REFERENCES Module (ModCode));
The answer is... there is no trouble with your query.
It works just fine.
Check here
INSERT INTO Student (StudID, StudFName, StudLName, DoB, Sex, Email) VALUES
(1, 'Jack', 'Black', TO_DATE('2015/01/01', 'yyyy/mm/dd'), 'M', 'jack#email.com');
INSERT INTO Student (StudID, StudFName, StudLName, DoB, Sex, Email) VALUES
(2, 'Andrew', 'Wiggin', TO_DATE('2015/01/01', 'yyyy/mm/dd'), 'M', 'andrew#email.com');
INSERT INTO Student (StudID, StudFName, StudLName, DoB, Sex, Email) VALUES
(3, 'Bob', 'Marley', TO_DATE('2015/01/01', 'yyyy/mm/dd'), 'M', 'bob#email.com');
INSERT INTO Staff (StaffID, Title, StaffFName, StaffLName, Email, Extension) VALUES
(1, 'Prof', 'Joe', 'Smith', 'stuff#emal.com', 0001);
INSERT INTO Module (ModCode, ModName, ModCredits, ModLevel, ModLeader) VALUES
(1, 'Java Fundamentals', 30, 'UG1', 1);
INSERT INTO Module (ModCode, ModName, ModCredits, ModLevel, ModLeader) VALUES
(2, 'C# Fundamentals', 15, 'UG2', 1);
INSERT INTO Enrolment (ModCode, StudID, StartDate) VALUES
(1, 1, TO_DATE('2015/01/01', 'yyyy/mm/dd'));
INSERT INTO Enrolment (ModCode, StudID, StartDate) VALUES
(1, 2, TO_DATE('2015/01/02', 'yyyy/mm/dd'));
INSERT INTO Enrolment (ModCode, StudID, StartDate) VALUES
(2, 3, TO_DATE('2015/01/03', 'yyyy/mm/dd'));
-------------------------------------------------------------
SELECT stu.StudFName, stu.StudLName, enrol.StartDate
From Student stu
INNER JOIN Enrolment enrol ON stu.StudID = enrol.StudID
INNER JOIN Module mod ON enrol.ModCode = mod.ModCode
WHERE mod.ModName = 'Java Fundamentals'
Result
STUDFNAME STUDLNAME STARTDATE
----------------------------------------
Jack Black January, 01 2015
Andrew Wiggin January, 02 2015
Hey everyone I have been having a problem with my SQL database, specifically the foreign key. Here is the table I am trying to enter into.
CREATE TABLE Employee (
EmployeeID int NOT NULL Primary Key,
LastName varchar(30) NOT NULL,
FirstName varchar(30) NOT NULL,
Address varchar(30) NOT NULL,
City varchar(20) NOT NULL,
State char(2) NOT NULL,
TelephoneAreacode char(5) NOT NULL,
TelephoneNumber char(8) NOT NULL,
JobTitle varchar(30) NOT NULL Foreign Key References Job_Title(JobTitle),
EEO1Classification varchar(30) NOT NULL,
HireDate char (10) NOT NULL,
Salary money NOT NULL,
Gender varchar(7) NOT NULL,
Age int NOT NULL
)
And the data
INSERT INTO Employee
VALUES ('95687', 'Edelman', 'Glenn', '175 Bishops Lane', 'LA Jolla', 'CA', '619', '5550199','Cashier', 'Sales Workers', '10/7/2003', '$21,500', 'Male', '64'),
('95688', 'McMullen', 'Eric', '763 Church ST', 'Lemm Grove', 'CA', '619', '5550135','Bagger', 'Sales Workers', '11/1/2002', '$12,500', 'Male', '20'),
('95995', 'Slentz', 'Raj', '123 Torrey DR', 'North Clairmont', 'CA', '619', '5550123','Assistant Manager', 'Officials & Managers', '6/1/2000', '$48,000', 'Male', '34'),
('55978', 'Broun', 'Erin', '2045 Parkway - Apt2B', 'Encinitas', 'CA', '760', '5550100', 'Bagger','Sales Workers', '3/12/2003', '$10,530', 'Female', '24'),
('55928', 'Carpenter', 'Donald', '927 Second St', 'Encinitas', 'CA', '760','5550154', 'Stocker','Office/Clerical', '11/1/2003', '$15,000', 'Male', '18'),
('59852', 'Esquivez', 'David', '10983 N. Coast Hwy Apt 902', 'Encinitas', 'CA', '760', '5550108','Butchers & Seafood Specialists', 'Operatives (Semi skilled)', '8/1/2003', '$19,000', 'Male', '22'),
('52362', 'Sharp', 'Nancy', '10793 Montecino RD', 'Ramona', 'CA', '858', '5550135', 'Cashier','Sales Workers', '7/12/2003', '$21,000', 'Female', '24');
The table with the foreign key is this one,
CREATE TABLE Job_Title (
JobTitle varchar(30) NOT NULL Primary key,
EEO1Classification varchar(30) NOT NULL,
JobDescription varchar(100) NOT NULL,
ExemptNonExempt varchar(30) NOT NULL,
)
And the data already entered there is
INSERT INTO Job_Title
VALUES
('Accounting Clerk','Office/Clerical', 'Records Data', 'Non-Exempt'),
('Assistant Manager','Officials & Managers', 'Supervises and coordinates activities', 'Exempt'),
('Bagger','Sales Workers', 'Places customer orders in bags', 'Non-Exempt'),
('Cashier','Sales Workers', 'Operates cash register to itemize and total customer’s purchases', 'Non-Exempt'),
('Computer Support Specialist','Technician', 'Installs, modifies, and makes minor repairs to personal computers', 'Non-Exempt'),
('Dir. of Fin. & Acct.','Officials & Managers', 'Plans and directs the finance and accounting activities', 'Exempt'),
('Asst. - Bakery & Pastry','Craft Workers (Skilled)', 'Bakes Cakes and Pastries', 'Non-Exempt'),
('Butchers & Seafood Specialists','Operatives (Semi skilled)', 'Cuts Meat and seafood', 'Non-Exempt'),
('Stocker','Office/Clerical', 'Stocks Shelves', 'Non-Exempt');
Please help.
Make sure that the values you are inserting in the table that contains the foreign key ( child table ) has a corresponding value in the table that contains the primary key (parent Table )
i,e you can not insert something into the child table that has no corresponding value in the parent table due to the pk -Fk relation ship
Sometimes the error occurs due to the reason that the Child table does not have the corresponding values already present in the Parent table's column which is being referenced as the foreign key. To do:
Truncating both the tables (make sure to backup the data if its
bulky) could simply fix the problem.
Try entering values in the Child table that are existing already in
the Parent table's column. Hope this would help.