Professional Documents
Culture Documents
2.creating and Renaming A Realtion
2.creating and Renaming A Realtion
2.creating and Renaming A Realtion
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:
where "column_list" defines the columns of the table with the following
syntax:
2
1.1 TO CREATE A TEMPORARY TABLE
3
1.2 TO CREATE A PERMANENT TABLE
IF IT DOESN'T EXIST
4
PROBLEM
5
1.3 CREATING A TABLE FROM AN
EXISTING TABLE
Syntax:
CREATE TABLE
<TRAGET TABLENAME>
SELECT * FROM
<SOURCE TABLE NAME>;
Example:
6
Syntax:
CREATE TABLE
<TRAGET TABLE NAME>
SELECT EMPNO, ENAME
FROM <SOURCE TABLE NAME>;
Example:
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) );
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:
char(n):
varchar(n):
int:
12
numeric(p, d):
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.
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;
name, course_id
15
AS (IN SELECT CLAUSE)
The as clause can appear in both the select and from clauses.
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):
19
SOLUTIONS
1.
SELECT CustomerName AS Customer,
ContactName AS [Contact Person]
FROM Customers;
20
3. TUPLE VARIABLE
“Find the names of all instructors whose salary is greater than at least
one instructor in the Biology department.”