OR Database/Type in Oracle Express/SQL Developer - sql

I try to do some stuff wie object relational Databases. I have Oracle Express and the SQL Developer. Everything works fine.
I can create my own, custom types and insert some rows. The problem is that it would not be display right. I think it is normally because they should be display object/types in a column...
Is there a solution to display the objects in the columns?
This is my code.
p.s. i am from germany. ANSCHRIFT_T is a type for an address with name of the tress (Strasse) and house number (Hausnr).
CREATE OR REPLACE TYPE ANSCHRIFT_T AS OBJECT (STRASSE CHAR(12), HAUSNR CHAR(3));
CREATE TABLE VERTRETER(V_NR NUMBER(4) PRIMARY KEY, ANSCHRIFT ANSCHRIFT_T);
INSERT INTO VERTRETER VALUES (1, ANSCHRIFT_T('TESTWEG','14'));
INSERT INTO VERTRETER VALUES (2, ANSCHRIFT_T('BLA BLA WEG', '25'));
SELECT V_NR, ANSCHRIFT FROM VERTRETER;
But the output its just this:

Double click on the value. You'll see your data.
Or, you can tell SQL Developer to show those values by default.
Preferences > Advanced, display structures
And then run your query again.
[

You need to add an alias for the table name and then you can select the values from the object column using table_alias.object_column.object_attribute. Like this:
Query:
SELECT V_NR,
v.ANSCHRIFT.STRASSE,
v.ANSCHRIFT.HAUSNR
FROM VERTRETER v;
Output:
V_NR ANSCHRIFT.STRASSE ANSCHRIFT.HAUSNR
---------- ----------------- ----------------
1 TESTWEG 14
2 BLA BLA WEG 25

Related

Store list of words to sql

I have a sqlite database with id, name and reason
I have a text file that contains a list in this format:
Name1
Name2
How can I store each name in a different row? so name will be filled in and reason will be empty
Use INSERT to add individual records into a table:
INSERT INTO table ( id, name ) VALUES ( <id>, <name> );
where I use <id>, <name> as placeholders for actual values ( <name> is the data from the file, <id> you have to supply unless id is an autoincrement column ). You need 1 statement for each line in the file.
How you create these statements is up to you:
By hand
In your favorite text editor that has a column mode
Write code and use a library that can interface with a text file as a database.
Use sepcialized tools
In the case of 3.) you would rewrite your insert statement along
INSERT INTO table ( id, name ) SELECT ... FROM <pseudo-db>
Consult the docs of the respective library on how to build the specific statement.
INSERT INTO Students(Id, Name, Reason) VALUES(1, 'Ahmad','');
INSERT INTO Students VALUES(2, 'Aly','');
SELECT * FROM Students;
Hence,it will show a list of name in row with reason column empty.

ID generation in Oracle

I'm trying to create a table in Oracle as follows:
create table person (
person_id number(5) generated always as identity
minvalue 1
maxvalue 99999
increment by 1 start with 1
cycle
cache 10,
firstname varchar(10),
lastname varchar(10));
This works all fine, but when I try something like this:
insert into person
values ('firstname', 'lastname');
I'm getting an error. When I try putting in a number where the ID should be, I get a different error.
I'm confused because I originally thought that formatting the person_id in this way automatically generated unique ID values, so that I wouldn't be able to put them in.
I'm also a bit confused on how this all fits into JDBC. If I'm working with Java and I insert a tuple into such a table, how am I able to get the generated ID into my Java program? I can't search by first and last name, because there could be two people with the same name.
You need to list the columns for an insert:
insert into person(firstname, lastname)
values ('firstname', 'lastname');
The fact that the column is generated doesn't affect the syntax of the insert. So, with no columns, the insert is the same as:
insert into person(person_id, firstname, lastname)
values ('firstname', 'lastname');
And with two values and three columns, you are getting an error.

sql query to display unavailable number?

