Knowee
Questions
Features
Study Tools

1. Create a table called EMP with the following structure. Name Type EMPNO NUMBER (6) ENAME VARCHAR2 (20) JOB VARCHAR2 (10) DEPTNO NUMBER (3) SAL NUMBER (7,2) Allow NULL for all columns except ename and job. 2. Add constraints to check, while entering the empno value (i.e) empno > 100. 3. Define the field DEPTNO as unique. 4. Create a primary key constraint for the table(EMPNO). 5. Write queries to implement and practice constraints.

Question

  1. Create a table called EMP with the following structure. Name Type EMPNO NUMBER (6) ENAME VARCHAR2 (20) JOB VARCHAR2 (10) DEPTNO NUMBER (3) SAL NUMBER (7,2) Allow NULL for all columns except ename and job.
  2. Add constraints to check, while entering the empno value (i.e) empno > 100.
  3. Define the field DEPTNO as unique.
  4. Create a primary key constraint for the table(EMPNO).
  5. Write queries to implement and practice constraints.
...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here are the SQL commands to perform the tasks you've described:

  1. Create the EMP table:
CREATE TABLE EMP (
    EMPNO NUMBER(6),
    ENAME VARCHAR2(20) NOT NULL,
    JOB VARCHAR2(10) NOT NULL,
    DEPTNO NUMBER(3),
    SAL NUMBER(7,2)
);
  1. Add a constraint to check the EMPNO value:
ALTER TABLE EMP
ADD CONSTRAINT EMPNO_CHECK CHECK (EMPNO > 100);
  1. Define the DEPTNO field as unique:
ALTER TABLE EMP
ADD CONSTRAINT DEPTNO_UNIQUE UNIQUE (DEPTNO);
  1. Create a primary key constraint for the EMPNO field:
ALTER TABLE EMP
ADD CONSTRAINT EMPNO_PK PRIMARY KEY (EMPNO);
  1. To practice constraints, you can try inserting some data into the table. For example:
-- This should succeed
INSERT INTO EMP (EMPNO, ENAME, JOB, DEPTNO, SAL) VALUES (101, 'John', 'Engineer', 1, 5000.00);

-- This should fail because EMPNO is not > 100
INSERT INTO EMP (EMPNO, ENAME, JOB, DEPTNO, SAL) VALUES (99, 'Jane', 'Analyst', 2, 6000.00);

-- This should fail because ENAME is NULL
INSERT INTO EMP (EMPNO, JOB, DEPTNO, SAL) VALUES (102, 'Manager', 3, 7000.00);

-- This should fail because DEPTNO is not unique
INSERT INTO EMP (EMPNO, ENAME, JOB, DEPTNO, SAL) VALUES (103, 'Mike', 'Engineer', 1, 8000.00);

-- This should fail because EMPNO is not unique (primary key violation)
INSERT INTO EMP (EMPNO, ENAME, JOB, DEPTNO, SAL) VALUES (101, 'Anna', 'Analyst', 4, 9000.00);

Remember to commit your changes after each successful insert:

COMMIT;

This problem has been solved

Similar Questions

Write queries for the following tables:T1 ( Empno, Ename , Salary, Designation)T2 (Empno, Deptno.)(1) Display the Deptno in which Employee Seeta is working.(2) Display Empno, Ename, Deptno.

Consider following schema and write query for given statementEmp (eid,ename,city,dname,salary) Project(eid,pid,pname,location)Create tables with Primary Key, foreign key constraints in given schemas.(1) Display name of employees who belongs to Computer department.(2) Display employee id whose name starts from letter J.(3) Display all details of employees whose salary is from 10000 to 20000.(4) Display name of employees who are having maximum salary.(5) Display name of employees whose salary is higher than average salary of the employee.(6) Display name of employees whose project id is 3 and location is Mumbai

Create Table Employee with attributes firstName,LastName,SSN,Address,Salary,Birthday,Sex,SupervisorSSN,DepartmentNo

How would you add a foreign key constraint on the dept_no column in the EMP table, referring to the id column in the DEPT table?

write sql query for the following:1. create database - emp2.create table - employee with 4 columns3.To see table structure

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.