SQL Fundmentals
SQL Fundmentals
SQL language
هي اختصار لجملة structured query language
RDBMS
يتم تخزين البيانات الموجودة في RDBMSفي كائنات قاعدة بيانات تسمى الجداول .الجدول
عبارة عن مجموعة من إدخاالت البيانات المرتبطة ويتكون من أعمدة وصفوف.
يتم تقسيم كل جدول إلى كيانات أصغر تسمى الحقول .تتكون الحقول الموجودة في جدول
العمالء من معرف العميل واسم العميل واسم جهة االتصال والعنوان والمدينة والرمز البريدي
والبلد .الحقل هو عمود في جدول مصمم لالحتفاظ بمعلومات محددة حول كل سجل في
الجدول.
السجل ،الذي يسمى أيًض ا الصف ،هو كل إدخال فردي موجود في الجدول .على سبيل المثال،
يوجد 91سجًال في جدول العمالء أعاله .السجل هو كيان أفقي في الجدول.
العمود هو كيان رأسي في جدول يحتوي على كافة المعلومات المرتبطة بحقل معين في
الجدول.
SQL Syntax
Most of the actions you need to perform on a database are done with SQL
statements.
The following SQL statement returns all records from a table named
"Customers":
Example
Database Tables
يتم تعريف كل جدول باسم (على سبيل.تحتوي قاعدة البيانات غالًبا على جدول واحد أو أكثر
. ويحتوي على سجالت (صفوف) تحتوي على بيانات،)" "العمالء" أو "الطلبات،المثال
،يحتوي الجدول أعاله على خمسة سجالت (واحد لكل عميل) وسبعة أعمدة (معرف العميل
.) والدولة، والرمز البريدي، والمدينة، والعنوان، واسم جهة االتصال،واسم العميل
SELECT - extracts data from a database.
SQL requires single quotes around text values (most database systems
will also allow double quotes).
. لفرز مجموعة النتائج بترتيب تصاعدي أو تنازليORDER BY يتم استخدام الكلمة األساسية
DESC
Alphabetically DESC
The following SQL statement selects all customers from the "Customers"
table, sorted by the "Country" and the "Customer Name" column. This
means that it orders by Country, but if some rows have the same Country,
it orders them by Customer Name:
The SQL AND Operator
The WHERE clause can contain one or many AND operators.
The AND operator is used to filter records based on more than one
condition, like if you want to return all customers from Spain that starts
with the letter 'G':
مثل إذا، لتصفية السجالت بناًء على أكثر من شرط واحدAND يتم استخدام عامل التشغيل
:"G" كنت تريد إرجاع جميع العمالء من إسبانيا الذين يبدأون بالحرف
Example
Select all Spanish customers that starts with either "G" or "R":
The following SQL statement selects all fields from Customers where
either City is "Berlin", Customer Name starts with the letter "G"
or Country is "Norway":
In the select statement below we want to return all customers that are
NOT from Spain:
The SQL INSERT INTO Statement
The INSERT INTO statement is used to insert new records in a table.
2. If you are adding values for all the columns of the table, you do not
need to specify the column names in the SQL query. However, make sure
the order of the values is in the same order as the columns in the table.
Here, the INSERT INTO syntax would be as follows:
It is not possible to test for NULL values with comparison operators, such
as =, <, or <>.
We will have to use the IS NULL and IS NOT NULL operators instead.
The SQL UPDATE Statement
The UPDATE statement is used to modify the existing records in a table.
،الدالة التجميعية هي دالة تقوم بإجراء عملية حسابية على مجموعة من القيم
وترجع قيمة واحدة.
غالًبا ما يتم استخدام الداالت التجميعية مع عبارةGROUP BY الخاصة بعبارة
SELECT. تقوم جملةGROUP BY بتقسيم مجموعة النتائج إلى مجموعات من
القيم ويمكن استخدام الدالة التجميعية إلرجاع قيمة واحدة لكل مجموعة.
The most commonly used SQL aggregate functions are:
The MIN() function returns the smallest value of the selected column.
The MAX() function returns the largest value of the selected column.
There are two wildcards often used in conjunction with the LIKE operator:
NOT IN
By using the NOT keyword in front of the IN operator, you return all
records that are NOT any of the values in the list.
The BETWEEN operator is inclusive: begin and end values are included.
BETWEEN Dates
Or
AS is Optional
Actually, in most database languages, you can skip the AS keyword and
get the same result:
Concatenate Columns
بناًء على عمود مرتبط بينهما، لدمج صفوف من جدولين أو أكثرJOIN يتم استخدام جملة
LEFT (OUTER) JOIN: Returns all records from the left table, and the
matched records from the right table
RIGHT (OUTER) JOIN: Returns all records from the right table, and
the matched records from the left table
Tip: FULL OUTER JOIN and FULL JOIN are the same.
SQL الكلمة األساسية الكاملة للصلة الخارجية
بإرجاع كافة السجالت عندما يكون هناكFULL OUTER JOIN تقوم الكلمة األساسية
.)2 ) أو األيمن (الجدول1 تطابق في سجالت الجدول األيسر (الجدول
Every SELECT statement within UNION must have the same number
of columns
SELECT لدمج مجموعة النتائج المكونة من عباراتUNION يتم استخدام عامل التشغيل
.أو أكثر
مثل، بتجميع الصفوف التي لها نفس القيم في صفوف ملخصGROUP BY تقوم عبارة
.""ابحث عن عدد العمالء في كل بلد
The column parameters specify the names of the columns of the table.
The datatype parameter specifies the type of data the column can hold
(e.g. varchar, integer, date, etc.).
The ALTER TABLE statement is also used to add and drop various
constraints on an existing table.
. إلضافة أو حذف أو تعديل األعمدة في جدول موجودALTER TABLE يتم استخدام عبارة
. أيًض ا إلضافة وإسقاط قيود مختلفة على جدول موجودALTER TABLE يتم استخدام عبارة
ADD Column
DROP COLUMN
RENAME COLUMN
Constraints can be specified when the table is created with the CREATE
TABLE statement, or after the table is created with the ALTER
TABLE statement.
CREATE INDEX - Used to create and retrieve data from the database
very quickly
Both the UNIQUE and PRIMARY KEY constraints provide a guarantee for
uniqueness for a column or set of columns.
However, you can have many UNIQUE constraints per table, but only
one PRIMARY KEY constraint per table.
Primary keys must contain UNIQUE values, and cannot contain NULL
values.
A table can have only ONE primary key; and in the table, this primary
key can consist of single or multiple columns (fields)
The following SQL creates a PRIMARY KEY on the "ID" column when the
"Persons" table is created:
MySQL:
To create a PRIMARY KEY constraint on the "ID" column when the table
is already created, use the following SQL:
MySQL:
A FOREIGN KEY is a field (or collection of fields) in one table, that refers
to the PRIMARY KEY in another table.
The table with the foreign key is called the child table, and the table
with the primary key is called the referenced or parent table.
Look at the following two tables:
Notice that the "PersonID" column in the "Orders" table points to the
"PersonID" column in the "Persons" table.
The "PersonID" column in the "Persons" table is the PRIMARY KEY in the
"Persons" table.
The FOREIGN KEY constraint prevents invalid data from being inserted
into the foreign key column, because it has to be one of the values
contained in the parent table.
MySQL: