want an optimal solution for inserting more than one data in a single column of a single row - oracle

DROP TABLE District_Info;
CREATE TABLE District_Info(
Dname VARCHAR2(20) primary key,
Boundary_dist VARCHAR2(20)
);
for this code, for each Dname there are more than one Boundary_dist. it would be better if i could use an array of varchar2(20) and insert as much boundary_dist as i need to. want some helpful suggestions.

As already mentioned, the ideal way to do this would be to store an additional row for each DName/Boundary_Dist combination. The simplest way to do with a structure this simple is to just change your primary key:
CREATE TABLE District_Info(
Dname VARCHAR2(20) primary key,
Boundary_dist VARCHAR2(20) primary key);
If you're going to need other data in that table that has a 1:1 correlation to District, you would be better off splitting BoundayDist into a separate table:
CREATE TABLE District_Info(
Dname VARCHAR2(20) primary key,
Other_info VARCHAR2(20)
);
CREATE TABLE District_Boundary(
Dname VARCHAR2(20) primary key,
Boundary_dist VARCHAR2(20) primary key);
If you really insist on storing more that one value per row, you can use a user-defined datatype:
create type varchar_20_list as table of varchar2(20);
CREATE TABLE District_Info(
Dname VARCHAR2(20) primary key,
Boundary_dist varchar_20_list);

you need to create a separate table for Boundary_dist and create a foreign key which references the District_Info table.

Agreed, it'd be best to have Boundary_dist in a second table and connect them with an ID, but if you must typically what I do is store it as "1,2,3,4,5" that way in the code I can manipulate that pretty easily with PHP using explode or with mysql using IN.

Related

'Error: ORA-01722: invalid number' when inserting into a table?

I'm very new to SQL and I am having trouble with an assignment that involves inserting records into a table. So far I have:
insert into show (show_id,production_company_id,
genre,name_of_show,mpaa_rating,feature_type) values
(1,1,'comedy','click','r','movie');
It keeps telling me the specified number is invalid and to specify a number but I don't know what that means since there is already a number there. I tried looking at other similar questions on the site, but most of them were a lot more complicated than what I have here. Thanks for any help.
edit: also, production_company_id is a foreign key if that helps at all.
here is the original table:
CREATE TABLE show (
SHOW_ID number(6,0) PRIMARY KEY,
PRODUCTION_COMPANY_ID NUMBER(6,0) NOT NULL REFERENCES PRODUCTION_COMPANY (PRODUCTION_COMPANY_ID),
GENRE VARCHAR(25),
NAME_OF_SHOW VARCHAR(25),
MPAA_RATING VARCHAR(25),
FEATURE_TYPE VARCHAR(25)
);
Please make sure that PRODUCTION_COMPANY_ID is defined as number in the table PRODUCTION_COMPANY, because in your table creation of show, production_company_id refers to that table.
If not, you can modify the column type like:
ALTER TABLE PRODUCTION_COMPANY
MODIFY PRODUCTION_COMPANY_ID number;
Or if you do not want to change the type of that column for some reason, you can try:
insert into show (show_id,production_company_id,
genre,name_of_show,mpaa_rating,feature_type) values
(1,'1','comedy','click','r','movie');

Special characters in Oracle table

I am trying to create table as below,
create table "customer`£`s"(customer_id number(10) not null,
UNIT VARCHAR2(50) not null,
city varchar2(50), constraint customers_pk PRIMARY KEY(customer_id));
But getting Invalid character error.
Can anyone help?
We are working on similar projects. I was able to create the table.

Is it possible to pre-define column data types?

When creating a table
Create table user(
username varchar2(30)
);
Is it possible to somewhere create a static or pre-defined data type?
short_name varchar2(30)
And then use this in the create table statement?
Create table user(
username short_name
);
Edit:
My initial thought was to build up and structure different varchar2 (and other data types) for easy reference when writing sql statements, example:
tiny_name varchar2(10)
short_name varchar2(30)
medium_name varchar2(60)
long_name varchar2(120)
The closest (I have seen so far) seems to be the use of TYPE, but that does not seem to cover what I am specifying in my question

SQL Oracle - checking value in another table with CHECK or TRIGGER

Is any way, how to create table with CHECK (or maybe TRIGGER?), that will check, if inserted value is already in another table?
Example
CREATE TABLE Employee(
Name VARCHAR(10),
Adress VARCHAR(20)
);
CREATE TABLE Section(
Section_name VARCHAR(10),
Managers_name VARCHAR(10)
);
And I want to check, that value inserted to Managers_name is already in Employee, if it isnt, then print error.
I found any ways, how it could be done, but everything I tried in Oracle, didnt work.
Add a PRIMARY KEY constraint to the employee table and a FOREIGN KEY constraint to the section table:
CREATE TABLE Employee(
Name VARCHAR(10) CONSTRAINT Employee_Name_PK PRIMARY KEY,
Adress VARCHAR(20)
);
CREATE TABLE Section(
Section_name VARCHAR(10),
Managers_name VARCHAR(10)
CONSTRAINT section_manager_fk REFERENCES Employee( Name )
);
Check constraint is, as explained by mt0, devoted to single table.
Trigger is to be avoided for consistency reasons: while you're selecting a record, another session might be in the process of deleting it.
The Foreign Key is the correct way to implement it: Trap FK constraint violation in your code (ORA-02291 i guess), search for "section_manager_fk" in the message and finally rewrite a user-friendly error message.

How to Create a Foreign Key from a Table to a Custom Datatype

Working with Oracle Express 11g, learning the interactions for object-relational databases.
Running into trouble while trying to create a foreign key from a normal table, to a table of a custom object (customers)
The customer object is as follows (customer_id NUMBER, fname VARCHAR2, lname VARCHAR2), for our setup, we wish to join a table Applications via customer_id NUMBER. Within the SQL Workshop, selecting the customers table is not even an option, and we cannot find syntax to manually create this relationship.
Any experts have some clues to help figure this one out?
create or replace type customer is object
(
customer_id NUMBER,
fname VARCHAR2(100),
lname VARCHAR2(100)
);
/
create table customers of customer
(
constraint customer_pk primary key(customer_id)
);
create table applications
(
application_id number,
customer_id number,
constraint applications_fk1 foreign key (customer_id)
references customers(customer_id)
);

Resources