I have an odd problem with a table, I'm inserting rows into tables, and now I am trying to test it, on one of my tests it allows an entry that it shouldn't.
My table definition:
CREATE TABLE ITEMS(
ITEM_NUMBER VARCHAR2(3)
CONSTRAINT ITEMS_ITEM_NUMBERf_PK PRIMARY KEY,
ITEM_DESCRIPTION VARCHAR2(30),
ITEM_SIZE VARCHAR2(2),
ITEM_COST NUMBER(30,0),
ITEM_QTY NUMBER(2,0),
ORDER_NUMBER VARCHAR2(4));
When I try and test the ITEM_SIZE column by putting in a number it just inserts it.
INSERT INTO ITEMS VALUES ('I32', 'Leather blazer', 'L', 14.00, 1, 'O114');
INSERT INTO ITEMS VALUES ('I33', 'Fleece vest', 'L', 28.00, 1, 'O128');
INSERT INTO ITEMS VALUES ('I34', 'Khaki pants', 'S', 22.00, 3, 'O122');
INSERT INTO ITEMS VALUES ('I35', 'Muscle Tee', 'M', 12.99, 1, 'O122');
INSERT INTO ITEMS VALUES ('I36', 'Trench Coat', 'M', 03.00, 1, 'O133');
INSERT INTO ITEMS VALUES ('I37', 'Crewneck sweater', 'XL', 07.00, 1, 'O107');
INSERT INTO ITEMS VALUES ('I38', 'Varsity Sweater', 'M', 08.99, 1, 'O108');
INSERT INTO ITEMS VALUES ('I39', 'CROOK', 'M', 12.00, 1, 'O112');
//--the tester--//
INSERT INTO ITEMS VALUES ('I49', 'bROOK', 10, 12.00, 1, 'O112');
In the statement above I insert the number 10 into ITEM_SIZE VARCHAR2(2). How can I avoid that?
As #kevinsky said, Oracle is doing implicit data conversion from number to string while you insert.
If you want to restrict the values that can be put into a column then you can add a check constraint:
alter table items add constraint items_size_chk
check (item_size in ('XXS','XS','S','M','L','XL','XXL','XXXL'));
Table ITEMS altered.
INSERT INTO ITEMS VALUES ('I49', 'bROOK', 10, 12.00, 1, 'O112');
SQL Error: ORA-02290: check constraint (SCHEMA.ITEMS_SIZE_CHK) violated
02290. 00000 - "check constraint (%s.%s) violated"
*Cause: The values being inserted do not satisfy the named check
If you want different restrictions you can change the way the check is done; for example you could have a regular expression check to exclude digits but allow any characters. In this case it seems more likely you want a list of specific values to be allowed though.
Note though that when a new valid size is added, you will have to modify the check constraint; and any of those size values will be allowed for any item.
If the valid sizes are coming from another table then you can use a foreign key constraint instead. You probably really want a list of sizes (or other attributes) that are valid for each item, so your data model might need some fleshing out.
Oracle is implicitly converting the number 10 into the varchar2 string '10'.
and here is another implicit conversion
SELECT item_size + 1 FROM items;
Result in Oracle 11g is
11
I'm not sure whether this was intended to be helpful when the parser was being built but it does cause a lot of confusion. However Oracle will not convert an alphabetical character into a number implicitly. It will fail.
Related
I have started my journey in learning SQL and right I am having trouble creating and inserting data into tables. Here is the code that I have tried, I get an error message saying that there aren't enough values. I am using Oracle.
Create table project
(
proj_id number(10),
medic_name varchar2(10),
purpose varchar2(12),
start_date date,
end_date date,
pi_id null,
CONSTRAINT pkprojid primary key (proj_id),
CONSTRAINT fkproject foreign key (pi_id) references researcher
);
alter session set nls_date_format = 'mm/dd/yyyy';
Insert into project values (PR001, 'Medic1', 'heart', '09/01/2017', '07/31/2019');
Insert into project values (PR002, 'Medic1', 'diabetes', '10/01/2016', '07/31/2020);
Insert into project values (PR003, 'Medic3', 'lung', '11/1/2014', '12/31/2020');
Insert into project values (PR004, 'Medic3', 'blood', '01/10/2017', '07/31/2019');
Insert into project values (PR005, 'Medic5', 'blood', '07/10/2018', '01/31/2020');
alter session set nls_date_format = 'mm/dd/yyyy';
Insert into project values (PR001, 'Medic1', 'heart', '09/01/2017', '07/31/2019');
Issues:
Your table has 6 columns, you are only passing 5 for insert; it seems like you are missing last column (pi_id), hence the error message that you are getting. If you want to skip the last column (which is possible since it is declared as nullable), you can explictly list the column when inserting
first column (proj_id) is of number datatype; PR001 is not a number (neither a string, since it is not quoted: this is a syntax error); did you mean 1 instead? Or, if you want to insert string values, you need to change the datatype of column proj_id to varchar(N) (N being the maximum length of the string, in bytes).
Here is an insert statement that should work for your current table definition:
insert into project(proj_id, medic_name, purpose, start_date, end_date)
values (1, 'Medic1', 'heart', '09/01/2017', '07/31/2019');
Note: there is a missing quote at the end of the date on the second insert statement; I assume that this is a typo.
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')
Currently working on a school project.
I'm trying to create the following table:
CREATE TABLE Purchase (
ID INT NOT NULL,
Type INT DEFAULT 3 NOT NULL,
Price DECIMAL(5,5) NOT NULL,
CONSTRAINT check_3 CHECK (TYPE = 3),
CONSTRAINT price_check CHECK (Cost>0),
CONSTRAINT pk_1 PRIMARY KEY (ID),
CONSTRAINT fk_1 FOREIGN KEY (ID,Type) REFERENCES Part(ID,Type));
My problem is when I'm trying to insert values into this column.
When I try to do this:
INSERT INTO Purchase VALUES (12, 3, 200);
I get the following error:
SQL> INSERT INTO Purchase VALUES (12, 3, 200);
INSERT INTO Purchase VALUES (12, 3, 200)
*
ERROR at line 1:
ORA-01438: value larger than specified precision allowed for this column
I don't get what I'm doing wrong. Can't I add integers to a decimal column? Is that the problem? Doesn't make much sense to me.
Thank you for taking the time to read this!
First, always list the columns for an insert:
INSERT INTO Purchase (id, type, price)
VALUES (12, 3, 200);
Second, your price is declared as DECIMAL(5, 5). That means that the prices range from 0.00000 to 0.99999.
Presumably, you want a broader range. I don't know what that is, but DECIMAL(10, 5) would solve your problem. More generally, just NUMBER solves your problem in Oracle.
DECIMAL(5,5) means you will recieve space for 5 number after the comma (5 of 5). Please try to create your table like that: DECIMAL(10,5)
Good day,
I have a table, which some columns, and BELTID set to primary key, and IS_AUTOINCREMENT set to YES.
I wish to insert a row of data inside this data without key in the BELTID, I am expect the BELDID will auto generate.
The query is as follow:
INSERT INTO mySchema.TABLE1(TYPE, ORIGINALBATCHID, MANUAL)
VALUES ('TEST', 124, 1);
I get this error:
SQLSTATE: 23505. A violation of the constraint imposed by a unique index or a unique constraint occurred.
Then I change my query to:
INSERT INTO mySchema.TABLE1(BELTID, TYPE, ORIGINALBATCHID, MANUAL)
VALUES (123, 'TEST', 124, 1);
And I am getting another error:
SQLSTATE: 428C9 A ROWID column cannot be specified as the target column of an INSERT or UPDATE.
Kindly advise on what mistake I make.
Query:
INSERT INTO `job_listing_has_employer_details` (`job_listing_id`, `employer_details_id`)
VALUES (6, '5')
Error:
Cannot add or update a child row: a foreign key constraint fails (mydb.job_listing_has_employer_details, CONSTRAINT job_listing_has_employer_details_ibfk_2 FOREIGN KEY (employer_details_id) REFERENCES employer_details (id))
What does this mean? The two ID's I am inserting into the table exist.
It means it can't find '5' in the id column of the employer_details table. If there is a 5 in that column of that table, then maybe the data is numeric, so must be passed without quotes. If so, try:
INSERT INTO `job_listing_has_employer_details` (`job_listing_id`, `employer_details_id`) VALUES (6, 5)