Keeping on Getting error code.
Here's my database:
CREATE TABLE IF NOT EXISTS LOCATION_tbl ( locationid VARCHAR(20) NOT NULL,
locationcity VARCHAR(100) NOT NULL,
locationregion VARCHAR(100) NOT NULL,
locationcountry VARCHAR(40) NOT NULL,
PRIMARY KEY (locationid) USE lab3b;
INSERT INTO LOCATION_tbl (locationid, locationcity, locationregion, locationcountry)
VALUES ('GRU',
'Sao Paulo',
'Sao Paulo',
'Brazil');
INSERT INTO LOCATION_tbl
VALUES ('CPT',
'Cape Town',
'Western Cape',
'South Africa');
INSERT INTO LOCATION_tbl
VALUES ('BOG',
'Bogota',
'Capital District' ,
'Colombia');
INSERT INTO LOCATION_tbl
VALUES ('HAN',
'Hanoi',
'Som Tay',
'Vietnam');
INSERT INTO LOCATION_tbl
VALUES ('MUC',
'Munich',
'Bavaria',
'Germany')
Help anyone??
It looks like you are missing a closing ')' from your create table statement
Related
Suppose the following setup:
create table chat_message (
id serial primary key,
message_id bigint unique not null, -- id from third party service
message_type varchar(255) unique not null
);
I can upsert into this table like this:
-- insert first record
insert into chat_message
values(1, 1234, 'start_message');
-- upsert
insert into chat_message (message_id, message_type)
values(1260, 'start_message')
on conflict (message_type)
do update set message_id = 1260;
This works fine, but I need to structure the table like this:
create table chat_message (
id serial primary key,
message_id bigint unique not null,
message_type varchar(255) not null -- this isn't necessarily unique
);
How can I accomplish this? Sure, message_id will always be unique, but there could be many messages with message_type = 'start_message'
I am using the COPY FROM command to load data from a file.
The table is defined with identity column, which is not part of the file.
CREATE TABLE APP2DBMAP (
FIELD_ID integer NOT NULL GENERATED BY DEFAULT AS IDENTITY,
FIELD_NAME varchar(128) ,
TABLE_NAME varchar(128) ,
COLUMN_NAME varchar(128) ,
CONSTRAINT PK_APP2DBMAP PRIMARY KEY ( FIELD_ID )
);
I executed the following COPY FROM command, the file contains 3 values in 1 row.
copy app2dbmap (field_name, table_name, column_name) from '/opt/NetMgr/data/templ_db.txt' DELIMITER ',' ;
And I got the following error:
ERROR: null value in column "field_id" violates not-null constraint
DETAIL: Failing row contains (null, 'aaa', 'bbb', 'ccc').
CONTEXT: COPY app2dbmap, line 1: "'aaa','bbb','ccc'"
I tried to change the column description of field_id to serial, and it did work fine.
I don't understand why it doesn't work with the original table definition.
The problem is you have specified the field_id to be a not null value and hence when the file is passing null as a value, your error is there.
If you want an auto increment id, Use,
CREATE TABLE APP2DBMAP (
FIELD_ID smallserial NOT NULL,
FIELD_NAME varchar(128) ,
TABLE_NAME varchar(128) ,
COLUMN_NAME varchar(128) ,
CONSTRAINT PK_APP2DBMAP PRIMARY KEY ( FIELD_ID )
);
You can also use bigserial(int4) instead of smallint(int8)
or you can give a default value,
CREATE TABLE APP2DBMAP (
FIELD_ID integer NOT NULL default 0,
FIELD_NAME varchar(128) ,
TABLE_NAME varchar(128) ,
COLUMN_NAME varchar(128) ,
CONSTRAINT PK_APP2DBMAP PRIMARY KEY ( FIELD_ID )
);
You will have to pass something, you cannot pass null in a not null column
I need Help with implementing a database in SQL Developer. I tried to implement date constraints but it doesn't work the way I want to.
Here is Office Table:
CREATE TABLE Office (
Office_ID varchar2(7) PRIMARY KEY NOT NULL,
Office_AddressLine varchar2(50) NOT NULL,
Office_PostCode varchar2(8) NOT NULL,
Office_Telephone varchar2(11),
Office_Email varchar2(30), PRIMARY KEY(Office_ID),
CONSTRAINT Office_Email_CK CHECK ( Office_Email like '%_#__%._%')
);
Here is Employee Table:
CREATE TABLE Employee (
Employee_ID varchar2(10) PRIMARY KEY NOT NULL,
Office_ID varchar2(7) NOT NULL,
Emp_FirstName varchar2(20) NOT NULL,
Emp_LastName varchar2(20) NOT NULL,
Emp_Gender varchar2(1) NOT NULL,
Emp_DateOfBirth Date NOT NULL,
Hire_Date date NOT NULL,
Emp_Telephone varchar2(11),
Emp_Email varchar2(30),
Emp_AddressLine varchar2(50) NOT NULL,
Emp_PostCode varchar2(8) NOT NULL,
Emp_Speciality varchar2(20) NOT NULL,
Emp_Qualification varchar2(20) NOT NULL,
Emp_AwardingBody varchar2(30) NOT NULL,
Emp_Salary number(6) NOT NULL,
Employment_History1 varchar2(50),
Employment_History2 varchar2(50),
Employment_History3 varchar2(50) ,
CONSTRAINT fk_staff_office FOREIGN KEY (Office_ID) REFERENCES office (Office_ID),
CONSTRAINT Emp_DateOfBirth_CK CHECK (Emp_DateOfBirth BETWEEN Date '1900-01-01' AND Date '2018-01-01'),
CONSTRAINT Emp_Gender_CK CHECK (Emp_Gender in ('M','F')),
CONSTRAINT Emp_Email_CK CHECK ( Emp_Email like '%_#__%._%')
);
It works this way, but what I want is this:
CREATE TABLE Employee (
Employee_ID varchar2(10) NOT NULL,
Office_ID varchar2(7) NOT NULL,
Emp_FirstName varchar2(20) NOT NULL,
Emp_LastName varchar2(20) NOT NULL,
Emp_Gender varchar2(1) NOT NULL, Emp_DateOfBirth Date NOT NULL,
Hire_Date date NOT NULL,
Emp_Telephone varchar2(11),
Emp_Email varchar2(30),
Emp_AddressLine varchar2(50) NOT NULL,
Emp_PostCode varchar2(8) NOT NULL,
Emp_Speciality varchar2(20) NOT NULL,
Emp_Qualification varchar2(20) NOT NULL,
Emp_AwardingBody varchar2(30) NOT NULL,
Emp_Salary number(6) NOT NULL,
Employment_History1 varchar2(50),
Employment_History2 varchar2(50),
Employment_History3 varchar2(50) ,
PRIMARY KEY (Employee_ID),
CONSTRAINT fk_staff_office FOREIGN KEY (Office_ID) REFERENCES office (Office_ID)
CONSTRAINT fk_staff_patient FOREIGN KEY (Patient_ID) REFERENCES Patient (Patient_ID),
CONSTRAINT Emp_DateOfBirth_CK CHECK (Emp_DateOfBirth BETWEEN Date '1900-01-01' AND Date sysdate),
CONSTRAINT Hire_Date_CK CHECK (Hire_Date <= curdate()),
CONSTRAINT Emp_Gender_CK CHECK (Emp_Gender in ('M','F')),
CONSTRAINT Emp_Email_CK CHECK ( Emp_Email like '%_#__%._%')
);
I don't know why sysdate function or getdate doesn't work and I also don't know how to implement the Constraint about the Hire_Date (I cannot do it with <= relationship). As it is now I have to give up in implementing them even if I will lose points.
Any help will be much appreciated.
You'll need a trigger to enforce such a constraint. Here's an example:
SQL> create table test
2 (
3 employee_id varchar2 (10),
4 hire_date date
5 );
Table created.
SQL> create or replace trigger trg_hd_biu
2 before insert or update of hire_date
3 on test
4 for each row
5 begin
6 if :new.hire_date not between date '1900-01-01' and sysdate
7 then
8 raise_application_error (-20001, 'Hire date out of range');
9 end if;
10 end;
11 /
Trigger created.
SQL> insert into test values ('1', date '2018-01-15');
1 row created.
SQL> insert into test values ('2', date '2018-05-25');
insert into test values ('2', date '2018-05-25')
*
ERROR at line 1:
ORA-20001: Hire date out of range
ORA-06512: at "SCOTT.TRG_HD_BIU", line 4
ORA-04088: error during execution of trigger 'SCOTT.TRG_HD_BIU'
SQL>
I have done it already using another approach (because our teacher doesn't want us to create triggers. We have to make use of constraints only) and it works. Thanks for help any way.
CREATE TABLE Employee (
Employee_ID varchar2(10) PRIMARY KEY NOT NULL,
Office_ID varchar2(7) NOT NULL,
Emp_FirstName varchar2(20) NOT NULL,
Emp_LastName varchar2(20) NOT NULL,
Emp_Gender varchar2(1) NOT NULL,
Emp_DateOfBirth Date NOT NULL,
Hire_Date date NOT NULL,
Emp_CurrentDate date default sysdate,
Emp_Telephone varchar2(11) NOT NULL,
Emp_Email varchar2(30) NOT NULL,
Emp_AddressLine varchar2(50) NOT NULL,
Emp_PostCode varchar2(8) NOT NULL,
Emp_Speciality varchar2(20) NOT NULL,
Emp_Qualification varchar2(20) NOT NULL,
Emp_AwardingBody varchar2(30) NOT NULL,
Emp_Salary number(6) NOT NULL,
Emp_Supervised_By varchar2(10) NOT NULL,
Employment_History1 varchar2(50),
Employment_History2 varchar2(50),
Employment_History3 varchar2(50) ,
CONSTRAINT fk_staff_office FOREIGN KEY (Office_ID) REFERENCES office (Office_ID),
CONSTRAINT Hire_Date_CK check (Hire_Date < Emp_CurrentDate AND (Hire_Date - Emp_DateOfBirth)/365 > 18),
CONSTRAINT Emp_DateOfBirth_CK check (Emp_DateOfBirth > TO_DATE('1900-01-01', 'YYYY-MM-DD')),
CONSTRAINT Emp_Salary_CK check (Emp_Salary > 0 AND Emp_Salary < 150000),
CONSTRAINT Emp_Gender_CK CHECK (Emp_Gender in ('M','F')),
CONSTRAINT Emp_Email_CK CHECK ( Emp_Email like '%_#__%._%'),
CONSTRAINT Emp_Telephone_CK CHECK (regexp_like(Emp_Telephone, '^[0123456789]{11}$') AND Emp_Telephone like '0%'),
CONSTRAINT Emp_FirstName_CK CHECK (regexp_like(Emp_FirstName, '^[ABCDEFGHIJKLMNOPQRSTUVWXYZ]{1,20}$')),
CONSTRAINT Emp_LastName_CK CHECK (regexp_like(Emp_LastName, '^[ABCDEFGHIJKLMNOPQRSTUVWXYZ]{1,20}$')),
CONSTRAINT Emp_PostCode_CK CHECK (regexp_like ( Emp_PostCode , '([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z]))))\s?[0-9][A-Za-z]{2})')),
CONSTRAINT Emp_Speciality_CK CHECK (regexp_like(Emp_Speciality, '^[ABCDEFGHIJKLMNOPQRSTUVWXYZ ]{1,20}$')),
CONSTRAINT Emp_Qualification_CK CHECK (regexp_like(Emp_Qualification, '^[ABCDEFGHIJKLMNOPQRSTUVWXYZ ]{1,20}$')),
CONSTRAINT Emp_AwardingBody_CK CHECK (regexp_like(Emp_AwardingBody, '^[ABCDEFGHIJKLMNOPQRSTUVWXYZ ]{1,30}$'))
);
What I want to do now is to create a constraint that enables me to set the value of the attribute Emp_Supervised_By automatically as the same value of the Employee_ID attribute when the Emp_Speciality is 'Manager'. Thanks in advance for help!
In postgresql I have a table which I need to add a new column. the original table ddl is belowing:
CREATE TABLE survey.survey_response (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
survey_id uuid NOT NULL,
survey_question_id uuid NULL,
user_id varchar(256) NULL,
device_id varchar(256) NULL,
user_country varchar(100) NULL,
client_type varchar(100) NULL,
product_version varchar(100) NULL,
answer text NULL,
response_date timestamptz NOT NULL DEFAULT now(),
survey_category varchar(100) NULL,
tags varchar(250) NULL,
tracking_id uuid NULL,
CONSTRAINT survey_response_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
) ;
Then I alter the table to add a new column:
alter table survey.survey_response add column system_tags varchar(30) ;
But after that I found my instert statement cannot make change to this new column, for all the original columns it works fine:
INSERT INTO survey.survey_response
(id, survey_id, user_id, tags, system_tags)
VALUES(uuid_generate_v4(), uuid_generate_v4(),'1123','dsfsd', 'dsfsd');
select * from survey.survey_response where user_id = '1123';
The "tags" columns contains inserted value, however, system_tags keeps null.
I tested the above scenario in my local postgreSQL 9.6, any ideas about this strange behavior? Thanks a lot
-----------------update----------
I found this survey.survey_response table has been partitioning based on month, So my inserted record will also be displayed in survey.survey_response_y2017m12. but the new system_tags column is also NULL
CREATE TABLE survey.survey_response_y2017m12 (
id uuid NOT NULL DEFAULT uuid_generate_v4(),
survey_id uuid NOT NULL,
survey_question_id uuid NULL,
user_id varchar(256) NULL,
device_id varchar(256) NULL,
user_country varchar(100) NULL,
client_type varchar(100) NULL,
product_version varchar(100) NULL,
answer text NULL,
response_date timestamptz NOT NULL DEFAULT now(),
survey_category varchar(100) NULL,
tags varchar(250) NULL,
tracking_id uuid NULL,
system_tags varchar(30) NULL,
CONSTRAINT survey_response_y2017m12_response_date_check CHECK (((response_date >= '2017-12-01'::date) AND (response_date < '2018-01-01'::date)))
)
INHERITS (survey.survey_response)
WITH (
OIDS=FALSE
) ;
If I run the same scenario in a non-partition table then the insert works fine.
So do I need any special settings for alter table for partition table?
Old thread but you need to drop and create again the RULE to fix the issue.
Given the following table definition:
CREATE TABLE ControlledSubstances.NationalDrugCode
(
NationalDrugCodeID INT NOT NULL
,NationalDrugCode VARCHAR(20) NOT NULL
,Product VARCHAR(100)
,Ingredient VARCHAR(500)
,ClassID VARCHAR(50)
,Class VARCHAR(50)
,DrugEnforcementAgencyClassID VARCHAR(50)
,DrugEnforcementAgencyClass VARCHAR(50)
,GenericDrug VARCHAR(50)
,Form VARCHAR(50)
,Drug VARCHAR(50)
,StrengthPerUnit NUMERIC(6,2)
,UnitOfMeasure VARCHAR(50)
,ConversionFactor NUMERIC(4,2)
,LongOrShortActing VARCHAR(50)
,IsPreventionForStates BIT NOT NULL
)
;
ALTER TABLE ControlledSubstances.NationalDrugCode
ADD CONSTRAINT PK_ControlledSubstances_NationalDrugCode PRIMARY KEY (NationalDrugCodeID)
,CONSTRAINT DF_ControlledSubstances_NationalDrugCode_IsPreventionForStates DEFAULT 0 FOR IsPreventionForStates
;
CREATE UNIQUE INDEX UQ_ControlledSubstances_NationalDrugCode_NationalDrugCode ON ControlledSubstances.NationalDrugCode (NationalDrugCode);
Why would I be receiving an error on insert for the column I defined as NOT NULL and created a default constraint of 0? I know I can handle the logic in the insert statement to not pass in NULL values, but I use this logic in multiple tables and have never gotten an error before. The error I receive is:
Cannot insert the value NULL into column 'IsPreventionForStates', table 'Staging.ControlledSubstances.NationalDrugCode'; column does not allow nulls. INSERT fails.
This will happen if you explicitly provide NULL as its value. The default constraint only kicks in when you don't supply a value at all, or when you use the DEFAULT keyword:
For example, if NationalDrugCodeID and IsPreventionForStates were your only two columns in the table (for illustration), this will fail:
INSERT INTO NationalDrugCode(NationalDrugCodeID, IsPreventionForStates) VALUES (5, NULL);
But either of these would work:
INSERT INTO NationalDrugCode(NationalDrugCodeID) VALUES (5);
INSERT INTO NationalDrugCode(NationalDrugCodeID, IsPreventionForStates) VALUES (5, DEFAULT);
In the edge case where you need ALL columns to have default values inserted, you can use:
INSERT INTO NationalDrugCode DEFAULT VALUES;