I want a table with the name of an employee and the year of his birth. ONLY THE YEAR IN DATE FORMAT:
CREATE TABLE EMPLOYEE(
Name VARCHAR2(50) NOT NULL,
Year_Birth DATE NOT NULL,
PRIMARY KEY (Name)
);
I want to do this:
INSERT INTO EMPLOYEE(Name, Year_Birth)
VALUES ('John Smith', 1985);
But it doesn't work (cause i'm passing a number value to a date column). I also tried this:
INSERT INTO EMPLOYEE(Name, Year_Birth)
VALUES ('John Smith', to_date('1972','YYYY') )
If i try this one i will get this:
ORA-02290: check constraint (PROJECTNAME.SYS_C0066777818) violated
Year_Birth must be a date column. Is there any way to achieve this?
I think birth year should be an INT, like this:
CREATE TABLE EMPLOYEE(
Name VARCHAR(50) NOT NULL,
Year_Birth INT NOT NULL,
PRIMARY KEY (Name)
);
Then this should work:
INSERT INTO EMPLOYEE(Name, Year_Birth)
VALUES ('John Smith', 1985);
You can create a column which is of data type int. This will then let you save the year of birth.
If the column must be of data type date then you could just save the date as the first of Jan with the relevant year (eg. for the year 2017 enter '20170101'). This will still allow you then perform date calculations on the data.
You can create a column which is of data type int. The insert statement will work.
insert into employee(Name, Year_Birth)
values ('John Smith', 1985);
I am assuming that your mysterious check constraint SYS_C0066777818 is enforcing a rule that year_birth must be 1st January.
to_date('1972','YYYY') does not give the 1st January 1972 as you might expect, it gives the first of the current month, in 1972. Who knows why, but that's the way it works. If you want 1st January 1972 then you will have to specify it explicitly, for example:
insert into employee (name, year_birth)
values ('John Smith', date '1972-01-01');
or an equivalent to_date expression that includes at least the month, or trunc(somedatevariable,'YEAR').
Related
I am using Apex Oracle to run a script file merging data/tables with an existing schema. Here is the complete script file.
The error is produced on Every Insert Command stating not a valid month.
One error is produced on Alter command stating Column Type incompatible with referenced column type
The Script File:
--A1_rr_upd.txt file
--dropping the table if already exists
DROP TABLE RRSTAFF;
--creating new table for RRSTAFF
CREATE TABLE RRSTAFF (
staff_num CHAR(4) PRIMARY KEY,
name VARCHAR(20),
gender CHAR(1),
date_join DATE,
date_resign DATE,
contact_num NUMBER(11),
address VARCHAR(255)
);
--Adding new hired staff
INSERT INTO RRSTAFF VALUES ('s001','Adrian','M','2021/07/02',null,'60122000000','6 Jalan BU6, Petaling Jaya,Selangor');
INSERT INTO RRSTAFF VALUES ('s002','Jewel','F','2021/07/12',null,'60123000000','2 Jalan PJS2, Sunway,Selangor');
INSERT INTO RRSTAFF VALUES ('s003','Sean','M','2021/07/12',null,'60166000000','100 Sunway South K,Selangor');
--Adding new customer details
INSERT INTO rrcustomer VALUES ('1011','Dr','Brendan');
INSERT INTO rrcustomer VALUES ('1012','Dr','Haya');
--Adding new records into models
INSERT INTO model VALUES ('LGC83','LG C1 83 in OLED 4K TV', '500');
INSERT INTO model VALUES ('LGG77','LG Gallery 77 in OLED 4K TV', '400');
INSERT INTO model VALUES ('SNY43','Sony 43 in X75 4K Ultra HD Android TV', '200');
INSERT INTO model VALUES ('SHA50','Sharp 50 in Full HD Basic TV ', '80');
--Adding new records into appliance
INSERT INTO appliance VALUES ('2010','LGC83','E',null);
INSERT INTO appliance VALUES ('2011','LGC83','E',null);
--Altering the HIRE table to link it with RRSTAFF using staff_id as Foriegn Key
ALTER TABLE
hire ADD(
staff_id VARCHAR(4),
FOREIGN KEY (staff_id) REFERENCES rrstaff(staff_num)
);
--Adding new hire records
INSERT INTO hire VALUES ('2010','2021/08/02','1011','2021/08/08','s001');
INSERT INTO hire VALUES ('2010','2021/08/22','1012','2021/08/28','s001');
INSERT INTO hire VALUES ('2011','2021/08/12','1013',null,'s001');
Use TO_DATE('2021/07/02', 'YYYY/MM/DD') to convert your date values to the standard database date format
Replace the above date value as your date columns
In Oracle, you express date constants using the date keyword and a string in the ISO standard YYYY-MM-DD format. For instance:
INSERT INTO RRSTAFF
VALUES ('s001', 'Adrian', 'M', DATE '2021-07-02', null, '60122000000', '6 Jalan BU6, Petaling Jaya,Selangor');
Here is a db<>fiddle.
using to_date makes the script robust and independent from an implicit format. It also makes the code more readable since to_date is quite obvious in its syntax.
I'm having a hard time creating a simple table:
CREATE TABLE `csat` (
`csat_id` INT NOT NULL AUTO_INCREMENT,
`value` INT,
`month` DATE NOT NULL,
PRIMARY KEY (`csat_id`)
);
CREATE TABLE `migrated` (
`migrated_id` INT NOT NULL AUTO_INCREMENT,
`title` INT,
`description` INT,
`month` DATE NOT NULL,
PRIMARY KEY (`migrated_id`)
);
INSERT INTO csat
VALUES (1, 1, 2017-06-15);
INSERT INTO migrated
VALUES (1, 2, 2018-06-15);
I get the error:
Data truncation: Incorrect date value: '1996' for column 'month' at row 1
It seems like my date is in the right format:
https://www.w3schools.com/sql/func_mysql_date.asp
I'm also wondering why I need to specify a value on the csat_id, because I thought SQL would just put that in for me since its the primary key.
You have to wrap your date values in single quotation marks: '2017-06-15', not 2017-06-15. Right now, MySQL is evaluating this as 2017 minus 6 minus 15, which comes to 1996.
Also, when inserting, it's best to specify the columns you're inserting into. And if your column is set to AUTO_INCREMENT, you don't need to specify it:
INSERT INTO csat
(`value`, `month`)
VALUES
(1, '2017-06-15');
I would also consider changing your column names. Perhaps make "value" more descriptive (value of what?) And month is misleading, since it's actually a date-type column.
You haven't said which database server you're using, but generally speaking dates are inputted as strings.
You should try the following inserts;
INSERT INTO csat (`csat_id`, `value`, `month`)
VALUES (1, 1, '2017-06-15');
INSERT INTO migrated (`migrated_id`, `title`, `description`, `month`)
VALUES (1, 2, 2, '2018-06-15');
Also, you should specify which columns you're inserting into. This prevents data from being entered into the wrong fields, especially when schema changes occur.
SQL does auto increment primary key fields (if defined that way). However, you had to define it in your insert statements because you didn't specify the columns you were inserting to.
Try this instead;
INSERT INTO csat (`value`, `month`)
VALUES (1, '2017-06-15');
INSERT INTO migrated (`title`, `description`, `month`)
VALUES (2, 2, '2018-06-15');
I guess you missed the single qoutes (as per Sql standards) at first in your date and then while inserting even if the column is autoincrement you need to specify columns other than the autoincrement column so as to make sure the data you are inserting belongs to that specific column or not
Try this
INSERT INTO
csat(value,month) values
(1,'2017-06-15')
I have been working on my SQL database for school project and I have a problem with my idea. I am doing a car service database (all made up) and I have a column deadline of datatype date for a table order (names for both in my language - 'rok' and 'narocilo') and I am trying to insert null into some of rows where deadline isn't specified by the customer.
create table narocilo
(
id_narocila integer NOT null,
opis varchar(50),
cena integer,
status varchar(20),
datum_narocila date,
rok date
);
This is some ways i tried to insert null but none of them worked
to_date('dd-mm-yyyy', null)
to_date(null)
null
to_date('null')
So, my question is, can I insert null into a date column and if yes, how?
Thank you in advance!
You have to insert an entire row's worth of data, but as long as you have not defined that field as NOT NULL like you have above, it should be fine to insert NULL values into the date fields:
INSERT INTO narocilo(id_narocila, opis, cena, status, datm_narocila, rok)
VALUES (1001, 'something', 6, 'Open', NULL, NULL)
A very simple way to insert NULL is to leave them out of the insert altogether:
INSERT INTO narocilo (id_narocila, opis, cena, status)
VALUES (?, ?, ?, ?);
Technically, this inserts the default value. But you haven't specified one, so the default is NULL.
I have the following DDL and DML statements :
create table emp_details (
ID number(2) constraint t_pk primary key,
F_Name varchar(10) not null,
L_Name varchar(10) not null,
DOB date,
Mob_no number(10),
City varchar(10),
PIN number(5),
Gender char(1),
Designation varchar(15),
Join_Date date,
);
insert into emp_details values (01,'John','Wick','1990-07-05',9856482358,'Goa',403001,'M','SDE II', '2015-01-08');
then, I get the error of ORA-01843. So, what could be the problem?
The easiest thing to do here is to use ANSI date literals instead of strings for the dates (using strings will depend on the value of NLS_DATE_FORMAT and you don't want to play around with that if you don't have to):
INSERT INTO emp_details
VALUES
( 01, 'John', 'Wick', DATE'1990-07-05', 9856482358
, 'Goa', 403001, 'M', 'SDE II', DATE'2015-01-08');
I have to add that explicitly listing the columns into which you're inserting values is a good habit to have. Otherwise, your INSERT query will break if you or someone else adds a column from your table:
INSERT INTO emp_details
( id, f_name, l_name, dob, mob_no, city, pin, gender, designation, join_date )
VALUES
( 01, 'John', 'Wick', DATE'1990-07-05', 9856482358
, 'Goa', 403001, 'M', 'SDE II', DATE'2015-01-08');
Last, another good practice is to use VARCHAR2 instead of VARCHAR when you're working with Oracle. Currently they work the same, but Oracle "reserves the right" to change VARCHAR to meet with the ANSI standard under which NULL values and the empty string won't be the same (with VARCHAR2 they will always be the same). IOW, the behavior of VARCHAR values in Oracle can change.
It seems when you query with
select * from nls_session_parameters p where p.parameter = 'NLS_DATE_FORMAT';
you won't get YYYY-MM-DD or YYYY-DD-MM from your result of error ORA-01843.
This problem is due to inserting wrong-formatted value for date columns DOB and Join_date.
There may be two ways to prevent this error :
Assume you get DD/MM/YYYY from above query, then use 05-07-1990 for
DOB, and 08/01/2015 for Join_Date columns, respectively.
Format your values as to_date('1990-07-05','YYYY-MM-DD') for
DOB and to_date('2015-01-08','YYYY-MM-DD') for Join_Date
I would like for example that user can add name JHONE and age 25, so next time he can add JHONE, 26 or ALEX 25, BUT not JHONE, 25 again.
So I'm looking for two column unique NOT separately.
P.S. I'm sorry if same question was mentioned before.
EDIT:
This is my example:
Would like to make userIdG and doWithCar will be like this
102163096246025413003 View
102163096246025413003 Buy
102163096246025413003 Let
102163096246025413003 Sell
And for Id = 102163096246025413003 you can't add any more values, BECAUSE column doWithCar will have only 4 possible choice view, buy, rent and sell
You could specify more than one column in UNIQUE:
CREATE TABLE tab(ID INT IDENTITY(1,1) PRIMARY KEY, name VARCHAR(100), age INT
,UNIQUE(name, age));
INSERT INTO tab(name, age) VALUES ('John', 25);
INSERT INTO tab(name, age) VALUES ('John', 26);
-- INSERT INTO tab(name,age) VALUES ('John', 25);
-- Violation of UNIQUE KEY constraint 'UQ__tab__CF0426FD76D3370A'.
-- Cannot insert duplicate key in object 'dbo.tab'.
-- The duplicate key value is (John, 25).
-- The statement has been terminated.
SELECT * FROM tab;
LiveDemo
Note:
You should store date of birth and not age itself (or make age calculated column and set UNIQUE(name, dob)).
this is what I do not understand) how database will know that it should be two columns as unique and not each column is unique
These are different concepts. DB "knows" it from UNIQUE constraint definition:
UNIQUE(userIdG,doWithCar) -- pair of column is unique
!=
UNIQUE(userIdG),UNIQUE(doWithCar) -- each column is unique