Storing an object into a SQL database in Oracle - sql

Simply trying to find the correct syntax/method to enter create SQL objects and store them inside an oracle database. (school project so it's got to be possible)
CREATE OR REPLACE TYPE Person AS OBJECT
(id NUMBER,
fname VARCHAR(255),
lname VARCHAR(255)) NOT FINAL
CREATE OR REPLACE TYPE customer UNDER Person (
num_purchases NUMBER,
email VARCHAR(255)
);
CREATE TABLE Customers (
id NUMBER,
cust customer
);
INSERT INTO Customers(id, cust)
VALUES (1, customer(1, "John", "Doe", 44, "doezer#gmail.com"));
Returns the error
ORA-00984: column not allowed here
I can't seem to find any way to declare a specific order for putting the values inside of the customer object other than the order that they were declared in. Thank you!

Seems you are inserting columns not literal values. replace your double quotes to single quotes.
INSERT INTO Customers(id, cust)
VALUES (1, customer(1, 'John', 'Doe', 44, 'doezer#gmail.com'));
SELECT ID, TREAT(cust AS Person).id, TREAT(cust AS Person).fname FROM Customers;
See dbfiddle

Related

Use a returning value from an INSERT table (A) into another table (B)

I need to use the returning value of the Table A (ID) and insert it in Table B as a parameter:
insert into tableA (ID, Name , Address)
values (GEN_ID(GENERATOR,1),'John','123 street')
returning ID
--Example: ID=159
insert into tableB (ID, TABLE_A_FK )
values (GEN_ID(GENERATOR,1), 159)
Instead of entering the actual value 159, can I create like a variable (e.g. declare ID int;), and just pass the parameter?
The only way to do this in a single statement, is to use EXECUTE BLOCK (basically an anonymous one-off procedure). It uses the same syntax as normal stored procedures in Firebird.
You can do:
execute block
as
declare id integer;
begin
insert into tableA (ID, Name , Address)
values (GEN_ID(GENERATOR,1), 'John', '123 street')
returning ID
into id;
insert into tableB (ID, TABLE_A_FK)
values (GEN_ID(GENERATOR,1), :id);
end
If necessary, execute block statements can be parameterized, so you can use parameters to provide values (instead of hard coding them). See the link above for details.

Insert a column with single quote or Apostrophe in Oracle

I am trying to insert into Table Users from Person table.
However, The first_name column in the person table contains apostrophe in the name (Eg- Rus'sell) which is preventing me from successful insertion. How do I fix this?
INSERT INTO USERS VALUES (SELECT FIRST_NAME,.........FROM PERSON);
INSERT INTO USERS VALUES (SELECT FIRST_NAME,.........FROM PERSON);
First of all, your insert statement is syntactically incorrect. It will raise ORA-00936: missing expression. The correct syntax to insert multiple records from source table is:
INSERT INTO table_name SELECT columns_list FROM source_table;
The VALUES keyword is used to insert a single record into table using following syntax:
INSERT INTO table_name(columns_list) VALUES (expressions_list);
If you already have the value stored in another table, then simple INSERT INTO..SELECT FROM should work without any issues. However, if you are trying to INSERT INTO..VALUES having single quotation marks, then the best way is to use Quoting string literal technique The syntax is q'[...]', where the "[" and "]" characters can be any of the following as long as they do not already appear in the string.
!
[ ]
{ }
( )
< >
You don't have to worry about the single-quotation marks within the string.
create table t(name varchar2(100));
insert into t values (q'[Rus'sell]');
insert into t values (q'[There's a ' quote and here's some more ' ' ']');
select * from t;
NAME
-----------------------------------------------
Rus'sell
There's a ' quote and here's some more ' ' '
I don't think your question is showing the complete details, because I can execute the following statements without any problem:
create table person( first_name varchar2(100));
create table users( first_name varchar2(100));
insert into person values ('Rus''sell');
insert into users select first_name from person;
Apologies for the obscurity if any in the question. The query I was working with was a long insert query with multiple joins.
To sum it was a stored proc where I was doing an insert, for which the data is given by long select query with multiple joins. One of the column is the FIRST_NAME column which had some values with Apostrophe in it (Rus'sell, Sa'm).
The Insert statement values were being generated as below which was causing an 'ORA-00917: missing comma' error.
INSERT INTO TABLE_NAME values (314159,0,'Rus'sell','Parks','...........)
I fixed this by Replacing the column in the select from a single quote to two single quotes, before giving it to the insert statement which basically solved the issue.
REPLACE(FIRST_NAME,'''','''''') AS FIRST_NAME
Hope it helps.

Why am I getting this error while trying to insert data ORA-00904 "PBIRTHDATE"

Can somebody correct me if I'm doing any syntax mistake while inserting into a table with complex data type?
This is my code:
CREATE TYPE ADDR AS OBJECT (
street VARCHAR2(32),
city VARCHAR2(32),
zip NUMBER(32)
);
CREATE TYPE NAMES AS OBJECT(
firstname VARCHAR2(64),
lastname VARCHAR2(64)
);
CREATE TABLE PERSON(
pid NUMBER(10),
pname NAMES,
paddr ADDR,
pbirthdate DATE
);
INSERT INTO PERSON
VALUES (1, pname('John','Doe'), paddr('None','Test',51050), TO_DATE('33445555', 'MMDDYYY'));
When you initialize an instance of an object type, you need to specify the type name, not the column name. You should also always specify the target columns in an INSERT statement.
INSERT INTO PERSON
(pid, pname, paddr, pbirthdate)
VALUES
(1, names('John','Doe'), addr('None','Test',51050), TO_DATE('33445555', 'MMDDYYY'));
^ ^
| here | here
Additionally, 33445555 is an invalid date given the format mask MMDDYYY'
The error message clearly points to this:
TO_DATE('33445555', 'MMDDYYY')
When I try to use this in a query while testing locally, I get this error message:
ORA-01843: not a valid month
The date you specified is not valid, because there is no 33rd month. Try using a valid date, and the error should go away:
TO_DATE('06242019', 'MMDDYYYY')
There is issue in TYPE usage and date:
INSERT INTO PERSON
VALUES (1, NAMES('John','Doe'), ADDR('None','Test',51050), TO_DATE('06242019', 'MMDDYYYY'));
Db Fiddle Demo
Cheers!!

cannot insert the NULL value into the column

I have been trying to insert data into the database, but it's continuously showing me this error, even though I not writing the name of the column(MemID) in the first fields, rather I am trying to enter it separately like this
create table Member (
MemID VARCHAR(20) PRIMARY KEY,
FullName VARCHAR(50),
email VARCHAR(50),
gender VARCHAR(50),
Contact_Number VARCHAR(50),
Registration_Number VARCHAR(50),
User_S# INT
);
insert into Member (FullName, email, gender, Contact_Number,
Registration_Number, User_S#) values ('Jemmy Joutapaitis',
'jjoutapaitiscf#dailymail.co.uk', 'Male', '86-(804)800-8008',
'3574884734839928', 449);
insert into Member (FullName, email, gender, Contact_Number,
Registration_Number, User_S#) values ('Cleo Glynn', 'cglynncg#i2i.jp',
'Male', '81-(694)548-5205', '5443114970343516', 450);
insert into Member (FullName, email, gender, Contact_Number,
Registration_Number, User_S#) values ('Ivonne Deetlefs',
'ideetlefsch#virginia.edu', 'Female', '86-(257)683-5628',
'3571675846170605', 451);
insert into Member(MemID)
values('Mem0');
insert into Member(MemID)
values('Mem1');
insert into Member(MemID)
values('Mem2');
This code is giving me the error
Cannot insert the value NULL into column 'MemID', table
'master.dbo.Member'; column does not allow nulls. INSERT fails.
But I am inserting MemID column separately.
And also the table which has created above is not showing in the database, is it because of the error or something else?
That's not how inserts work.
When an insert is triggered sql tries to add the row immediately, without waiting for the consequent statements (it's stands true for any dml statements not only insert).
So when your first insert is triggered then sql is trying to put null in MemID as you haven't supplied any MemID in your insert.
Try inserting it in the very first place itself in the insert. Instead of separate statement something like:
insert into Member (MemID, FullName, email, gender, Contact_Number,
Registration_Number, User_S#) values ('Mem0','Jemmy Joutapaitis',
'jjoutapaitiscf#dailymail.co.uk', 'Male', '86-(804)800-8008',
'3574884734839928', 449);
EDIT
With response to your comment , I think Prepared statements is what you're looking for.
Since you're using python you could do prepare your insert statement like:
sql = "insert into Member (MemID, FullName, email, gender, Contact_Number,
Registration_Number, User_S#) values (%s,'Jemmy Joutapaitis',
'jjoutapaitiscf#dailymail.co.uk', 'Male', '86-(804)800-8008',
'3574884734839928', 449);"
sql = sql.format(self.db_scan_table)
//whenever you've memid ready use.
self.cursor.execute(sql, ('Mem01'))
Define an AUTO_INCREMENT column to generate ID values for you:
CREATE TABLE Member (
MemID INT AUTO_INCREMENT PRIMARY KEY,
-- ...
);
If this isn't practical because you need some kind of particular string definition for these then your only option is to supply it as part of the initial INSERT call. You have a constraint here, the PRIMARY KEY value must not be NULL.
should be able to using the query below. identify the next number
insert into member(memid,......)
values ( 'MEM' + cast((select count(1) from member)+1 as varchar(100)),... )
if you want to start with 1 then + 1
otherwise remove + 1
insert into member(memid,......)
values ( 'MEM' + cast((select count(1) from member) as varchar(100)),... )

sql: insert value

I have a problem about inserting values into the table with sqlite.
supposing the table:
create table test
{
KeyName varchar(50) primary key,
KeyValue varchar (255)
};
I want to insert data like ('john', 'friend'), but I don't know whether the 'john' existed.
Currently I solve it:
using select * where KeyName = "john"
according the result from list 1, I use insert or update;
I'd like to know whether there is better solution?
thanks
you can use insert or replace which replaces the record if it already exists.
so you query be INSERT OR REPLACE INTO
check this link : http://www.sqlite.org/lang_conflict.html