i have some list of numbers with me, i want to check that numbers in a specified table available or not, if any number is not available in the table from the list of given numbers, then that unavailable number need to be display
I didnt sure that i understad your query correctly,
but it seems you mean somthing like this:
select *
from TABLE_NAME t
where t.COLUMN_NAME not in (1,2,3.... (#your list values#))
save the list of number first:
Create table list(id int);
insert into list(id)values(1);
insert into list(id)values(2);
insert into list(id)values(3);
insert into list(id)values(4);
Create sample table
Create table chck_list(id int,name varchar2(10));
insert into chck_list(id,name)values(1,'Micheal');
insert into chck_list(id,name)values(2,'John');
insert into chck_list(id,name)values(8,'Jack');
Query to check if there are id in list not showed in chck_list
select list.id
from list lst
where not exists(select 1 from chck_list chck where chck.id=lst.id)
Result
=========================
id
=========================
3
4

how to re-insert all of the existing fields but only change one field data in postgresql?

I have bunch of data in one table, for example lets say I have a table with four columns.
id,name,age,address.
id stays the same but all the other fields changes. i.e it will look something like this.
id name age address
1 steve 20 test address 1
1 mark 21 test address 2
and now I want to have the same data set but only change the id, for example everything will stay the same but only the id changes to look something like this:
id name age address
1 steve 20 test address 1
1 mark 21 test address 2
2 steve 20 test address 1
2 mark 21 test address 2
but I don't want to insert it one field at a time, because I have 100's of fields and to do it like that will waste so much time.
so my question is how can i do this as easily as possible in Postgres?
I forgot to mention the other fields are not set by default and I want to copy the data from a specific field id, so in general it should be like. first I want to grab all the fields from a specific id, then insert these fields again but now with the new id which is 2 for example.
insert into table(id,name,age,address) select 2,name,age,address from table
create table list (id int,name text,age int,address text);
insert into list values (1,'steve',20,'AAAAAAAAA');
insert into list values (1,'mark',21,'BBBBBBBBB');
Now create a function for the INSERT Operation:
create or replace function ins_list(_updateID int ,_getID int) returns void as
$$
-- _updateID : - the new ID that you want to insert(ex. 2)
-- _getID : - the ID that you need to search to get values from name,age,address (ex.1)
insert into list (id,name,age,address) select _updateID,name,age,address from list where id = _getID
$$
language sql
to Insert you just need to call like
select ins_list(2,1);

How do I stitch together tables using SQL?

Ok, I am learning SQL and just installed SQL Server. I've read about outer joins and inner joins but am not sure either is what I want. Basically, I want to reconstruct a text file that has been "chopped" up into 5 smaller text files. The columns are the same across all 5 text files, e.g. name, age, telephone #, etc. The only difference is that they have different numbers of rows of data.
What I'd like to do is "append" the data from each file into one "mega-file". Should I create a table containing all of the data, or just create a view? Then, how do I implement this...do I use union? Any guidance would be appreciated, thanks.
Beyond your immediate goal of merging the five files it sounds like you want the data contained in your text files to be generally available for more flexible analysis.
An example of why you might require this is if you need to merge other data with the data in your text files. (If this is not the case then Oded is right on the money, and you should simply use logparser or Visual Log Parser.)
Since your text files all contain the same columns you can insert them into one table*.
Issue a CREATE statement defining your table
Insert data into your newly created table**
Create an index on field(s) which might often be used in query predicates
Write a query or create a view to provide the data you need
*Once you have your data in a table you can think about creating views on the table, but to start you might just run some ad hoc queries.
**Note that it is possible to accomplish Step 2 in other ways. Alternatively you can programmatically construct and issue your INSERT statements.
Examples of each of the above steps are included below, and a tested example can be found at: http://sqlfiddle.com/#!6/432f7/1
-- 1.
CREATE TABLE mytable
(
id int identity primary key,
person_name varchar(200),
age integer,
tel_num varchar(20)
);
-- 2. or look into BULK INSERT option https://stackoverflow.com/q/11016223/42346
INSERT INTO mytable
(person_name, age, tel_num)
VALUES
('Jane Doe', 31, '888-888-8888'),
('John Smith', 24, '888-555-1234');
-- 3.
CREATE UNIQUE INDEX mytable_age_idx ON mytable (age);
-- 4.
SELECT id, person_name, age, tel_num
FROM mytable
WHERE age < 30;
You need to look into using UNION.
SELECT *
FROM TABLE1
UNION
SELECT *
FROM TABLE2
And I would just create a View -- no need to have a stored table especially if the data ever changes.