Data base scheme
Data base scheme
SQL-99
Schema Definition, Constraints, Queries,
and Views
Chapter Outline
SQL Data Definition and Data Type
The CREATE TABLE command in SQL
Attribute data types and domains in SQL
Specifying Constraints in SQL
Specifying attribute constraints and attribute defaults
Specifying key and referential integrity constraints
Specifying constraints on tuples using CHECK
Schema Change Statements in SQL
The DROP command
The ALTER command
Basic Queries in SQL
Introduction to Databases 2
Specifying Constraints in SQL:
Introduction to Databases 3
Schema Change Statements in SQL
The DROP command
The DROP command can be used to drop table and its definition.
Introduction to Databases 4
Schema Change Statements in SQL
The ALTER command
Introduction to Databases 5
The ALTER command (contd.)
Used to add an attribute to one of the base relations
The new attribute will have NULLs in all the tuples of the relation right after the
command is executed; hence, the NOT NULL constraint is not allowed for such an
attribute
Example:
ALTER TABLE EMPLOYEE ADD JOB VARCHAR(12);
The database users must still enter a value for the new attribute JOB for each
EMPLOYEE tuple.
This can be done using the UPDATE command.
Introduction to Databases 6
Basic Queries in SQL
Introduction to Databases 7
Retrieval Queries in SQL (contd.)
Query 0: Retrieve the birthdate and address of the employee whose name
is 'John B. Smith'.
Introduction to Databases 9
Simple SQL Queries (contd.)
Introduction to Databases 10
Simple SQL Queries (contd.)
Query 2: For every project located in 'Stafford', list the project number,
the controlling department number, and the department manager's last
name, address, and birthdate.
Q2: SELECT Pnumber, Dnum, Lname, Bdate, Address
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE Dnum=Dnumber AND Mgr_ssn=Ssn AND
Plocation='Stafford‘ ;
In Q2, there are two join conditions
The join condition Dnum=Dnumber relates a project to its controlling department
The join condition Mgr_ssn=Ssn relates the controlling department to the employee
who manages that department
Introduction to Databases 11
Ambiguous Attribute Names, Aliasing,
and Tuple Variables
Aliasing:
In SQL, we can use the same name for two (or more) attributes as
long as the attributes are in different tables
A query that refers to two or more attributes with the same name
must qualify the attribute name with the table name by prefixing the
relation name to the attribute name
Example:
EMPLOYEE.Lname, DEPARTMENT.Dname
Introduction to Databases 12
ALIASES (contd.)
Query 8: For each employee, retrieve the employee's name, and the name of his or
her immediate supervisor.
In Q8, the alternate relation names E and S are called aliases or tuple variables for
the EMPLOYEE relation
We can think of E and S as two different copies of EMPLOYEE; E represents
employees in role of supervisees and S represents employees in role of supervisors
Introduction to Databases 13
ALIASES (contd.)
Introduction to Databases 14
طيب ازاااااااااااااااااى؟
للحقل وللجدول aliasمبدايا ده بيبقى شكل ال
-------------------
SELECT column_name AS alias_name
)ALIASES (contd. ;FROM table_name
----------------------------
)SELECT column_name(s
;FROM table_name AS alias_name
نبص بقى كويس فى الجدولين اللى معانا فى صور البوست
Customersصورة لجدول العمالء
Ordersو التانية لجدول للطلبات
لو عاوزين نعمل تغيير السماء حقول معينة فى جدول العمالء مثال – بنعمل االتى
Characteristics of Relations SELECT CustomerID AS ID, CustomerName AS Customer
;FROM Customers
ولو عاوزين نعمل تغيير السم الجدول فممكن نعمل كده لتغيير اسم جدول ال
Customers
وجدول ال
Orders
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
------------------------------
cل customersفى المثال اللى فات غيرنا اسم جدول ال
oل ordersواسم جدول ال
-------------------------------
باختصار بقى
Aliasامتى نستخدم ال
او بالعربى امتى بنغير االسماء سواء التغيير ده السم الحقل او اسم الجدول
) ( Joinلو عندنا اكتر من جدول واحنا عاملينلهم ترابط مع بعض 1 -
فى بوست تانى Queryوهنعرف ازاى نعمل فانكشن فى جملة ال Queryلو استخدمنا فانكشن فى ال 2 -
لو اسماؤ الحقول كبيرة او ملهاش معنى 3 -
لو عاوز تعمل دمج الكتر من حقل مع بعض 4 -
-----------------------
لو شايف انك استفدت من البوست متوقفوش عندك
#eraasoft_sqlولو عاوز تعرف السلسلة كلها تابع الهاشتاج ده
----------------------------------
#eraasoft
Introduction to Databases #sql 15