فصل دوم دیتابس
Chapter 2: Relational Model
فصل 2: مدل ارتباطی
Chapter 2: Relational Model
Structure of Relational Databases
n ساختار دیتابیسهای ارتباطی.
Fundamental Relational-Algebra-Operations
اساسات روابط-الجبری
عملیات.
Additional Relational-Algebra-Operations
اضافی الجبری-عملیاتی
Extended Relational-Algebra-Operations
گسترش رابطه الجبری-عملیات
Null Values
امقدارهای Null
Modification of the Database
اصلاح دیتابیس.
Example of a Relation
Attribute Types
انواع صفت
Each attribute of a relation has a name
هر ویژگی یک رابطه نام
دارد.
The set of allowed values for each attribute is called the domain
of the attribute
مجموعه ای از مقادیر
مجاز برای هر ویژگی، دامنه ویژگی است.
Attribute values are (normally) required to be atomic; that is,
indivisible
مقدار صفت(به طور
معمول) مورد نیاز هسته اتمی است؛ این است، تقسیم نشده است.
the value of an attribute
can be an account number,
but cannot be a set of account numbers
مقدار یک ویژگی می
تواند یک شماره حساب باشد.
اما نمی تواند مجموعه
ای از شماره های حساب باشد
Domain is said to be atomic if all its members are atomic
اگر تمام اعضای آن اتمی باشند، گفته می شود که دامنه به صورت اتمی است.
The special value null
is a member of every domain
null مقدار خاص عضو هر دامنه است.
The null value causes complications in the definition of many
operations
مقدار ارزش ناخوشایندی
را در تعریف بسیاری از عملیات ایجاد می کند.
We shall ignore the effect of null values in our main presentation and
consider their effect later
ما باید اثر ارزش صفر را در ارائه اصلی ما نادیده بگیریم و بعدا آن را در نظر بگیریم.
Relation Schema
طرح ارتباطی
Formally, given domains D1, D2, ….
Dn a relation r is a subset of
D1 x D2 x … x Dn
Thus, a relation is a set of n-tuples (a1, a2,
…, an) where each ai Î Di
Schema of a relation consists of
attribute definitions
4 name
4 type/domain
integrity constraints
Relation Instance
مثال ارتباط
The current values (relation
instance) of a relation are specified by a table
n مقادیر جاری (مثال رابطه) یک رابطه با
یک جدول مشخص می شود
An element t
of r is a tuple, represented by a row in a table
یک عنصر t از r یک دسته است که توسط یک ردیف
در یک جدول نشان داده شده است
Order of tuples is
irrelevant (tuples may be stored in an arbitrary order)
منظور از tuples بی اهمیت است (tuples ممکن است در یک نظم دلخواه ذخیره شود).
Database
دیتابیس
A database consists of multiple relations
یک دیتابیس شامل چندین
رابطه است
Information about an enterprise is broken up into parts, with each relation storing one part of the
information
اطلاعات در مورد یک
شرکت به قسمت های مختلف تقسیم می شود، و هر ارتباطی یک بخش از اطلاعات را
ذخیره می کند
Account : information about accounts depositor : which customer owns which account customer : information about customers
مثال حساب: اطلاعات مربوط به حساب ها سپرده گذار: کدام مشتری کدام حساب را دارد مشتری: اطلاعات در مورد مشتریان
The customer Relation
ارتباط مشتری
The depositor Relation
رابطه امانتگذار
Why Split Information Across Relations?
چرا تقسیم اطلاعات در سراسر رابطه؟
o Storing all information as a single relation such as bank(account_number, balance, customer_name, ..) results in
ذخیره تمام اطلاعات به عنوان یک رابطه واحد مانند بانک (account_number، تعادل، نام مشتری، ..)
o repetition of information
تکرار اطلاعات
Example:if two customers own an account (What gets repeated?)
مثلا اگر دو مشتری یک حساب داشته باشند (چه چیزی تکرار می شود؟)
o the need for null values
نیاز به ارزش صفر دارد
Example:to represent a customer without an account
به عنوان مثال، برای نشان دادن یک مشتری بدون یک حساب کاربری
o Normalization theory (Chapter 7) deals with how to design relational schemas
تئوری عادی سازی (فصل 7) به نحوه طراحی طرح های ارتباطی می پردازد.
Keys
کلید
Let K Í R
K is a superkey of
R if values for K are sufficient to identify a unique tuple
of each possible relation r(R)
K یک سوپرکل از R است اگر مقادیر K برای شناسایی یک مرتبۀ منحصر به فرد از هر رابطه احتمالی R کافی باشند،
l by “possible r ” we mean a relation r that could exist in the enterprise we are modeling.
با "احتمال R" ما معنی رابطه r است که می تواند در سازمانی که ما مدل سازی وجود دارد.
Example: {customer_name,
customer_street} and
{customer_name}
are both superkeys of Customer, if no two customers can possibly
have the same name
هر دو سوپیرکی مشتری هستند، در صورتی که دو مشتری نتوانند نام یکسان داشته باشند.
In real life, an attribute such as customer_id would be used instead of customer_name to uniquely identify customers, but we omit it to keep our examples small, and instead assume customer names are unique.
در زندگی واقعی، یک ویژگی مانند customer_id، به جای نام customer_name به منظور شناسایی مشتری ها به کار برده می شود، اما ما آن را حذف می کنیم تا مثال های ما را کوچک نگه داریم، و در عوض فرض می کنیم که نامهای مشتری منحصر به فرد هستند.
Keys (Cont.)
کلید (ادامه)
K is a candidate key if K is
minimal
K یک کلید نامزد است اگر K حداقل باشد.
Example: {customer_name} is a candidate key for Customer, since it is a superkey and no subset of it is a superkey.
به عنوان مثال: {customer_name} یک کلید نامزد برای مشتری است، زیرا این یک کلید سوپر است و هیچ زیر مجموعه ای از آن سوپر کلید نیست.
Primary key: a candidate key chosen as the principal means of identifying tuples within a relation
کلید اصلی:یک کلید نامزد انتخاب شده به عنوان وسیله اصلی برای شناسایی گره ها
در یک رابطه
Should choose an attribute whose value never, or
very rarely, changes.
باید ویژگیی را که ارزش آن هرگز و یا به ندرت تغییر می کند را انتخاب کنید.
Email address is unique, but may change
آدرس ایمیل منحصر به فرد است اما ممکن است تغییر کند.
Foreign Keys
کلید های خارجی
A relation schema may have an attribute that corresponds to the primary key of another relation. The attribute is called a foreign key.
یک طرح ارتباطی ممکن است یک ویژگی داشته باشد که مربوط به کلید اولیه یک رابطه دیگر است. این ویژگی یک کلید خارجی است.
o customer_name and account_number attributes of depositor are foreign keys to customer and account respectively.
صفات customer_name و account_number از سپرده گذاران به ترتیب به مشتری و حسابهای خارجی می باشند.
o Only values occurring in the primary key attribute of the referenced relation may occur in the foreign key attribute of the referencing relation.
فقط مقادیر رخ می دهد در ویژگی کلید اولیه ارتباط اشاره شده ممکن است در ویژگی کلید خارجی نسبت ارجاع رخ دهد.
Schema Diagram
Query Languages
زبانهای درخواستی
Language in which user requests information from the database.
زبان که در آن کاربر اطلاعات را از دیتابیسدرخواست می کند.
Categories of languages
دسته های زبان ها
Procedural
رويهاى
Non-procedural, or declarative
غیر رویه ای و یا اعلامیه ای
“Pure” languages:
"زبان خالص":
Relational algebra
جبر رابطه ای
Tuple relational calculus
Tuple حساب کاربری رابطه ای
Domain relational calculus
محاسبات ارتباطی دامنه
Pure languages form underlying basis of query languages that people use.
زبان های خالص پایه ای از زبان های پرس و جو که مردم استفاده می کنند، شکل می گیرد.
Relational Algebra
الجبر وابسته
Procedural language
پرونده زبان
Six basic operators
شش اپراتور اصلی
select: s
project: Õ
union: È
set difference: –
Cartesian product: x
rename: r
انتخاب کنید: s
پروژه: Õ
اتحادیه: È
مجموعه تفاوت: -
محصول دكارتي: x
تغییر نام دهید:
The operators take one or two
relations as inputs and produce a new relation as a result.
اپراتورها یک یا دو روابط را به عنوان ورودی ها در نظر می گیرند و یک نتیجه جدید را ایجاد می کنند.
Select Operation – Example
Rename Operation
تغییر نام عملیات
Allows us to name, and therefore to refer to, the results of
relational-algebra expressions.
به ما اجازه می دهد نام و به همین ترتیب نتایج حاصل از اصطلاحات مرتبط الجبری
را به آن اشاره کنیم.
Allows us to refer to a relation by more than one name.
n به ما اجازه می دهد تا با یک نام
بیشتر ارتباط برقرار کنیم.
Example:
r x (E)
returns
the expression E under the name X
If a relational-algebra expression E has arity n, then
returns the result of expression E under the name X, and with the
attributes renamed to A1 , A2 , …., An .
مثال:
علامت E را با نام X باز می گرداند n اگر یک بیان رابطه جبری E دارا باشد، آنگاه نتیجه نتیجه E را با نام X به دست می آورد و با صفات به A1، A2، ...، An تغییر نام داده شده است.
Composition of Operations
ترکیب عملیات
Can build expressions using multiple operations
می توان عبارات را با استفاده از عملیات چندگانه ساخت.
Example: sA=C(r x s)
r x s
sA=C(r x s)
Banking Example
مثال بانکداری
branch (branch_name, branch_city, assets)
customer (customer_name, customer_street, customer_city)
account (account_number, branch_name, balance)
loan (loan_number, branch_name, amount)
depositor (customer_name, account_number)
borrower (customer_name, loan_number)
Example Queries
Example Queries
Find the names of all customers who have a loan at the Perryridge
branch.
پیدا کردن نام تمام مشتریانی که قرض در شعبه Perryridge دارند.
Additional Operations
عملیات اضافی
Additional Operations
عملیات اضافی
Set intersection
Natural join
Aggregation
Outer Join
Division
تقاطع را تنظیم کنید
پیوستگی طبیعی
جمع آوری
تاریخ عضویت
تقسیم
All above, other than aggregation, can be expressed using basic
operations we have seen earlier
تمام موارد فوق، غیر از جمع آوری، می تواند با استفاده از عملیات اساسی که پیشتر دیده می شود بیان شود
Natural-Join Operation
عملیات ارتباطی طبیعی
Notation: r s
Let r and s be relations on schemas R and S
respectively.
اجازه دهید r و s به ترتیب در طرح های R و S باشند.
Then, r s is a relation on schema R È S obtained as follows:
سپس، r s یک رابطه در طرح R È S به دست آمده به شرح زیر است:
Consider each pair of tuples tr
from r and ts from
هر جفت trup را از r و ts از s در نظر بگیرید.
If tr and ts
have the same value on each of the attributes in R Ç S, add a tuple t to the result, where
اگر tr و ts برای هر یک از صفات در R Ç S یکسان باشند، یک tuple t را به نتیجه اضافه کنید، جایی که
t has the same value as tr on r
t مقدار همان مقدار tr در r است
t has the same value as ts on s
t دارای مقدار مشابه ts در s است
Example:
R = (A, B, C, D)
S = (E, B, D)
Result schema = (A, B, C, D, E)
r s is defined as: Õr.A, r.B, r.C, r.D, s.E (sr.B = s.B Ù r.D = s.D (r x s))
Bank Example Queries
درخواست نمونه بانک
Find the largest account balance
بزرگترین حساب را پیدا کنید
Strategy:
استراتژی:
Find those balances that are not the largest
پیدا کردن آن تعادل که بزرگترین نیستند.
Rename account relation as d so that we can compare each
account balance with all others
تغییر حساب حساب به عنوان د، بنابراین ما می توانیم هر حساب مانده با همه دیگر
مقایسه کنید
Use set difference to find those account balances that were not
found in the earlier step.
استفاده از تفاوت تنظیم برای پیدا کردن آن تعادل حساب که در مرحله قبل یافت نشد.
The query is:
پرس و جو:
Aggregate Functions and Operations
توابع و عملکردهای کلیدی
Aggregation function: takes a collection of values and returns a single value as a result.
تابع تقسیم بندی: یک مجموعه ای از مقادیر را می گیرد و یک مقدار واحد را راپس میدهد..
avg:
average value
min: minimum value
max: maximum value
sum: sum of values
count: number of values
Aggregate operation in
relational algebra
عملیات جمع در جبر رابطه ای E is any relational-algebra expression
E بیان هر رابطه جبری است.
G1, G2 …, Gn is a list of attributes on which to group (can be empty)
Each Fi is an aggregate function
Each Ai is an attribute name
Aggregate Operation – Example
عملیات کامل - به عنوان مثال
Question: Which aggregate operations cannot be expressed using basic relational operations?
سوال: کدام عملیات مجموع را نمی توان با استفاده از عملیات پایه ی ارتباطی بیان کرد؟
Aggregate Operation – Example
عملیات کامل - به عنوان مثال
Relation account grouped by branch-name:
حساب مربوط به گروه بندی بر اساس نام شاخه:
Aggregate Functions (Cont.)
توابع جمع (ادامه)
Result of aggregation does not have a name
نتیجه تجمع اسم ندارد
Can use rename operation to give it a name
من می توانم عملیات نامگذاری را نام ببرم
For convenience, we permit renaming as part of aggregate operation
برای راحتی، ما اجازه می دهیم نامگذاری به عنوان بخشی از عملیات جمع آوری شده باشد.
branch_name g sum(balance) as sum_balance (account)
Outer Join
An extension of the join operation that avoids loss of information.
گسترش یک عملیات پیوستن که
از دست دادن اطلاعات جلوگیری می کند
Computes the join and then adds tuples form one relation that does not
match tuples in the other relation to the result of the join.
محاسبه پیوستن و سپس اضافه کردن روابط یک رابطه را تشکیل می دهد که با روابط
دیگر با نتیجه ی پیوست مطابقت ندارد.
Uses null values:
استفاده از مقادیر null:
null signifies that the value is unknown or does not exist
L null نشان می دهد که مقدار ناشناخته است یا وجود ندارد
All comparisons involving null are (roughly speaking) false by definition.
همه مقایسه هایی که شامل null هستند (تقریبا بطور کلی) با تعریف نادرست است.
We shall study precise meaning of comparisons with nulls later
ما بعد معنایی دقیق مقایسه را با nulls بررسی خواهیم کرد
Outer Join – Example
Outer Join – Example
Outer Join – Example
Null Values
ارزشهای نزولی
It is possible for tuples to have a null value, denoted by null,
for some of their attributes
برای برخی از ویژگی های
آنها ممکن است برای tuples یک مقدار صفر، نشان داده شده با null باشد
null signifies an unknown
value or that a value does not exist.
null یک مقدار ناشناس است یا یک مقدار وجود ندارد.
The result of any arithmetic expression involving null is null.
نتیجه هر علامت محاسباتی که
شامل null است null است.
Aggregate functions simply ignore null values (as in SQL)
توابع به طور ساده صرفه
جویی در ارزش های صفر (مانند در SQL)
For duplicate elimination and grouping, null is treated like any other
value, and two nulls are assumed to be
the same (as in SQL)
برای حذف تکراری و گروه بندی، صفر به مانند هر مقدار دیگر رفتار می شود و دو صفر فرض می شوند همانند (مانند SQL)
Null Values
ارزشهای نزولی
Comparisons with null values return the special truth value: unknown
مقادیر صف مقدار ارزش حقیقی را مشخص می کند: ناشناخته است
If false was used instead of unknown,
then not (A
would not be
equivalent to A
>= 5
Three-valued logic using the truth value unknown:
OR: (unknown or
true) = true,
(unknown or false) = unknown
(unknown or
unknown) = unknown
AND: (true
and unknown)
= unknown,
(false and
unknown) =
false,
(unknown and
unknown) = unknown
NOT: (not
unknown) = unknown
In SQL “P is unknown” evaluates
to true if predicate P evaluates to unknown
Result of select predicate is treated as false if
it evaluates to unknown
نتیجه ی انتخابی به صورت نامعلوم به عنوان غلط محسوب می شود
Division Operation
تقسیم عملیات
Notation:
Suited to queries that include the phrase “for all”.
مناسب برای پرس و جو که شامل عبارت "برای همه" است
Let r and s be relations on schemas R and S respectively where.
l R = (A1, …, Am , B1, …, Bn )
l S = (B1, …, Bn)
The result of r ¸ s is a relation on schema
R – S = (A1, …, Am)
r ¸ s = { t | t Î Õ R-S (r) Ù " u Î s ( tu Î r ) }
Where tu means the concatenation of tuples t and u to produce a single tuple
Another Division Example
Division Operation (Cont.)
Property
l Let q = r ¸ s
l
Then q is the
largest relation satisfying q x s
Í r
Definition in terms of the basic algebra operation
Let r(R) and s(S) be relations, and let S Í R
r ¸ s = ÕR-S (r ) – ÕR-S ( ( ÕR-S (r ) x s ) – ÕR-S,S(r ))
To see why
o ÕR-S,S (r) simply reorders attributes of r
o ÕR-S (ÕR-S (r ) x s ) – ÕR-S,S(r) ) gives those tuples t in ÕR-S (r ) such that for some tuple u Î s, tu Ï r.
Bank Example Queries
درخواست نمونه بانک
Find the names of all customers who have a loan and an account at
bank.
پیدا کردن نام تمام
مشتریانی که قرض و یک حساب کاربری در بانک دارند.
Õcustomer_name (borrower)
Ç Õcustomer_name (depositor)
Find the name of all customers who have a loan at the bank and the
loan amount
Õcustomer_name, loan_number, amount (borrower loan)
Bank Example Queries
Find all customers who have an account from at least the “Downtown” and the Uptown” branches.
همه مشتریانی را پیدا کنید که دارای حساب کاربری حداقل از شاخه های "مرکز شهر" و "Uptown" هستند.
Query 1
Õcustomer_name (sbranch_name = “Downtown” (depositor account )) Ç
Õcustomer_name (sbranch_name = “Uptown” (depositor account))
l Query 2
Õcustomer_name, branch_name (depositor account) ¸ rtemp(branch_name) ({(“Downtown” ), (“Uptown” )})
Note that Query 2 uses a constant relation.
Bank Example Queries
Ø Find all customers who have an account at all branches located in Brooklyn city.
پیدا کردن همه مشتریانی که دارای حساب کاربری در تمام شاخه های واقع در شهر بروکلین هستند.
Ø Õcustomer_name, branch_name (depositor account) ¸ Õbranch_name (sbranch_city = “Brooklyn” (branch))
Formal Definition
A basic expression in the relational algebra consists of either one of
the following:
یک عبارت اولیه در جبر رابطه
ای شامل یکی از موارد زیر است:
A relation in the database
A constant relation
Let E1 and E2 be relational-algebra expressions; the
following are all relational-algebra expressions:
E1 È E2
E1 – E2
E1 x E2
sp (E1),
P is a predicate on attributes in E1
Õs(E1), S is a list consisting of some of the attributes in E1
r x (E1), x is the new name for the result of E1
Select Operation
n Notation: s p(r)
n p is called the selection predicate
n Defined as: sp(r) = {t | t Î r and p(t)}
Where p is a formula in propositional calculus consisting of terms connected by : Ù (and), Ú (or), Ø (not) Each term is one of:
op or
where op is one of: =, ¹, >, ³. £
Example of selection: s branch_name=“Perryridge”(account)
Project Operation
عملیات پروژه
Notation:
where A1, A2 are attribute names and r is a relation name.
The result is defined as the relation of k columns obtained by erasing the columns that are not listed
نتیجه به عنوان رابطه k ستون ها به دست آمده توسط پاک کردن ستون هایی که در فهرست ذکر نشده تعریف نمی شود
n Duplicate rows removed from result, since relations are sets
ردیف های تکراری از نتیجه حذف شده، از آنجا که روابط مجموعه هستند
n Example: To eliminate the branch_name attribute of account Õaccount_number, balance (account)
Union Operation
n Notation: r È s
n Defined as:
r È s = {t | t Î r or t Î s}
n For r È s to be valid.
1. r, s must have the same arity (same number of attributes)
2. The attribute domains must be compatible (example: 2nd column of r deals with the same type of values as does the 2nd column of s)
n Example: to find all customers with either an account or a loan Õcustomer_name (depositor) È Õcustomer_name (borrower)
Set Difference Operation
تنظیم عمل تفاوت
n Notation r – s
n Defined as:
r – s = {t | t Î r and t Ï s}
n Set differences must be taken between compatible relations.
r and s must have the same arity
attribute domains of r and s must be compatible
Cartesian-Product Operation
Notation r x s
Defined as:
r x s = {t q | t Î r and q Î s}
Assume that attributes of r(R) and s(S) are disjoint. (That is, R Ç S = Æ).
If attributes of r and s are not disjoint, then renaming must be used.
Set-Intersection Operation
عملیات تنظیم تقاطع
n Notation: r Ç s
n Defined as:
n r Ç s = { t | t Î r and t Î s }
n Assume:
l r, s have the same arity
l attributes of r and s are compatible
n Note: r Ç s = r – (r – s)
Assignment Operation
عملیات تخصصی
The assignment operation (¬) provides a convenient way to express complex queries.
عملیات تخصیص (¬) یک راه مناسب برای بیان سوالات پیچیده فراهم می کند
Write query as a sequential program consisting of
a series of assignments
followed by an expression whose value is displayed as a result of the query.
l Assignment must always be made to a temporary relation variable.
n Example: Write r ¸ s as
temp1 ¬ ÕR-S (r ) temp2 ¬ ÕR-S ((temp1 x s ) – ÕR-S,S (r )) result = temp1 – temp2
The result to the right of the ¬ is assigned to the relation variable on the left of the ¬.
May use variable in subsequent expressions.
Extended Relational-Algebra-Operations
مرتبط با جبر عملیاتی گسترش یافته است
Generalized Projection
پروژۀ عمومی
Aggregate Functions
وظایف تجمعی
Outer Join
Generalized Projection
پروژۀ عمومی
Extends the projection operation by allowing arithmetic functions to be used in the projection list.
عملیات پیش بینی را با اجازه دادن به توابع محاسباتی در لیست پروژکتور گسترش می دهد.
n E is any relational-algebra expression
n Each of F1, F2, …, Fn are are arithmetic expressions involving constants and attributes in the schema of E.
n Given relation credit_info(customer_name, limit, credit_balance), find how much more each person can spend:
Õcustomer_name, limit – credit_balance (credit_info)
Modification of the Database
اصلاح دیتابیس
n The content of the database may be modified using the following operations:
محتوای پایگاه داده ممکن است با استفاده از عملیات زیر اصلاح شود:
l Deletion
l Insertion
l Updating
n All these operations are expressed using the assignment operator.
همه این عملیات با استفاده از اپراتور تخصیص بیان شده است.
Deletion
n A delete request is expressed similarly to a query, except instead of displaying tuples to the user, the selected tuples are removed from the database.
درخواست حذف به طور مشابه به یک پرس و جو بیان می شود، به جز به جای نمایش داده ها به کاربر، تپه های انتخاب شده از پایگاه داده حذف می شوند.
n Can delete only whole tuples; cannot delete values on only particular attributes
تنها می تواند تمام tuple ها را حذف کند؛نه می تواند مقادیر تنها در خصوص ویژگی ها را حذف کند
n A deletion is expressed in relational algebra by:
r ¬ r – E
where r is a relation and E is a relational algebra query.
نکته:شمامیتوانیداین فایل راازاین ویبلاک نیزبدست بیاورید:
http://www.sayedbaqirhashemi11.blogfa.com
chapter two database translated...برچسب : نویسنده : sayedbaqirhashemi11 بازدید : 250