How to insert values in to tables.in Oracle

by Tibebe
(AdisAbeba(Ethiopia))

I have two tables in my Oracle database, having a relationship. My tables are Employee, and Job. The employee table have JobId foreign key.


So how can insert datas to Employee table?

The way to add data to tables in your Oracle database is to use the SQL INSERT statement which takes the form of

INSERT INTO mytable (col1,col2,col3 ... coln)
VALUES (val1,val2,val3...valn)
.

In this case having a foreign key to the Jobs table just means that the employee's job must already have been defined in the job table before the row is inserted into the employee table. If it is not then Oracle won't allow the record to be inserted and will raise an exception (an error).

Let's make the example more concrete and assume that the jobs table is defined like this:

Jobs
(job_id    NUMBER
,job_title VARCHAR2(80)
,max_salary NUMBER
,min_salary NUMBER)


and that employees is defined like this:

Employees
(emp_id   NUMBER
,name VARCHAR2(80)
,job_id NUMBER CONSTRAINT emp_job_fk REFERENCES jobs(job_id))


Let's also assume that we have 1 record in the jobs table with a job_id of 1 and a job_tit;e of 'DEVELOPER'.

To insert a record into the employees table in our Oracle database therefore we would write a SQL statement something like this:

INSERT INTO employees (emp_id,name,job_id)
VALUES (1, 'Tibebe',1);


Click here for more help with SQL.

Click here to post comments

Join in and write your own page! It's easy to do. How? Simply click here to return to Oracle Questions.