2.creating and Renaming A Realtion

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

CREATING AND

RENAMING A RELATION
1. CREATE TABLE (PERMANENT)

 The create table statement allows you to create a new table in the
database. It has a number of syntax formats:

CREATE TABLE tbl_name (column_list);

where "column_list" defines the columns of the table with the following
syntax:

col_name col_type col_options,


col_name col_type col_options,...
col_name col_type col_options

2
1.1 TO CREATE A TEMPORARY TABLE

 CREATE TEMPORARY TABLE tbl_name (column_list);

3
1.2 TO CREATE A PERMANENT TABLE
IF IT DOESN'T EXIST

 CREATE TABLE tbl_name IF NOT EXISTS


(column_list);

To create a permanent table with data types and data


generated from a select statement:

CREATE TABLE IF NOT EXISTS User (Login


VARCHAR(8), Password CHAR(8));

4
PROBLEM

 1. Write a query to create a table employee with empno,


ename, designation, and salary.

CREATE TABLE EMP (EMPNO NUMBER (4),


ENAME VARCHAR2 (10),
DESIGNATIN VARCHAR2 (10),
SALARY NUMBER (8,2));

5
1.3 CREATING A TABLE FROM AN
EXISTING TABLE

 Write a query for create a table from an existing table with


all the fields

 Syntax:

CREATE TABLE
<TRAGET TABLENAME>
SELECT * FROM
<SOURCE TABLE NAME>;

 Example:
6

CREATE TABLE EMP1 AS SELECT * FROM EMP;


1.4 CREATING A TABLE FROM AN EXISTING
TABLE WITH SELECTED FIELDS
 Write a query for create a from an existing table with selected
fields

 Syntax:

CREATE TABLE
<TRAGET TABLE NAME>
SELECT EMPNO, ENAME
FROM <SOURCE TABLE NAME>;

 Example:

CREATE TABLE EMP2 AS SELECT EMPNO, ENAME FROM 7


EMP;
CLASS WORK

1. Create the following table with required constraints

8
 2. Create a table SALARY using CUSTOMERS table and
having fields customer ID and customer SALARY

9
SOLUTIONS
 1. CREATE TABLE CUSTOMERS( ID INT NOT NULL,
NAME VARCHAR (20) NOT NULL, AGE INT NOT NULL,
ADDRESS CHAR (25) , SALARY DECIMAL (18, 2),
PRIMARY KEY (ID) );

 2. CREATE TABLE SALARY AS SELECT ID, SALARY


FROM CUSTOMERS;

10
1.5 CREATING A TABLE FROM AN EXISTING TABLE (ONLY
STRUCTURE NOT THE DATA)

 Write a query for create a new table from an existing table without any
record:

 Syntax:

CREATE TABLE
<TRAGET TABLE NAME>
AS SELECT * FROM
<SOURCE TABLE NAME>
WHERE
<FALSE CONDITION>;

Example:

CREATE TABLE EMP3 AS SELECT * FROM EMP WHERE 1>2;


11
2. DATA TYPES

 The SQL standard supports a variety of built-in types, including:

 char(n):

 A fixed-length character string with user-specified length n. The full form,


character, can be used instead.

 varchar(n):

 A variable-length character string with user-specified maximum length n. The


full form, character varying, is equivalent.

 int:
12

 An integer (a finite subset of the integers that is machine dependent). The


 smallint:

 A small integer (a machine-dependent subset of the integer type).

 numeric(p, d):

 A fixed-point number with user-specified precision.

The number consists of p digits (plus a sign), and d of the p digits are to the right of the decimal
point. Thus, numeric(3,1) allows 44.5 to be stored exactly, but neither 444.5 or 0.32 can be stored
exactly in a field of this type.

 real, double precision:

 Floating-point and double-precision floating-point numbers with machine-dependent


precision.

 float(n):
13
 A floating-point number, with precision of at least n digits.
2. THE RENAME OPERATION

Relation: Instructor

14

Relation : Teaches
select name, course_id
from instructor, teaches
where instructor.ID= teaches.ID;

 The result of this query is a relation with the


following attributes:

name, course_id

15
AS (IN SELECT CLAUSE)

 The as clause can appear in both the select and from clauses.

 For example, if we want the attribute name name to be replaced


with the name instructor_name, we can rewrite the preceding
query as:

select name as instructor name, course id


from instructor, teaches
where instructor.ID= teaches.ID;
16
AS (IN FROM CLAUSE)

 For all instructors in the university who have taught


some course, find their names and the course ID of all
courses they taught.”

 select T.name, S.course id


 from instructor as T, teaches as S

 where T.ID= S.ID;

17
CLASS WORK

18
 1. Specify two aliases, one for the CustomerName column and
one for the ContactName column

 2.  select all the orders from the customer with Customer ID=4
and cname=(Around the Horn).

We use the "Customers" and "Orders" tables, and give them the
table aliases of "c" and "o" respectively (Here we have used
aliases to make the SQL shorter):

 3. Do the same Ex. 2 without using AS keyword

19
SOLUTIONS

 1.
SELECT CustomerName AS Customer,
ContactName AS [Contact Person]
FROM Customers;

2. SELECT o.OrderID, o.OrderDate, c.CustomerName


FROM Customers AS c, Orders AS o
WHERE c.CustomerName="Around the Horn" AND
c.CustomerID=o.CustomerID;
3.
 SELECT Orders.OrderID, Orders.OrderDate, Customers.CustomerName
FROM Customers, Orders
WHERE Customers.CustomerName="Around the Horn" AND
Customers.CustomerID=Orders.CustomerID;

20
3. TUPLE VARIABLE
 “Find the names of all instructors whose salary is greater than at least
one instructor in the Biology department.”

 We can write the SQL expression:

select distinct T.name


from instructor as T, instructor as S
where T.salary > S.salary and S.dept name = ’Biology’;

In the above query, T and S can be thought of as copies of the


relation instructor, but more precisely, they are declared as aliases,
that is as alternative names, for the relation instructor.
21

You might also like