Special characters in Oracle table - oracle

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.

Related

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)
);

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

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.

Creating Multiple tables in Oracle [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Creating Multiple table in Oracle
I am using Oracle Express 10g and I'm enter the following text to create 2 tables in the sql command line, but it is not working.
CREATE TABLE student (
matric_no VARCHAR2(8),
first_name VARCHAR2(20),
last_name VARCHAR2(20),
date_of_birth DATE
);
CREATE TABLE student1 (
matric_no VARCHAR2(8),
first_name VARCHAR2(20),
last_name VARCHAR2(20),
date_of_birth DATE
);
Can anyone see what I am doing wrong.
The error messgae I get is:
ORA-00911: invalid character
Thanks
Are you sure you didn't type this out in WORD?
Sometimes there are problems with "invisible" characters. For example if you hit TAB in WORD, it will store it as a special character which will thereby cause an error when you try running it in SQLPlus.
try to create the two tables separate. First one and then the other, and without semicolon:
CREATE TABLE student (
matric_no VARCHAR2(8),
first_name VARCHAR2(20),
last_name VARCHAR2(20),
date_of_birth DATE
)
remove ";" when you are editing your query in notepad and then give a try

Foreign Key Constraint Issue in Oracle

Having issues declaring a FK in Oracle 9i. I've looked at a number of examples here on SO and in some online docs (e.g. http://www.techonthenet.com/oracle/foreign_keys/foreign_delete.php) without any real luck; trying a similar syntax to that in the link generates the same error:
Error at Command Line:19 Column:4
Error report:
SQL Error: ORA-02253: constraint specification not allowed here
02253. 00000 - "constraint specification not allowed here"
*Cause: Constraint specification is not allowed here in the statement.
*Action: Remove the constraint specification from the statement.
An excerpt of the SQL itself is as below. "Line 19" refers to the line starting with CONSTRAINT
CREATE TABLE Flight (
flight_no varchar2(10) NOT NULL,
airplane_id varchar2(20) NOT NULL
CONSTRAINT flight_airplane_id_fk FOREIGN KEY (airplane_id) REFERENCES Airplane (airplane_id)
ON UPDATE RESTRICT ON DELETE RESTRICT,
dept_date date NOT NULL,
...
Alternatively, trying it without the CONSTRAINT keyword generates an error about a right parenthesis that I can't seem to see is missing.
PS: I understand ON UPDATE RESTRICT is the default behaviour in Oracle, but I prefer to be explicit wherever possible.
First off, in Oracle, there is no ON UPDATE RESTRICT or ON DELETE RESTRICT option. Those appear to be valid in other database engines but they aren't present in the constraint syntax diagram and do not appear to be valid. There is an ON DELETE clause but the only two valid options are CASCADE or SET NULL. There is no ON UPDATE clause.
If we add a comma at the end of the airplane_id definition before the constriant definition and remove the two invalid clauses, your DDL should be valid
CREATE TABLE Flight (
flight_no varchar2(10) NOT NULL,
airplane_id varchar2(20) NOT NULL,
CONSTRAINT flight_airplane_id_fk
FOREIGN KEY (airplane_id) REFERENCES Airplane (airplane_id),
dept_date date NOT NULL,
<<more columns>>
);
Put your constraint at the end:
CREATE TABLE Flight (
flight_no varchar2(10) NOT NULL,
airplane_id varchar2(20) NOT NULL,
dept_date date NOT NULL,
CONSTRAINT flight_airplane_id_fk FOREIGN KEY (airplane_id) REFERENCES Airplane (airplane_id) ON UPDATE RESTRICT ON DELETE RESTRICT
);

Resources