Chapterبەش 9 · Lectureوانە 9.1
Databases and SQL basicsبنکەدراوەکان و بنەماکانی SQL
What a database is and why PHP sites use MySQL, the MySQL data types (with TIMESTAMP vs DATETIME), and the first SQL statements: CREATE, INSERT, SELECT, UPDATE and DELETE.
چییە و بۆچی PHP بەکار دەهێنن، لە MySQLدا (لەگەڵ جیاوازیی TIMESTAMP و DATETIME) و یەکەم فەرمانەکانی : CREATE، INSERT، SELECT، UPDATE و DELETE.
What you will learnچی فێر دەبیت
- Explain what a database, a table, a row (record) and a column (field) are, using a spreadsheet as a comparison.
- Give four reasons why PHP and MySQL are used together, and know that XAMPP runs MariaDB.
- Choose suitable MySQL data types for numbers, text and dates, and explain how TIMESTAMP differs from DATETIME.
- Write CREATE DATABASE, CREATE TABLE, INSERT, SELECT, UPDATE and DELETE statements with correct quotes.
- Avoid the classic mistakes: UPDATE or DELETE without WHERE, curly quotes, and misreading ORDER BY ... ASC.
- ڕوون بکەیتەوە ، ، ڕیز () و چین، بە بەراوردکردن لەگەڵ پەڕەی ئێکسڵ.
- چوار هۆکار بهێنیتەوە بۆ پێکەوە بەکارهێنانی PHP و MySQL و بزانیت XAMPP کار پێ دەکات.
- گونجاو بۆ ژمارە، دەق و بەروار هەڵبژێریت و ڕوون بکەیتەوە TIMESTAMP چۆن لە DATETIME جیاوازە.
- ئەم فەرمانانە بە کۆتەیشنی دروست بنووسیت: CREATE DATABASE، CREATE TABLE، INSERT، SELECT، UPDATE و DELETE.
- خۆت لە هەڵە باوەکان بپارێزیت: UPDATE یان DELETE بەبێ WHERE، کۆتەیشنی چەماوە و هەڵە تێگەیشتن لە ORDER BY ... ASC.
Why websites need databasesبۆچی تۆڕگەکان پێویستیان بە بنکەدراوە هەیە
Up to now, a PHP page could only show what you wrote into it, or what a visitor sent a moment ago. When the ends, all its disappear. Refresh the page and everything starts again from zero.
تا ئێستا PHP تەنها دەیتوانی ئەوە پیشان بدات کە خۆت تێیدا نووسیبووت، یان ئەوەی سەردانکەرێک چرکەیەک پێش ئێستا ناردبووی. کاتێک تەواو دەبێت، هەموو لە ناو دەچن. ئەگەر لاپەڕەکە نوێ بکەیتەوە، هەموو شتێک لە سفرەوە دەست پێ دەکاتەوە.
Real websites must remember things: user accounts, products and prices, students and their marks, messages. They keep this data in a . The data stays there after the script ends, after the server restarts, and tomorrow. This is called .
ڕاستەقینە دەبێت شتەکانی لەبیر بێت: ، کاڵا و نرخەکانیان، خوێندکاران و نمرەکانیان، نامەکان. ئەم زانیارییانە لە هەڵدەگرن. زانیارییەکان دوای تەواوبوونی ، دوای دووبارە هەڵکردنەوەی و سبەینێش هەر دەمێننەوە. بەمە دەوترێت .
An organized collection of data, stored so that a program can quickly find, add, change and delete it. Websites keep their users, products and messages in databases.
کۆمەڵەیەکی ڕێکخراوی دراوەیە کە بە شێوەیەک خەزن کراوە بەرنامەیەک بتوانێت بە خێرایی بیدۆزێتەوە، زیادی بکات، بیگۆڕێت و بیسڕێتەوە. تۆڕگەکان زانیاریی بەکارهێنەر و کاڵا و نامەکانیان تێدا خەزن دەکەن.
A is like writing on the classroom whiteboard: it is wiped when the lesson (the script) ends. A is like the register book in the school office: what you write stays, and any teacher can look it up tomorrow.
وەک نووسین وایە لەسەر تەختەی پۆل: کاتێک وانەکە () تەواو دەبێت دەسڕدرێتەوە. وەک دەفتەری ناونووسینی ئیدارەی قوتابخانە وایە: ئەوەی تێیدا دەینووسیت دەمێنێتەوە و هەر مامۆستایەک دەتوانێت سبەینێ سەیری بکات.
Where the analogy stops workingسنووری ئەم نموونەیە
People read a register book slowly, one at a time. A database is read by programs: it can search millions of rows in a moment, and many visitors can use it at the same time.
خەڵک دەفتەری ئیدارە بە هێواشی و یەک بە یەک دەخوێننەوە. بەڵام بەرنامەکان دەیخوێننەوە: دەتوانێت لە چرکەیەکدا ملیۆنان بپشکنێت و زۆر سەردانکەر لە یەک کاتدا بەکاری بهێنن.
What this chapter covers
ئەم بەشە چی لەخۆ دەگرێت
- SQL basics (this lecture): tables, data types and the first statements.
- Creating a database step by step with XAMPP and phpMyAdmin, screen by screen: phpMyAdmin.
- Connecting from PHP, "opening the door to your data": Connecting PHP to MySQL.
- Inserting a record and a form that saves what the visitor types, and reading rows back: Insert and select.
- Keeping the database safe, a complete CRUD application, and SQLite — "a whole database in one file", with five reasons it fits a course like this: Database security, CRUD application, SQLite.
- بنەماکانی (ئەم وانەیە): ، و یەکەم فەرمانەکان.
- دروستکردنی هەنگاو بە هەنگاو بە XAMPP و phpMyAdmin، شاشە بە شاشە: phpMyAdmin.
- پەیوەندیکردن لە PHPـەوە، «کردنەوەی دەرگای زانیارییەکانت»: پەیوەندیکردنی PHP بە MySQL.
- زیادکردنی و کە ئەوەی سەردانکەر دەینووسێت هەڵیدەگرێت و خوێندنەوەی : زیادکردن و هەڵبژاردن.
- پاراستنی ، بەرنامەیەکی تەواوی و — « کە هەمووی لە یەک فایلدایە»، لەگەڵ پێنج هۆکار کە بۆچی بۆ کۆرسێکی وەک ئەمە گونجاوە: ئاسایشی بنکەدراوە، بەرنامەی CRUD، SQLite.
In Kurdish: what a database is, what parts it has, and why programs and websites use databases to keep their information.
بە کوردی: چییە، چ بەشێکی هەیە و بۆچی و بەکاردەهێنن بۆ هەڵگرتنی .
Whole video, in Central Kurdish (Sorani).
هەموو ڤیدیۆکە، بە کوردیی ناوەندی (سۆرانی).
Database, table, row and columnبنکەدراوە، خشتە، تۆمار و ستوون
A is managed by a program called a (database management system). The DBMS stores the data on disk, keeps it safe, and answers your questions about it. , and are DBMSs.
بەرنامەیەک بەڕێوەی دەبات کە پێی دەوترێت . ئەم سیستەمە زانیارییەکان لەسەر دیسک هەڵدەگرێت، دەیانپارێزێت و وەڵامی پرسیارەکانت دەربارەیان دەداتەوە. ، و نموونەی ئەم سیستەمانەن.
MySQL is a system: it keeps data in tables of rows and columns. A relational database looks very much like a spreadsheet file:
MySQL سیستەمێکی : زانیارییەکان لە ڕیز و هەڵدەگرێت. زۆر لە فایلێکی ئێکسڵ دەچێت:
- A is the whole file, for example
school. One MySQL server can hold many databases. - A is one sheet inside it. Each table holds one kind of thing:
students,courses,users. - A (also called a field) is one piece of information that every row has, such as
nameorcity. Each column has a name and a . - A (also called a record) is all the data about one thing: one student, one order.
- A is a column whose value is different in every row, usually
id, so each row can be found by its own number.
- هەموو فایلەکەیە، بۆ نموونە
school. یەک MySQL دەتوانێت چەندین هەڵبگرێت. - یەک پەڕەیە لە ناویدا. هەر یەک جۆر شت هەڵدەگرێت:
students،courses،users. - یەک پارچە زانیارییە کە هەموو هەیەتی، وەک
nameیانcity. هەر ناوێک و هەیە. - هەموو زانیارییەکانی یەک شتە: یەک خوێندکار، یەک داواکاری.
- کە نرخەکەی لە هەموو جیاوازە، زۆرجار
id، بۆ ئەوەی هەر بە ژمارەی تایبەتی خۆی بدۆزرێتەوە.
Think of each table as a printed form. The columns are the questions on the form (name, city, marks). Every row is one filled-in form: it answers all of the questions.
هەر وەک چاپکراو وێنا بکە. پرسیارەکانی سەر فۆرمەکەن (ناو، شار، نمرە). هەر پڕکراوەیە: وەڵامی هەموو پرسیارەکان دەداتەوە.
Where the analogy stops workingسنووری ئەم نموونەیە
In a spreadsheet you can type anything into any cell. A database table is stricter: every column has a fixed data type, so the database refuses a word where a number is expected.
لە ئێکسڵدا دەتوانیت هەر شتێک لە هەر خانەیەکدا بنووسیت. بەڵام توندترە: هەر جۆرێکی جێگیری هەیە، بۆیە ڕەتی دەکاتەوە ئەگەر لە جیاتی ژمارە وشەیەک بنووسیت.
Database, table, row and columnبنکەدراوە، خشتە، تۆمار و ستوون
Three kinds of tableسێ جۆر خشتە
The word table is used for three different things, and they are not the same:
وشەی خشتە بۆ سێ شتی جیاواز بەکار دێت و ئەوانە وەک یەک نین:
| What gives a value its meaning?چی واتا دەداتە ؟ | What may a cell hold? چی دەتوانێت هەڵبگرێت؟ | What is it for?بۆ چییە؟ | |
|---|---|---|---|
| Database table | The column it is in: every column has a fixed name and , and every row has the same columnsئەو تێیدایە: هەر ناو و جێگیری هەیە و هەموو هەمان هەیە | Only a value of the column's type that obeys its rules (NOT NULL, UNIQUE …)تەنها لە جۆری کە ڕێساکانی ڕەچاو بکات (NOT NULL، UNIQUE …) | Storing data that programs can search, link and protectهەڵگرتنی بۆ ئەوەی بتوانن بیگەڕێن، پێکەوەی ببەستن و بیپارێزن |
| Only its position (cell C7) and any label that someone typed near itتەنها شوێنەکەی ( C7) و هەر ناونیشانێک کە کەسێک لە نزیکیەوە نووسیبێتی | Anything: a number, a word or a , mixed in one columnهەر شتێک: ژمارە، وشە یان ، تێکەڵ لە یەک ستووندا | Calculating, and quick lists for one personژماردن و لیستی خێرا بۆ یەک کەس | |
| HTML | Nothing: <td> only says show this in a cellهیچ: <td> تەنها دەڵێت ئەمە لە خانەیەکدا پیشان بدە | Text to displayدەق بۆ پیشاندان | Showing data on a پیشاندانی لەسەر |
The database's strict rule, every row has the same columns and each column has one fixed type, can feel limiting. But it is exactly what lets the check every new row and search millions of rows fast.
یاسا توندەکەی ، هەموو هەمان هەیە و هەر یەک جۆری جێگیری هەیە، لەوانەیە وەک بەربەست دیار بێت. بەڵام ڕێک ئەوەیە کە ڕێگە بە دەدات هەموو نوێ بپشکنێت و بە خێرایی لەناو ملیۆنان بگەڕێت.
Why PHP and MySQL work togetherبۆچی PHP و MySQL پێکەوە کار دەکەن
is a database server: a program that runs in the background and waits for requests, much like Apache waits for browsers. In XAMPP you start it with the Start button next to MySQL in the . Your PHP script sends it SQL, and it sends back the results.
: بەرنامەیەکە لە پشتەوە کار دەکات و چاوەڕێی داواکاری دەکات، هەروەک چۆن Apache چاوەڕێی دەکات. لە XAMPPدا لە بە دوگمەی Startـی تەنیشت MySQL هەڵی دەکەیت. PHPـەکەت بۆ دەنێرێت و ئەویش ئەنجامەکان دەگەڕێنێتەوە.
The course gives four reasons why PHP is so often used with MySQL:
وانەکە چوار هۆکار دەهێنێتەوە بۆ ئەوەی بۆچی PHP زۆرجار لەگەڵ MySQL بەکار دەهێنرێت:
- Both are . You can develop on Windows and serve the finished site on a Unix (Linux) server without changing your code.
- MySQL is the most popular database system used with PHP. Most tutorials, hosting companies and PHP applications support it.
- MySQL is ideal for both small and large applications, from a class project to sites with millions of users.
- MySQL is free to download and use. The free version is the open-source Community Edition; the company Oracle also sells paid editions with extra support.
- هەردووکیان . دەتوانیت لەسەر Windows گەشەی پێ بدەیت و تەواوکراوەکە لەسەر Unix (Linux) دابنێیت، بەبێ گۆڕینی کۆدەکەت.
- MySQL بەناوبانگترین سیستەمی کە لەگەڵ PHP بەکار دێت. زۆربەی فێرکارییەکان، کۆمپانیاکانی هۆستینگ و بەرنامەکانی PHP پشتگیری دەکەن.
- MySQL بۆ بەرنامەی بچووک و گەورەش گونجاوە، لە پرۆژەیەکی پۆلەوە تا خاوەن ملیۆنان بەکارهێنەر.
- MySQL بەخۆڕایی دادەبەزێنرێت و بەکار دەهێنرێت. وەشانە بەخۆڕاییەکە Community Editionـە کە ؛ کۆمپانیای Oracle وەشانی پارەداریش دەفرۆشێت کە پشتگیریی زیاتری هەیە.
SQL: the language of databasesSQL: زمانی بنکەدراوەکان
You talk to MySQL in (Structured Query Language). The course's Kurdish notes call it «زمانی پرسگەلی ڕێکخراو». One SQL command is called a or a . With SQL you create databases and tables, and you add, read, change and delete data.
بە (Structured Query Language) قسە لەگەڵ MySQL دەکەیت. تێبینییە کوردییەکانی وانەکە پێی دەڵێن «زمانی پرسگەلی ڕێکخراو». هەر فەرمانێکی SQL پێی دەوترێت یان . بە SQL و دروست دەکەیت و زانیاری زیاد دەکەیت، دەیخوێنیتەوە، دەیگۆڕیت و دەیسڕیتەوە.
Writing rules
یاساکانی نووسین
- SQL such as
SELECT,FROMandWHEREare not case-sensitive, but we write them in CAPITALS so they stand out from table and column names. - End each statement with a semicolon
;. In phpMyAdmin you can run several statements at once when each ends with;. - Put text values in straight single quotes:
'Erbil'. Write numbers without quotes:85. - MySQL lets you put table and column names in backticks:
`stage_4`. This tells MySQL that the word is a name, not a keyword. phpMyAdmin writes them this way. - Comments start with
--(two dashes and a space).
- SQL وەک
SELECT،FROMوWHEREهەستیار نین بە گەورە و بچووکیی پیت، بەڵام بە پیتی گەورە دەیاننووسین بۆ ئەوەی لە ناوی و جیا بکرێنەوە. - هەر فەرمانێک بە
;کۆتایی پێ بهێنە. لە phpMyAdminدا دەتوانیت چەند فەرمانێک پێکەوە جێبەجێ بکەیت ئەگەر هەر یەکەیان بە;تەواو بێت. - دەق لە ناو کۆتەیشنی تاکی ڕاستدا دابنێ:
'Erbil'. ژمارە بەبێ کۆتەیشن بنووسە:85. - MySQL ڕێگە دەدات ناوی و لە ناو باکتیک (
`) دابنێیت:`stage_4`. ئەمە بە MySQL دەڵێت ئەو وشەیە ناوە، نەک . phpMyAdmin بەم شێوەیە دەیاننووسێت. - بە
--دەست پێ دەکات (دوو داش و بۆشاییەک).
Four statements for four jobs (CRUD)
چوار فەرمان بۆ چوار کار (CRUD)
Almost every web application does four things with its data. Together they are called : Create, Read, Update, Delete. Each one has its own SQL statement:
نزیکەی هەموو چوار کار بە زانیارییەکانی دەکات. پێکەوە پێیان دەوترێت : Create (دروستکردن)، Read (خوێندنەوە)، Update (نوێکردنەوە)، Delete (سڕینەوە). هەر یەکەیان فەرمانی SQLـی تایبەتی خۆی هەیە:
| CRUD | SQL statementفەرمانی SQL | What it doesچی دەکات |
|---|---|---|
| CreateCreate (دروستکردن) | INSERT | adds a new row نوێ زیاد دەکات |
| ReadRead (خوێندنەوە) | SELECT | reads rows دەخوێنێتەوە |
| UpdateUpdate (نوێکردنەوە) | UPDATE | changes rows that already existئەو دەگۆڕێت کە هەن |
| DeleteDelete (سڕینەوە) | DELETE | removes rows دەسڕێتەوە |
Before you can use these four, the database and its tables must exist. That is the job of CREATE, explained after the data types. A full CRUD application is built in PHP CRUD application.
پێش ئەوەی ئەم چوارە بەکار بهێنیت، دەبێت و هەبن. ئەوە کاری CREATEـە، کە دوای ڕوون دەکرێتەوە. بەرنامەیەکی تەواوی CRUD لە بەرنامەی CRUD بە PHP دروست دەکرێت.
MySQL data typesجۆرەکانی دراوەی SQL لە MySQLدا
Every column has a . The type says what may be stored there (whole numbers, text, dates) and how much space it takes. Choosing the right type keeps wrong data out and makes the table smaller and faster.
هەر هەیە. جۆرەکە دەڵێت چی دەکرێت لەوێ هەڵبگیرێت (، دەق، بەروار) و چەند شوێن دەگرێت. هەڵبژاردنی جۆری دروست ڕێگە لە زانیاریی هەڵە دەگرێت و بچووکتر و خێراتر دەکات.
The kind of value a database column can hold, such as INT for whole numbers, VARCHAR(n) for short text, TEXT for long text, DATE and TIMESTAMP for time, and BLOB for binary data.
ئەو جۆرە نرخەیە کە ستوونێکی دەتوانێت هەڵیبگرێت، وەک INT بۆ ژمارەی تەواو، VARCHAR(n) بۆ دەقی کورت، TEXT بۆ دەقی درێژ، DATE و TIMESTAMP بۆ کات و BLOB بۆ دراوەی دووانەیی.
MySQL data typesجۆرەکانی دراوەی SQL لە MySQLدا
Numeric typesجۆرە ژمارەییەکان
MySQL supports all the standard SQL numeric data types. They come in two groups:
MySQL پشتگیریی هەموو جۆرە ژمارەییە ستانداردەکانی SQL دەکات. دوو گرووپن:
- Exact types store the number exactly:
INTEGER(INT),SMALLINT,DECIMALandNUMERIC. - Approximate types store a very close value, which can be a tiny bit wrong:
FLOAT,REALandDOUBLE PRECISION(DOUBLE).
- جۆرە وردەکان ژمارەکە بە تەواوی وەک خۆی هەڵدەگرن:
INTEGER(INT)،SMALLINT،DECIMALوNUMERIC. - جۆرە نزیکەییەکان نرخێکی زۆر نزیک هەڵدەگرن کە لەوانەیە کەمێک جیاواز بێت:
FLOAT،REALوDOUBLE PRECISION(DOUBLE).
| Typeجۆر | Storageقەبارە | Signed rangeمەودا (بە نیشانە) | UNSIGNED rangeمەودا (UNSIGNED) | Good forگونجاو بۆ |
|---|---|---|---|---|
TINYINT | 1 byte1 بایت | −128 … 127 | 0 … 255 | age, yes/no (0 or 1)تەمەن، بەڵێ/نەخێر (0 یان 1) |
SMALLINT | 2 bytes2 بایت | −32,768 … 32,767 | 0 … 65,535 | a year, small countsساڵ، ژمارەی بچووک |
MEDIUMINT | 3 bytes3 بایت | −8,388,608 … 8,388,607 | 0 … 16,777,215 | medium countsژمارەی مامناوەند |
INT | 4 bytes4 بایت | −2,147,483,648 … 2,147,483,647 | 0 … 4,294,967,295 | id columns, most whole numbers id، زۆربەی |
BIGINT | 8 bytes8 بایت | −2⁶³ … 2⁶³−1 | 0 … 2⁶⁴−1 | very large numbersژمارەی زۆر گەورە |
DECIMAL(p,s) / NUMERIC | depends on the digitsبەپێی ژمارەی ڕەقەمەکان | exactورد | — | money: DECIMAL(10,2)پارە: DECIMAL(10,2) |
FLOAT | 4 bytes4 بایت | approximateنزیکەیی | — | measurementsپێوانەکان |
DOUBLE / DOUBLE PRECISION / REAL | 8 bytes8 بایت | approximate, more preciseنزیکەیی، وردتر | — | scientific valuesنرخە زانستییەکان |
Any numeric type can be declared UNSIGNED: it then cannot hold negative numbers. For the whole-number types this also doubles the positive range. For example, age TINYINT UNSIGNED holds 0 to 255 instead of −128 to 127.
هەر جۆرێکی ژمارەیی دەکرێت بە UNSIGNED ڕابگەیەنرێت: ئەو کاتە ناتوانێت هەڵبگرێت. لە جۆرەکانی مەودا موجەبەکەش دوو هێندە دەبێت. بۆ نموونە، age TINYINT UNSIGNED لە جیاتی −128 تا 127، لە 0 تا 255 هەڵدەگرێت.
MySQL data typesجۆرەکانی دراوەی SQL لە MySQLدا
Text typesجۆرە دەقییەکان
The slides list only numbers and dates, but almost every table also stores text. These are the text types you will use in this course:
سلایدەکان تەنها ژمارە و بەروار ڕیز دەکەن، بەڵام نزیکەی هەموو دەقیش هەڵدەگرێت. ئەمانە ئەو جۆرە دەقییانەن کە لەم کۆرسەدا بەکاریان دەهێنیت:
| Typeجۆر | Holdsچی هەڵدەگرێت | Example columnنموونەی |
|---|---|---|
VARCHAR(n) | text of up to n characters; the most common typeدەقی تا n پیت؛ باوترین جۆر | name VARCHAR(50) |
CHAR(n) | text of exactly n characters (shorter text is padded)دەقی ڕێک n پیت (دەقی کورتتر پڕ دەکرێتەوە) | country_code CHAR(2) |
TEXT | long text, up to 65,535 bytesدەقی درێژ، تا 65,535 بایت | comment TEXT |
BLOB | binary data such as an image file دووانەیی وەک فایلی وێنە | photo BLOB |
MySQL data typesجۆرەکانی دراوەی SQL لە MySQLدا
Date and time typesجۆرەکانی بەروار و کات
The types for dates and times are DATETIME, DATE, TIMESTAMP, TIME and YEAR. MySQL always writes dates year first: '2026-10-16'.
جۆرەکانی بەروار و کات بریتین لە DATETIME، DATE، TIMESTAMP، TIME و YEAR. MySQL هەمیشە بەروار بە ساڵ لە پێشەوە دەنووسێت: '2026-10-16'.
| Typeجۆر | Formatشێوە | Exampleنموونە | Rangeمەودا | "Zero" value on the slideنرخی «سفر» لە سلایدەکەدا |
|---|---|---|---|---|
DATETIME | YYYY-MM-DD hh:mm:ss | '2026-10-16 14:30:00' | 1000-01-01 00:00:00 … 9999-12-31 23:59:59 | '0000-00-00 00:00:00' |
DATE | YYYY-MM-DD | '2026-10-16' | 1000-01-01 … 9999-12-31 | '0000-00-00' |
TIMESTAMP | YYYY-MM-DD hh:mm:ss | '2026-10-16 14:30:00' | 1970-01-01 00:00:01 UTC … 2038-01-19 03:14:07 UTC | '0000-00-00 00:00:00' |
TIME | hh:mm:ss | '14:30:00' | −838:59:59 … 838:59:59 | '00:00:00' |
YEAR | YYYY | 2026 | 1901 … 2155 | 0000 |
TIMESTAMP or DATETIME?TIMESTAMP یان DATETIME؟
Both look the same (2026-10-16 14:30:00), but they behave differently when the time zone changes. A is a moment in world time; a DATETIME is just a date and a clock time written down.
هەردووکیان وەک یەک دەردەکەون (2026-10-16 14:30:00)، بەڵام کاتێک دەگۆڕێت جیاواز ڕەفتار دەکەن. ساتێکە لە کاتی جیهانیدا؛ DATETIME تەنها بەروار و کاتژمێرێکی نووسراوە.
TIMESTAMP | DATETIME | |
|---|---|---|
| Time-zone conversionگۆڕینی | Converted from the current time zone to UTC when stored, and back to the current time zone when read.لە کاتی هەڵگرتندا لە ئێستاوە دەگۆڕدرێت بۆ UTC و لە کاتی خوێندنەوەدا دەگەڕێنرێتەوە بۆ ئێستا. | No conversion: stored exactly as given.هیچ گۆڕینێک نییە: وەک خۆی هەڵدەگیرێت. |
| When the time-zone setting changesکاتێک ڕێکخستنی دەگۆڕێت | The value you see changes.ئەو نرخەی دەیبینیت دەگۆڕێت. | Stays the same.وەک خۆی دەمێنێتەوە. |
| Rangeمەودا | 1970 … 2038 (the "year 2038" limit)1970 … 2038 (سنووری «ساڵی 2038») | 1000 … 9999 |
| Can it be indexed?دەتوانرێت بۆ دابنرێت؟ | Yesبەڵێ | Yes (the slide says no; that is wrong)بەڵێ (سلایدەکە دەڵێت نەخێر؛ ئەوە هەڵەیە) |
| Typical useبەکارهێنانی باو | when a row was created or changed: DEFAULT CURRENT_TIMESTAMPکاتی دروستبوون یان گۆڕانی : DEFAULT CURRENT_TIMESTAMP | birthdays, exam dates, appointments, dates after 2038ڕۆژی لەدایکبوون، بەرواری تاقیکردنەوە، ژوانەکان، بەرواری دوای 2038 |
CREATE TABLE times (ts TIMESTAMP, dt DATETIME);
SET time_zone = '+00:00'; -- the connection works in UTC
INSERT INTO times VALUES ('2026-10-16 12:00:00', '2026-10-16 12:00:00');
SET time_zone = '+03:00'; -- now read it with Erbil time (UTC+3)
SELECT ts, dt FROM times;+---------------------+---------------------+ | ts | dt | +---------------------+---------------------+ | 2026-10-16 15:00:00 | 2026-10-16 12:00:00 | +---------------------+---------------------+
What MySQL shows (MySQL rules for TIMESTAMP; this example could not be run in our sandbox).
ئەوەی MySQL پیشانی دەدات (بەپێی یاساکانی MySQL بۆ TIMESTAMP؛ ئەم نموونەیە لە ژینگەی تاقیکردنەوەماندا جێبەجێ نەکرا).
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 3-4
Both columns receive the same text while the connection is in UTC.
هەردوو هەمان نرخ وەردەگرن کاتێک پەیوەندییەکە بە UTC کار دەکات.
- Lineدێڕی 6-7
After changing the time zone,
tsis shown 3 hours later;dtdoes not move.دوای گۆڕینی ،
tsبە 3 کاتژمێر دواتر پیشان دەدرێت؛dtناجووڵێت.
A TIMESTAMP is like a flight time kept in world time (UTC): each airport shows it in its own local time. A DATETIME is like a time written in your notebook: it shows the same numbers wherever you open it.
TIMESTAMP وەک کاتی گەشتێکی فڕۆکە وایە کە بە کاتی جیهانی (UTC) هەڵگیراوە: هەر فڕۆکەخانەیەک بە کاتی ناوخۆیی خۆی پیشانی دەدات. DATETIME وەک کاتێک وایە کە لە دەفتەرەکەتدا نووسیبێتت: لە هەر شوێنێک بیکەیتەوە هەمان ژمارە پیشان دەدات.
Where the analogy stops workingسنووری ئەم نموونەیە
MySQL converts a TIMESTAMP using the time zone of the connection (often the server's), not the visitor's browser.
MySQL بۆ گۆڕینی TIMESTAMP پەیوەندییەکە بەکار دەهێنێت (زۆرجار هی )، نەک هی سەردانکەر.
Creating a database and a tableدروستکردنی بنکەدراوە و خشتە
Three statements build the empty "container" for your data:
سێ فەرمان «قاپە» بەتاڵەکە بۆ زانیارییەکانت دروست دەکەن:
CREATE DATABASE name;creates a new, empty database.USE name;selects it, so the next statements work inside it.CREATE TABLE name ( ... );creates a table and lists its columns, each with a type and rules.
CREATE DATABASE name;نوێی بەتاڵ دروست دەکات.USE name;هەڵیدەبژێرێت، بۆ ئەوەی فەرمانەکانی دواتر لە ناویدا کار بکەن.CREATE TABLE name ( ... );دروست دەکات و ڕیز دەکات، هەر یەکەیان لەگەڵ جۆر و یاساکانی.
CREATE DATABASE widget_corp;
USE widget_corp;
CREATE TABLE subjects (
id int(11) NOT NULL auto_increment,
menu_name varchar(30) NOT NULL,
position int(3) NOT NULL,
visible tinyint(1) NOT NULL,
PRIMARY KEY (id)
);Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
Create a database called
widget_corp.بە ناوی
widget_corpدروست بکە.- Lineدێڕی 2
Work inside
widget_corpfrom now on.لێرەوە لە ناو
widget_corpدا کار بکە.- Lineدێڕی 3
Create the table
subjects(the menu subjects of a website).subjectsدروست بکە (بابەتەکانی لیستی ).- Lineدێڕی 4
id: a whole number, required (NOT NULL), andauto_incrementgives each new row the next number (1, 2, 3, ...).id: ، پێویست (NOT NULL) وauto_incrementژمارەی دواتر (1، 2، 3، ...) دەدات بە هەر نوێ.- Lineدێڕی 5
menu_name: text of up to 30 characters, required.menu_name: دەقی تا 30 پیت، پێویست.- Lineدێڕی 6
position: a whole number that orders the menu.position: کە ڕیزبەندیی لیستەکە دیاری دەکات.- Lineدێڕی 7
visible: 0 or 1, used as no/yes.visible: 0 یان 1، وەک نەخێر/بەڵێ بەکار دێت.- Lineدێڕی 8
idis the : no two rows may have the sameid.id: هیچ دوو ناتوانن هەمانidیان هەبێت.
The course summary uses the same pattern with general names: CREATE DATABASE db; USE db; and a table Table_Name with the columns id, name varchar(30), age int(3) and visible tinyint(1), again with PRIMARY KEY (id).
پوختەی وانەکان هەمان شێوە بە ناوی گشتی بەکار دەهێنێت: CREATE DATABASE db; USE db; و Table_Name بە id، name varchar(30)، age int(3) و visible tinyint(1)، دیسان لەگەڵ PRIMARY KEY (id).
Short MySQL chapters: create a database and a table with data types, then add, read, change and delete rows with INSERT, SELECT, UPDATE and DELETE.
چەند بەشێکی کورتی : دروستکردنی و لەگەڵ ، پاشان زیادکردن، خوێندنەوە، گۆڕین و سڕینەوەی بە INSERT، SELECT، UPDATE و DELETE.
Watch 10:29–22:38: CREATE DATABASE and CREATE TABLE.
لە 10:29 تا 22:38 سەیر بکە: CREATE DATABASE و CREATE TABLE.
Creating a database and a tableدروستکردنی بنکەدراوە و خشتە
Column rules, and what INT(11) meansیاساکانی ستوون و واتای INT(11)
| Ruleیاسا | Meaningواتا |
|---|---|
NOT NULL | The column must always have a value; it may not be empty (NULL). دەبێت هەمیشە نرخێکی هەبێت؛ نابێت بەتاڵ (NULL) بێت. |
AUTO_INCREMENT | MySQL fills in the next number by itself. Use it on the id column. See .MySQL خۆی ژمارەی دواتر دادەنێت. لەسەر id بەکاری بهێنە. بڕوانە . |
PRIMARY KEY | The column that identifies each row; its values are unique and never empty.ئەو هەر دەناسێنێت؛ نرخەکانی دووبارە نابنەوە و هەرگیز بەتاڵ نین. |
UNIQUE | No two rows may have the same value (useful for email).هیچ دوو ناتوانن هەمان نرخیان هەبێت (بۆ email بەسوودە). |
DEFAULT value | The value used when an INSERT does not give one.ئەو نرخەی بەکار دێت کاتێک INSERT نرخێک نادات. |
UNSIGNED | No negative numbers. نا. |
CHECK (condition) | Every row must make the condition true, for example CHECK (marks <= 100). MySQL enforces it from version 8.0.16 (older versions read it and ignored it); MariaDB in XAMPP enforces it too. More rules like this: integrity rules. دەبێت بۆ هەموو ڕاست بێت، بۆ نموونە CHECK (marks <= 100). MySQL لە وەشانی 8.0.16ـەوە جێبەجێی دەکات (وەشانە کۆنەکان دەیانخوێندەوە و پشتگوێیان دەخست)؛ MariaDBـی ناو XAMPPـیش جێبەجێی دەکات. ڕێسای زیاتری لەم جۆرە: ڕێساکانی دروستی. |
Creating a database and a tableدروستکردنی بنکەدراوە و خشتە
The same table in modern styleهەمان خشتە بە شێوازی نوێ
CREATE DATABASE school CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
USE school;
CREATE TABLE students (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50) NOT NULL,
city VARCHAR(30) NOT NULL,
marks TINYINT UNSIGNED NOT NULL
);Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
utf8mb4is the that can store every language, Kurdish included (and emoji). Always use it.utf8mb4ئەو کە دەتوانێت هەموو زمانێک هەڵبگرێت، کوردیش (و ئیمۆجی). هەمیشە بەکاری بهێنە.- Lineدێڕی 4
All the rules for
idon one line: positive whole number, required, automatic numbering, primary key.هەموو یاساکانی
idلە یەک دێڕدا: موجەب، پێویست، ، .- Lineدێڕی 7
Marks are 0–100, so the 1-byte
TINYINT UNSIGNED(0–255) is enough.نمرەکان 0–100ن، بۆیە
TINYINT UNSIGNEDی 1 بایتی (0–255) بەسە.
The rest of this page uses this students table. In the next lecture you will create tables with phpMyAdmin's forms instead of typing SQL; phpMyAdmin writes the same CREATE TABLE statement for you.
بەشەکانی تری ئەم لاپەڕەیە ئەم studentsـە بەکار دەهێنن. لە وانەی داهاتوودا لە جیاتی نووسینی SQL، بە phpMyAdmin دروست دەکەیت؛ phpMyAdmin هەمان فەرمانی CREATE TABLE بۆت دەنووسێت.
Creating a database and a tableدروستکردنی بنکەدراوە و خشتە
Looking around: SHOW and DESCRIBEسەیرکردنی ئەوەی هەیە: SHOW و DESCRIBE
Three statements show what is already there. SHOW DATABASES; lists the databases on the server. SHOW TABLES; lists the tables of the database you chose with USE. DESCRIBE students; (short: DESC students;) shows the structure of one table: each column with its type, whether NULL is allowed, its key, its default value and extras such as auto_increment.
سێ پیشانی دەدەن چی هەیە. SHOW DATABASES; سەر لیست دەکات. SHOW TABLES; ئەو لیست دەکات کە بە USE هەڵتبژاردووە. DESCRIBE students; (بە کورتی DESC students;) پێکهاتەی یەک پیشان دەدات: هەر لەگەڵ جۆرەکەی، ئایا NULL ڕێگەپێدراوە، ، و زیادەکانی وەک auto_increment.
SHOW TABLES;
DESCRIBE students;+------------------+ | Tables_in_school | +------------------+ | students | +------------------+ +-------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | name | varchar(50) | NO | | NULL | | | city | varchar(30) | NO | | NULL | | | marks | tinyint(3) unsigned | NO | | NULL | | +-------+---------------------+------+-----+---------+----------------+
Real output from MariaDB 10.4, the database server in XAMPP.
ڕاستەقینە لە MariaDB 10.4، ناو XAMPP.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
MariaDB still shows the old display widths,
int(10)andtinyint(3). MySQL 8 writes the same types without them:int unsigned,tinyint unsigned(what INT(11) means).MariaDB هێشتا پانیی پیشاندانە کۆنەکان پیشان دەدات،
int(10)وtinyint(3). MySQL 8 هەمان جۆرەکان بەبێ ئەوان دەنووسێت:int unsigned،tinyint unsigned(INT(11)واتای چییە).
On XAMPP, SHOW DATABASES; also lists the server's own databases (information_schema, mysql, performance_schema) and the phpmyadmin and test databases next to yours. These are MySQL and MariaDB statements; SQLite uses .tables and PRAGMA table_info(students); instead (SQLite's DDL).
لە XAMPPـدا SHOW DATABASES; خودی (information_schema، mysql، performance_schema) و phpmyadmin و testیش لە تەنیشت هی خۆتەوە لیست دەکات. ئەمانە MySQL و MariaDBـن؛ SQLite لە جیاتییان .tables و PRAGMA table_info(students); بەکار دەهێنێت (DDLـی SQLite).
Creating a database and a tableدروستکردنی بنکەدراوە و خشتە
Changing a table later: ALTER TABLEگۆڕینی خشتەیەک دواتر: ALTER TABLE
Designs change: the school now wants each student's phone number. You do not delete the table and lose its rows. ALTER TABLE changes the structure and keeps the data:
دیزاین دەگۆڕێت: قوتابخانەکە ئێستا ژمارەی تەلەفۆنی هەر خوێندکارێکی دەوێت. ناسڕیتەوە و لەدەست نادەیت. ALTER TABLE پێکهاتەکە دەگۆڕێت و دەپارێزێت:
ALTER TABLE students ADD COLUMN phone VARCHAR(15);
ALTER TABLE students ADD UNIQUE (phone);
ALTER TABLE students MODIFY city VARCHAR(40) NOT NULL DEFAULT 'Erbil';
DESCRIBE students;
ALTER TABLE students DROP COLUMN phone; -- and remove it again+-------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+---------------------+------+-----+---------+----------------+ | id | int(10) unsigned | NO | PRI | NULL | auto_increment | | name | varchar(50) | NO | | NULL | | | city | varchar(40) | NO | | Erbil | | | marks | tinyint(3) unsigned | NO | | NULL | | | phone | varchar(15) | YES | UNI | NULL | | +-------+---------------------+------+-----+---------+----------------+
Real output of DESCRIBE (line 4), MariaDB 10.4. After line 6 the table is as before, but city keeps its new length and default.
ڕاستەقینەی DESCRIBE (دێڕی 4)، MariaDB 10.4. دوای دێڕی 6 وەک پێشووی لێدێتەوە، بەڵام city درێژی و نوێیەکەی دەمێنێت.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
A new column at the end. The rows that already exist get
NULLin it.نوێ لە کۆتاییدا. ئەو هەن
NULLتێیدا وەردەگرن.- Lineدێڕی 2
No two students may have the same phone number (several
NULLs are allowed).هیچ دوو خوێندکارێک ناتوانن هەمان ژمارەی تەلەفۆنیان هەبێت (چەند
NULLێک ڕێگەپێدراوە).- Lineدێڕی 3
MODIFYrepeats the whole column definition: the new type and every rule you want to keep.MODIFYهەموو پێناسەی دووبارە دەکاتەوە: جۆرە نوێیەکە و هەموو ڕێسایەک کە دەتەوێت بیهێڵیتەوە.- Lineدێڕی 6
DROP COLUMNdeletes the column with all its values. There is no undo.DROP COLUMNلەگەڵ هەموو دەسڕێتەوە. گەڕانەوەی نییە.
The four basic statementsچوار فەرمانە سەرەکییەکە
All examples below run on the students table from the previous section. Each statement is followed by the result that the database gives back.
هەموو نموونەکانی خوارەوە لەسەر studentsی بەشی پێشوو کار دەکەن. دوای هەر فەرمانێک ئەو ئەنجامە دێت کە دەیگەڕێنێتەوە.
Short MySQL chapters: create a database and a table with data types, then add, read, change and delete rows with INSERT, SELECT, UPDATE and DELETE.
چەند بەشێکی کورتی : دروستکردنی و لەگەڵ ، پاشان زیادکردن، خوێندنەوە، گۆڕین و سڕینەوەی بە INSERT، SELECT، UPDATE و DELETE.
Watch 22:38–37:03: INSERT, SELECT, UPDATE and DELETE.
لە 22:38 تا 37:03 سەیر بکە: INSERT، SELECT، UPDATE و DELETE.
The four basic statementsچوار فەرمانە سەرەکییەکە
INSERT: add rows (Create)INSERT: زیادکردنی تۆمار (Create)
INSERT INTO table_name (column1, column2, column3)
VALUES (value1, value2, value3);Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
Name the table, then list the columns you will fill.
ناوی بنووسە، پاشان ئەو ڕیز بکە کە پڕیان دەکەیتەوە.
- Lineدێڕی 2
Give the values in the same order as the columns.
نرخەکان بە هەمان ڕیزبەندیی بنووسە.
We leave out id: AUTO_INCREMENT fills it in. Text values are in single quotes; numbers are not. You can add one row, or several rows separated by commas:
id نانووسین: AUTO_INCREMENT خۆی پڕی دەکاتەوە. نرخە دەقییەکان لە ناو کۆتەیشنی تاکدان؛ ژمارەکان نا. دەتوانیت یەک زیاد بکەیت، یان چەند کە بە کۆما جیا کراونەتەوە:
INSERT INTO students (name, city, marks) VALUES ('Aram', 'Erbil', 85);Query OK, 1 row affected
Result checked by running the statement on SQLite in our test sandbox (MySQL is not available there); MySQL returns the same rows.
ئەنجامەکە بە جێبەجێکردنی فەرمانەکە لەسەر SQLite لە ژینگەی تاقیکردنەوەماندا پشکنراوە (MySQL لەوێ بەردەست نییە)؛ MySQL هەمان دەگەڕێنێتەوە.
INSERT INTO students (name, city, marks) VALUES
('Shilan', 'Sulaymaniyah', 92),
('Karwan', 'Duhok', 78),
('Dilan', 'Erbil', 92),
('Rezan', 'Halabja', 66);Query OK, 4 rows affected
Result checked by running the statement on SQLite in our test sandbox (MySQL is not available there); MySQL returns the same rows.
ئەنجامەکە بە جێبەجێکردنی فەرمانەکە لەسەر SQLite لە ژینگەی تاقیکردنەوەماندا پشکنراوە (MySQL لەوێ بەردەست نییە)؛ MySQL هەمان دەگەڕێنێتەوە.
Copying rows from another tableکۆپیکردنی تۆمارەکان لە خشتەیەکی ترەوە
INSERT can also take its rows from a SELECT. There is no VALUES: every row that the SELECT finds becomes a new row, filling the listed columns in order. Here the students with 90 marks or more go into an honours list:
INSERT دەتوانێت خۆی لە SELECTێکیش وەربگرێت. VALUES نییە: هەر کە SELECTـەکە دەیدۆزێتەوە دەبێتە نوێ و بەو ڕیزەی نووسراون پڕ دەکاتەوە. لێرەدا ئەو خوێندکارانەی 90 نمرە یان زیاتریان هێناوە دەچنە لیستی شانازی:
CREATE TABLE honours (
id INT UNSIGNED NOT NULL PRIMARY KEY,
name VARCHAR(50) NOT NULL,
marks TINYINT UNSIGNED NOT NULL
);
INSERT INTO honours (id, name, marks)
SELECT id, name, marks FROM students WHERE marks >= 90;
SELECT * FROM honours;Query OK, 2 rows affected Records: 2 Duplicates: 0 Warnings: 0 +----+--------+-------+ | id | name | marks | +----+--------+-------+ | 2 | Shilan | 92 | | 4 | Dilan | 92 | +----+--------+-------+
Real output from MariaDB 10.4, the database server in XAMPP.
ڕاستەقینە لە MariaDB 10.4، ناو XAMPP.
To copy a whole table in one step, CREATE TABLE … AS SELECT … creates a new table and fills it; you will use it for students_copy in UPDATE. The copy gets the columns and the rows, but not the primary key or AUTO_INCREMENT. Microsoft Access calls these two an append query and a make-table query (SELECT … INTO): see the four action queries.
بۆ کۆپیکردنی هەموو بە یەک هەنگاو، CREATE TABLE … AS SELECT … نوێ دروست دەکات و پڕی دەکاتەوە؛ لە UPDATE بۆ students_copy بەکاری دەهێنیت. کۆپییەکە و وەردەگرێت، بەڵام و AUTO_INCREMENT نا. Microsoft Access بەم دووانە دەڵێت append query و make-table query (SELECT … INTO): بڕوانە چوار جۆرەکەی پرسی گۆڕانکاری.
The four basic statementsچوار فەرمانە سەرەکییەکە
SELECT: read rows (Read)SELECT: خوێندنەوەی تۆمار (Read)
SELECT * FROM table_name
WHERE column1 = 'some_text'
ORDER BY column1, column2 ASC;Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
*means "all columns".FROMnames the table.*واتە «هەموو ».FROMناوی دیاری دەکات.- Lineدێڕی 2
The keeps only the rows that match the condition. Text in straight single quotes.
تەنها ئەو دەهێڵێتەوە کە لەگەڵ مەرجەکە دەگونجێن. دەق لە ناو کۆتەیشنی تاکی ڕاستدا.
- Lineدێڕی 3
sorts the result: first by
column1, then bycolumn2.ئەنجامەکە ڕیز دەکات: سەرەتا بەپێی
column1، پاشان بەپێیcolumn2.
Read the whole table:
هەموو بخوێنەوە:
SELECT * FROM students;+----+--------+--------------+-------+ | id | name | city | marks | +----+--------+--------------+-------+ | 1 | Aram | Erbil | 85 | | 2 | Shilan | Sulaymaniyah | 92 | | 3 | Karwan | Duhok | 78 | | 4 | Dilan | Erbil | 92 | | 5 | Rezan | Halabja | 66 | +----+--------+--------------+-------+ 5 rows in set
Result checked by running the statement on SQLite in our test sandbox (MySQL is not available there); MySQL returns the same rows.
ئەنجامەکە بە جێبەجێکردنی فەرمانەکە لەسەر SQLite لە ژینگەی تاقیکردنەوەماندا پشکنراوە (MySQL لەوێ بەردەست نییە)؛ MySQL هەمان دەگەڕێنێتەوە.
Choose columns and rows. Instead of *, list the columns you need, and add WHERE:
و هەڵبژێرە. لە جیاتی *، ئەو بنووسە کە پێویستتن و WHERE زیاد بکە:
SELECT name, marks FROM students WHERE city = 'Erbil';+-------+-------+ | name | marks | +-------+-------+ | Aram | 85 | | Dilan | 92 | +-------+-------+ 2 rows in set
Result checked by running the statement on SQLite in our test sandbox (MySQL is not available there); MySQL returns the same rows.
ئەنجامەکە بە جێبەجێکردنی فەرمانەکە لەسەر SQLite لە ژینگەی تاقیکردنەوەماندا پشکنراوە (MySQL لەوێ بەردەست نییە)؛ MySQL هەمان دەگەڕێنێتەوە.
| Operator | Meaningواتا | Exampleنموونە |
|---|---|---|
= | equalیەکسان | city = 'Erbil' |
<> or != | not equalیەکسان نییە | city <> 'Erbil' |
< > <= >= | smaller / greaterبچووکتر / گەورەتر | marks >= 80 |
AND, OR | combine conditionsتێکەڵکردنی مەرجەکان | city = 'Erbil' AND marks > 90 |
LIKE | pattern: % means "any characters"شێوە: % واتە «هەر پیتێک» | name LIKE 'K%' |
More conditions (BETWEEN, IN, and why = must match the whole value: city = 'Erbil' does not find Erbil city), patterns with % and _, and sorting on several columns are in Better SELECT queries: see also LIKE and ORDER BY.
زیاتر (BETWEEN، IN و بۆچی = دەبێت لەگەڵ هەموو بگونجێت: city = 'Erbil' Erbil city نادۆزێتەوە)، % و _ و بەپێی چەند لە باشترکردنی فەرمانی SELECT دان: بڕوانە LIKE و ORDER BYـیش.
Sort the result with ORDER BY. ASC means small to big (), DESC big to small (). ASC is the default.
ئەنجامەکە بە ORDER BY ڕیز بکە. ASC واتە لە بچووکەوە بۆ گەورە ()، DESC لە گەورەوە بۆ بچووک (). ASC بنەڕەتییە.
SELECT * FROM students WHERE marks >= 80 ORDER BY marks DESC, name ASC;+----+--------+--------------+-------+ | id | name | city | marks | +----+--------+--------------+-------+ | 4 | Dilan | Erbil | 92 | | 2 | Shilan | Sulaymaniyah | 92 | | 1 | Aram | Erbil | 85 | +----+--------+--------------+-------+ 3 rows in set
Result checked by running the statement on SQLite in our test sandbox (MySQL is not available there); MySQL returns the same rows.
ئەنجامەکە بە جێبەجێکردنی فەرمانەکە لەسەر SQLite لە ژینگەی تاقیکردنەوەماندا پشکنراوە (MySQL لەوێ بەردەست نییە)؛ MySQL هەمان دەگەڕێنێتەوە.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
Highest marks first; when two students have the same mark (92), their names decide the order.
سەرەتا بەرزترین نمرە؛ کاتێک دوو خوێندکار هەمان نمرەیان هەیە (92)، ناوەکانیان ڕیزبەندییەکە دیاری دەکەن.
SELECT name, city, marks FROM students ORDER BY city, marks DESC;+--------+--------------+-------+ | name | city | marks | +--------+--------------+-------+ | Karwan | Duhok | 78 | | Dilan | Erbil | 92 | | Aram | Erbil | 85 | | Rezan | Halabja | 66 | | Shilan | Sulaymaniyah | 92 | +--------+--------------+-------+ 5 rows in set
Result checked by running the statement on SQLite in our test sandbox (MySQL is not available there); MySQL returns the same rows.
ئەنجامەکە بە جێبەجێکردنی فەرمانەکە لەسەر SQLite لە ژینگەی تاقیکردنەوەماندا پشکنراوە (MySQL لەوێ بەردەست نییە)؛ MySQL هەمان دەگەڕێنێتەوە.
The four basic statementsچوار فەرمانە سەرەکییەکە
UPDATE: change rows (Update)UPDATE: گۆڕینی تۆمار (Update)
UPDATE table_name
SET column1 = 'some_text'
WHERE id = 1;Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
SETgives the new value. Change several columns with commas:SET city = 'Erbil', marks = 90.SETنرخە نوێیەکە دەدات. بۆ گۆڕینی چەند کۆما بەکار بهێنە:SET city = 'Erbil', marks = 90.- Lineدێڕی 3
WHEREchooses which rows change. Using the primary keyidchanges exactly one row.WHEREدیاری دەکات کام بگۆڕدرێت. بەکارهێنانیidڕێک یەک دەگۆڕێت.
Karwan's mark was entered wrongly. Fix it, then check:
نمرەی Karwan بە هەڵە نووسرابوو. چاکی بکە، پاشان بیپشکنە:
UPDATE students SET marks = 81 WHERE id = 3;Query OK, 1 row affected
Result checked by running the statement on SQLite in our test sandbox (MySQL is not available there); MySQL returns the same rows.
ئەنجامەکە بە جێبەجێکردنی فەرمانەکە لەسەر SQLite لە ژینگەی تاقیکردنەوەماندا پشکنراوە (MySQL لەوێ بەردەست نییە)؛ MySQL هەمان دەگەڕێنێتەوە.
SELECT * FROM students WHERE id = 3;+----+--------+-------+-------+ | id | name | city | marks | +----+--------+-------+-------+ | 3 | Karwan | Duhok | 81 | +----+--------+-------+-------+ 1 row in set
Result checked by running the statement on SQLite in our test sandbox (MySQL is not available there); MySQL returns the same rows.
ئەنجامەکە بە جێبەجێکردنی فەرمانەکە لەسەر SQLite لە ژینگەی تاقیکردنەوەماندا پشکنراوە (MySQL لەوێ بەردەست نییە)؛ MySQL هەمان دەگەڕێنێتەوە.
The four basic statementsچوار فەرمانە سەرەکییەکە
DELETE: remove rows (Delete)DELETE: سڕینەوەی تۆمار (Delete)
DELETE FROM table_name
WHERE id = 1;Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 2
Only the rows that match
WHEREare deleted.تەنها ئەو دەسڕدرێنەوە کە لەگەڵ
WHEREدەگونجێن.
DELETE FROM students WHERE id = 5;Query OK, 1 row affected
Result checked by running the statement on SQLite in our test sandbox (MySQL is not available there); MySQL returns the same rows.
ئەنجامەکە بە جێبەجێکردنی فەرمانەکە لەسەر SQLite لە ژینگەی تاقیکردنەوەماندا پشکنراوە (MySQL لەوێ بەردەست نییە)؛ MySQL هەمان دەگەڕێنێتەوە.
SELECT * FROM students;+----+--------+--------------+-------+ | id | name | city | marks | +----+--------+--------------+-------+ | 1 | Aram | Erbil | 85 | | 2 | Shilan | Sulaymaniyah | 92 | | 3 | Karwan | Duhok | 81 | | 4 | Dilan | Erbil | 92 | +----+--------+--------------+-------+ 4 rows in set
Result checked by running the statement on SQLite in our test sandbox (MySQL is not available there); MySQL returns the same rows.
ئەنجامەکە بە جێبەجێکردنی فەرمانەکە لەسەر SQLite لە ژینگەی تاقیکردنەوەماندا پشکنراوە (MySQL لەوێ بەردەست نییە)؛ MySQL هەمان دەگەڕێنێتەوە.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 1
Rezan (id 5) is gone. The next new row will get id 6, not 5:
AUTO_INCREMENTdoes not reuse numbers.Rezan (id 5) نەما. نوێی داهاتوو id 6 وەردەگرێت، نەک 5:
AUTO_INCREMENTژمارەکان دووبارە بەکار ناهێنێتەوە.
Common mistakesهەڵە باوەکان
| Mistakeهەڵە | What happensچی ڕوو دەدات | Fixچارەسەر |
|---|---|---|
WHERE city = ‘Erbil’ | Syntax error #1064 (curly quotes) #1064 (کۆتەیشنی چەماوە) | WHERE city = 'Erbil' |
WHERE city = Erbil | MySQL looks for a column called Erbil: "Unknown column"MySQL بەدوای بە ناوی Erbil دەگەڕێت: «Unknown column» | Quote text values: 'Erbil'نرخی دەقی لە کۆتەیشندا دابنێ: 'Erbil' |
UPDATE students SET marks = 0; | Every row changesهەموو دەگۆڕێن | Always add WHERE (test it with SELECT first)هەمیشە WHERE زیاد بکە (سەرەتا بە SELECT تاقی بکەرەوە) |
DELETE FROM students; | The table becomes empty بەتاڵ دەبێت | Add WHERE id = ...WHERE id = ... زیاد بکە |
ORDER BY city, marks DESC | Only marks is descending; city stays ascendingتەنها marks نزمبوونەوەیە؛ city بەرزبوونەوە دەمێنێتەوە | Write a direction for each column you want reversedبۆ هەر کە دەتەوێت پێچەوانە بێت ئاڕاستە بنووسە |
price FLOAT | Rounding errors in moneyهەڵەی خڕکردنەوە لە پارەدا | price DECIMAL(10,2) |
id INT(11) | Works, but (11) does nothing useful (deprecated)کار دەکات، بەڵام (11) هیچ سوودێکی نییە (بەسەرچوو) | id INT |
| Database created with a latin1 collation بە latin1 دروست کراوە | Kurdish letters are saved as question marksپیتە کوردییەکان وەک نیشانەی پرسیار هەڵدەگیرێن | CHARACTER SET utf8mb4 |
CREATE TABLE takes (student_id INT, c-id VARCHAR(10)); | Syntax error: the hyphen is read as a minus sign (MySQL/MariaDB: #1064 … near '-id VARCHAR(10))'; SQLite: near "-": syntax error) : نیشانەی - وەک نیشانەی لێدەرکردن دەخوێنرێتەوە (MySQL/MariaDB: #1064 … near '-id VARCHAR(10))'؛ SQLite: near "-": syntax error) | Use an underscore: c_id. (Backticks, `c-id`, also work, but then every query must type them.) بەکار بهێنە: c_id. (بە backtick، `c-id`، کار دەکات، بەڵام ئەو کاتە دەبێت لە هەموو بینووسیت.) |
SELECT * FROM Students; -- the table is students | Works on Windows and macOS, but on a Linux server: Table 'school.Students' doesn't exist. There, database and table names follow the file system and are case-sensitive; column names never areلە Windows و macOS کار دەکات، بەڵام لەسەر Linux: Table 'school.Students' doesn't exist. لەوێ ناوی و بەدوای سیستەمی فایلەکاندا دەڕۆن و ؛ ناوی هەرگیز وا نین | Name databases and tables in small letters and always write them the same wayناوی و بە پیتی بچووک بنووسە و هەمیشە بە هەمان شێوە بیاننووسە |
Exercisesڕاهێنانەکان
More SELECT practice, with conditions, patterns and sorting on a shop database, is in the exercises of Better SELECT queries.
ڕاهێنانی زیاتری SELECT، بە ، و لەسەر دوکانێک، لە ڕاهێنانەکانی وانەی باشترکردنی فەرمانی SELECT دایە.
Design a table books for a school library. Each book has an id, a title (up to 100 characters), an author (up to 60 characters), the year it was published, a price with 2 decimal places, and the date it was added to the library. Write the CREATE TABLE statement.
books بۆ کتێبخانەی قوتابخانەیەک دیزاین بکە. هەر کتێبێک id، ناونیشان (تا 100 پیت)، نووسەر (تا 60 پیت)، ساڵی بڵاوبوونەوە، نرخ بە 2 ژمارەی دوای خاڵ و بەرواری زیادکردنی بۆ کتێبخانەکەی هەیە. فەرمانی CREATE TABLE بنووسە.
Hintئاماژە
Think: which values are whole numbers, which are text, which is money, which is a date?
بیر بکەرەوە: کام نرخ ، کامیان دەقە، کامیان پارەیە، کامیان بەروارە؟
Show answerوەڵامەکە پیشان بدە
CREATE TABLE books (
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(100) NOT NULL,
author VARCHAR(60) NOT NULL,
published YEAR,
price DECIMAL(6,2) NOT NULL,
added_on DATE NOT NULL
);YEAR (or SMALLINT) for the year — YEAR only holds 1901 to 2155, so choose SMALLINT if the library has older books; DECIMAL for the price because money must be exact, DATE for the day it was added. DECIMAL(6,2) allows prices up to 9999.99.
YEAR (یان SMALLINT) بۆ ساڵ — YEAR تەنها 1901 تا 2155 هەڵدەگرێت، بۆیە ئەگەر کتێبخانەکە کتێبی کۆنتری هەیە SMALLINT هەڵبژێرە؛ DECIMAL بۆ نرخ چونکە پارە دەبێت ورد بێت، DATE بۆ ئەو ڕۆژەی زیاد کراوە. DECIMAL(6,2) ڕێگە بە نرخی تا 9999.99 دەدات.
Using the students table from this lecture, write SQL statements that: (a) show the names of all students from Erbil, highest marks first; (b) add a new student Hana from Kirkuk with 88 marks; (c) change Dilan's city to Sulaymaniyah; (d) delete the student whose id is 2.
بە بەکارهێنانی studentsی ئەم وانەیە، فەرمانی SQL بنووسە کە: (أ) ناوی هەموو خوێندکارانی Erbil پیشان بدات، سەرەتا بەرزترین نمرە؛ (ب) خوێندکارێکی نوێ بە ناوی Hana لە Kirkuk بە نمرەی 88 زیاد بکات؛ (ج) شاری Dilan بگۆڕێت بۆ Sulaymaniyah؛ (د) ئەو خوێندکارە بسڕێتەوە کە id ـەکەی 2ـە.
Show answerوەڵامەکە پیشان بدە
-- (a)
SELECT name FROM students WHERE city = 'Erbil' ORDER BY marks DESC;
-- (b)
INSERT INTO students (name, city, marks) VALUES ('Hana', 'Kirkuk', 88);
-- (c)
UPDATE students SET city = 'Sulaymaniyah' WHERE id = 4;
-- (d)
DELETE FROM students WHERE id = 2;In (c) we use Dilan's id (4) instead of WHERE name = 'Dilan', because two students could have the same name.
لە (ج)دا idی Dilan (4) بەکار دەهێنین لە جیاتی WHERE name = 'Dilan'، چونکە دەکرێت دوو خوێندکار هەمان ناویان هەبێت.
Find and fix the three mistakes in this statement: UPDATE students SET marks = ‘95’, city = Duhok;
سێ هەڵەکەی ئەم فەرمانە بدۆزەرەوە و چاکیان بکە: UPDATE students SET marks = ‘95’, city = Duhok;
Show answerوەڵامەکە پیشان بدە
- Curly quotes around 95: a number needs no quotes at all.
Duhokis text, so it needs straight single quotes:'Duhok'.- There is no
WHERE, so every student would change.
- کۆتەیشنی چەماوە بە دەوری 95دا: ژمارە هیچ کۆتەیشنێکی پێویست نییە.
Duhokدەقە، بۆیە پێویستی بە کۆتەیشنی تاکی ڕاستە:'Duhok'.WHEREنییە، بۆیە هەموو خوێندکارەکان دەگۆڕێن.
UPDATE students SET marks = 95, city = 'Duhok' WHERE id = 3;You must store (a) the moment a user's account was created and (b) a student's date of birth. Which type do you choose for each: TIMESTAMP, DATETIME or DATE? Why?
دەبێت (أ) ساتی دروستبوونی هەژماری بەکارهێنەرێک و (ب) بەرواری لەدایکبوونی خوێندکارێک هەڵبگریت. بۆ هەر یەکەیان کام جۆر هەڵدەبژێریت: TIMESTAMP، DATETIME یان DATE؟ بۆچی؟
Show answerوەڵامەکە پیشان بدە
(a) TIMESTAMP DEFAULT CURRENT_TIMESTAMP: it records a moment in world time and is filled in automatically. (b) DATE: a birthday has no clock time, and it must not change when the time zone changes. (A DATETIME would also keep the value fixed, but the time part is not needed.)
(أ) TIMESTAMP DEFAULT CURRENT_TIMESTAMP: ساتێک بە کاتی جیهانی هەڵدەگرێت و خۆکارانە پڕ دەکرێتەوە. (ب) DATE: ڕۆژی لەدایکبوون کاتژمێری نییە و نابێت بە گۆڕینی بگۆڕێت. (DATETIMEیش نرخەکە جێگیر دەهێڵێتەوە، بەڵام بەشی کاتژمێرەکەی پێویست نییە.)
Name three things a database can do that a spreadsheet cannot do, or does badly.
سێ شت ناو بهێنە کە دەیکات و ناتوانێت بیکات، یان بە خراپی دەیکات.
Show answerوەڵامەکە پیشان بدە
- Many users at once, each with their own rights: hundreds of PHP pages read and write at the same moment, and a teacher may change marks while a student may only read them.
- Rules and links that refuse wrong data: types,
NOT NULL,UNIQUEand keys. A mark for a student who does not exist is refused. - Safe changes: a group of changes succeeds completely or not at all, and after a crash the database recovers.
- Fast questions over millions of rows in SQL, asked by programs such as PHP.
- چەندین لە یەک کاتدا، هەر یەکە بە مافی خۆی: سەدان لاپەڕەی PHP لە هەمان چرکەدا دەخوێننەوە و دەنووسن و مامۆستایەک دەتوانێت نمرە بگۆڕێت لە کاتێکدا خوێندکارێک تەنها دەتوانێت بیخوێنێتەوە.
- ڕێسا و پەیوەندی کە هەڵە ڕەت دەکەنەوە: جۆرەکان،
NOT NULL،UNIQUEو . نمرە بۆ خوێندکارێک کە بوونی نییە ڕەت دەکرێتەوە. - گۆڕانکاریی سەلامەت: کۆمەڵێک گۆڕانکاری یان بە تەواوی سەردەکەون یان هیچ و دوای تێکچوون خۆی چاک دەکاتەوە.
- پرسیاری خێرا لەسەر ملیۆنان بە SQL، کە وەک PHP دەیپرسن.
More: Fourteen advantages of a database.
زیاتر: چواردە سوودی بنکەدراوە.
Four class teams play a football tournament. Build a database sports with two tables. team: team_id is a whole number from 1 to 9 and the primary key (write the key on its own line, as a rule for the whole table); team_name must be at least 10 characters long. Show the structure of team, insert the teams Team Zagros, Team Halgurd, Team Safin and Team Piramagrun, and show the rows. Then create match_details for six matches (an id such as M1, the date, the two team ids and the two scores), with suitable types and rules.
چوار تیمی پۆلەکان پاڵەوانێتییەکی تۆپی پێ دەکەن. sports بە دوو دروست بکە. team: team_id لە 1 تا 9 و ( لەسەر دێڕی خۆی بنووسە، وەک ڕێسایەک بۆ هەموو )؛ team_name دەبێت لانیکەم 10 پیت بێت. پێکهاتەی team پیشان بدە، تیمەکانی Team Zagros، Team Halgurd، Team Safin و Team Piramagrun زیاد بکە و پیشان بدە. پاشان match_details بۆ شەش یاری دروست بکە (ناسنامەیەکی وەک M1، ڕێکەوت، ژمارەی هەردوو تیمەکە و ئەنجامی هەردووکیان)، بە جۆر و ڕێسای گونجاو.
Hintئاماژە
Use CHECK from Column rules and CHAR_LENGTH() for the length of the name.
CHECK لە ڕێساکانی ستوون و CHAR_LENGTH() بۆ درێژیی ناوەکە بەکار بهێنە.
Show answerوەڵامەکە پیشان بدە
CREATE DATABASE sports CHARACTER SET utf8mb4;
USE sports;
CREATE TABLE team (
team_id TINYINT UNSIGNED NOT NULL CHECK (team_id BETWEEN 1 AND 9),
team_name VARCHAR(30) NOT NULL CHECK (CHAR_LENGTH(team_name) >= 10),
PRIMARY KEY (team_id)
);
DESCRIBE team;
INSERT INTO team VALUES
(1, 'Team Zagros'), (2, 'Team Halgurd'), (3, 'Team Safin'), (4, 'Team Piramagrun');
SELECT * FROM team;
CREATE TABLE match_details (
match_id CHAR(3) NOT NULL PRIMARY KEY,
match_date DATE NOT NULL,
first_team_id TINYINT UNSIGNED NOT NULL,
second_team_id TINYINT UNSIGNED NOT NULL,
first_team_score TINYINT UNSIGNED NOT NULL,
second_team_score TINYINT UNSIGNED NOT NULL,
FOREIGN KEY (first_team_id) REFERENCES team (team_id),
FOREIGN KEY (second_team_id) REFERENCES team (team_id),
CHECK (first_team_id <> second_team_id)
);
INSERT INTO match_details VALUES
('M1', '2026-11-03', 1, 2, 3, 1), ('M2', '2026-11-04', 3, 4, 0, 2),
('M3', '2026-11-05', 1, 3, 2, 2), ('M4', '2026-11-05', 2, 4, 1, 4),
('M5', '2026-11-04', 1, 4, 0, 1), ('M6', '2026-11-03', 2, 3, 2, 0);+-----------+---------------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+---------------------+------+-----+---------+-------+ | team_id | tinyint(3) unsigned | NO | PRI | NULL | | | team_name | varchar(30) | NO | | NULL | | +-----------+---------------------+------+-----+---------+-------+ +---------+-----------------+ | team_id | team_name | +---------+-----------------+ | 1 | Team Zagros | | 2 | Team Halgurd | | 3 | Team Safin | | 4 | Team Piramagrun | +---------+-----------------+
Real output from MariaDB 10.4, the database server in XAMPP.
ڕاستەقینە لە MariaDB 10.4، ناو XAMPP.
Explain the codeڕوونکردنەوەی کۆدەکە
- Lineدێڕی 5-6
Column rules: the id range and the minimum name length.
ڕێساکانی : سنووری ژمارەکە و کەمترین درێژیی ناو.
- Lineدێڕی 7
The primary key as a table-level rule.
وەک ڕێسایەکی ئاستی .
- Lineدێڕی 22-24
Both team ids must be real teams (), and a team cannot play itself.
هەردوو ژمارەی تیم دەبێت تیمی ڕاستەقینە بن () و تیمێک ناتوانێت لەگەڵ خۆی یاری بکات.
Test the rules: INSERT INTO team VALUES (10, 'Team Hawraman'); is refused (MariaDB: CONSTRAINT team.team_id failed; MySQL 8: Check constraint 'team_chk_1' is violated), and so is the short name 'Team Ali'. A match of team 2 against team 2 breaks the last CHECK, and team 7 breaks the foreign key. CHAR_LENGTH() counts letters; LENGTH() counts bytes, which is different for Kurdish names (LENGTH or CHAR_LENGTH).
ڕێساکان تاقی بکەرەوە: INSERT INTO team VALUES (10, 'Team Hawraman'); ڕەت دەکرێتەوە (MariaDB: CONSTRAINT team.team_id failed؛ MySQL 8: Check constraint 'team_chk_1' is violated) و ناوە کورتەکەی 'Team Ali'یش هەروەها. یارییەکی تیمی 2 لەگەڵ تیمی 2 دوایین CHECK دەشکێنێت و تیمی 7 دەشکێنێت. CHAR_LENGTH() پیتەکان دەژمێرێت؛ LENGTH() دەژمێرێت، کە بۆ ناوی کوردی جیاوازە (LENGTH یان CHAR_LENGTH).
Exercisesڕاهێنانەکان
Lab: tasks from a practical examتاقیگە: ئەرکەکانی تاقیکردنەوەیەکی پراکتیکی
An old IT practical exam asked students to build this table in Microsoft Access and answer four questions (the same lab in Access). Do the same in SQL, in phpMyAdmin's SQL tab (the names are changed):
تاقیکردنەوەیەکی پراکتیکیی کۆنی IT داوای لە خوێندکاران کرد ئەم لە Microsoft Access دروست بکەن و وەڵامی چوار پرسیار بدەنەوە (هەمان تاقیگە لە Access). هەمان کار بە SQL بکە، لە تابی SQLـی phpMyAdmin (ناوەکان گۆڕدراون):
employee_id | full_name | age | address | basic_salary | department |
|---|---|---|---|---|---|
| 100 | Soran Ahmed | 26 | sul | 825 | HR |
| 101 | Hana Kamal | 34 | erb | 560 | Finance |
| 102 | Karwan Omar | 22 | sul | 400 | Customer Service |
| 103 | Shilan Aziz | 20 | sul | 825 | IT |
| 104 | Rezan Salih | 20 | erb | 700 | Sales |
Write CREATE TABLE employees so that no column can be left empty, and insert the five rows. How can you prove that the rule works?
CREATE TABLE employees بە شێوەیەک بنووسە کە هیچ بەتاڵ نەمێنێتەوە و پێنج زیاد بکە. چۆن دەسەلمێنیت کە ڕێساکە کار دەکات؟
Show answerوەڵامەکە پیشان بدە
CREATE TABLE employees (
employee_id INT UNSIGNED NOT NULL PRIMARY KEY,
full_name VARCHAR(50) NOT NULL,
age TINYINT UNSIGNED NOT NULL,
address VARCHAR(30) NOT NULL,
basic_salary DECIMAL(10,2) NOT NULL,
department VARCHAR(30) NOT NULL
);
INSERT INTO employees VALUES
(100, 'Soran Ahmed', 26, 'sul', 825, 'HR'),
(101, 'Hana Kamal', 34, 'erb', 560, 'Finance'),
(102, 'Karwan Omar', 22, 'sul', 400, 'Customer Service'),
(103, 'Shilan Aziz', 20, 'sul', 825, 'IT'),
(104, 'Rezan Salih', 20, 'erb', 700, 'Sales');
-- test: leave department out
INSERT INTO employees (employee_id, full_name, age, address, basic_salary)
VALUES (105, 'Dilan Faraj', 23, 'erb', 650);ERROR 1364 (HY000): Field 'department' doesn't have a default value
Real output from MariaDB 10.4, the database server in XAMPP.
ڕاستەقینە لە MariaDB 10.4، ناو XAMPP.
Every column is NOT NULL (in Access: the field property Required = Yes). The test row without a department is refused; with an explicit NULL the message is ERROR 1048: Column 'department' cannot be null, and SQLite says NOT NULL constraint failed: employees.department. DECIMAL keeps the salaries exact.
هەموو NOT NULLـە (لە Accessـدا: تایبەتمەندیی Required = Yes). تاقیکردنەوەکە بەبێ department ڕەت دەکرێتەوە؛ بە NULLی ئاشکرا پەیامەکە ERROR 1048: Column 'department' cannot be nullـە و SQLite دەڵێت NOT NULL constraint failed: employees.department. DECIMAL مووچەکان بە وردی دەپارێزێت.
Increase by 20 % the salary of every employee whose basic salary is below 600.
مووچەی هەموو فەرمانبەرێک کە مووچە بنەڕەتییەکەی لە 600 کەمترە 20 % زیاد بکە.
Show answerوەڵامەکە پیشان بدە
SELECT full_name, basic_salary FROM employees WHERE basic_salary < 600; -- check first
UPDATE employees SET basic_salary = basic_salary * 1.2 WHERE basic_salary < 600;
SELECT employee_id, full_name, basic_salary FROM employees;Query OK, 2 rows affected +-------------+-------------+--------------+ | employee_id | full_name | basic_salary | +-------------+-------------+--------------+ | 100 | Soran Ahmed | 825.00 | | 101 | Hana Kamal | 672.00 | | 102 | Karwan Omar | 480.00 | | 103 | Shilan Aziz | 825.00 | | 104 | Rezan Salih | 700.00 | +-------------+-------------+--------------+
Real output from MariaDB 10.4, the database server in XAMPP.
ڕاستەقینە لە MariaDB 10.4، ناو XAMPP.
basic_salary * 1.2 uses each row's own old value: 560 becomes 672 and 400 becomes 480. Run the SELECT with the same WHERE first, so you know which rows will change.
basic_salary * 1.2 کۆنی هەر خۆی بەکار دەهێنێت: 560 دەبێتە 672 و 400 دەبێتە 480. سەرەتا SELECTـەکە بە هەمان WHERE جێبەجێ بکە، بۆ ئەوەی بزانیت کام دەگۆڕێن.
Replace every address sul with Sulaymaniyah.
هەموو ناونیشانێکی sul بە Sulaymaniyah بگۆڕە.
Show answerوەڵامەکە پیشان بدە
UPDATE employees SET address = 'Sulaymaniyah' WHERE address = 'sul';Query OK, 3 rows affected
Real output from MariaDB 10.4, the database server in XAMPP.
ڕاستەقینە لە MariaDB 10.4، ناو XAMPP.
Three rows change (Soran, Karwan and Shilan). The order of the tasks matters: after this, a query for address = 'sul' finds nothing.
سێ دەگۆڕێن (Soran، Karwan و Shilan). ڕیزبەندیی ئەرکەکان گرنگە: دوای ئەمە، بۆ address = 'sul' هیچ نادۆزێتەوە.
The exam's other two tasks are plain SELECTs: who lives in sul (WHERE address = 'sul', before Lab 3: Soran, Karwan and Shilan), and who earns 825 and works in IT (WHERE basic_salary = 825 AND department = 'IT': Shilan). Queries of the same kind are exercises 1 and 8 in the exercises of Better SELECT queries.
دوو ئەرکەکەی تری تاقیکردنەوەکە SELECTـی سادەن: کێ لە sul دەژی (WHERE address = 'sul'، پێش تاقیگە 3: Soran، Karwan و Shilan) و کێ 825 وەردەگرێت و لە IT کار دەکات (WHERE basic_salary = 825 AND department = 'IT': Shilan). هەمان جۆر ڕاهێنانی 1 و 8ن لە ڕاهێنانەکانی وانەی باشترکردنی فەرمانی SELECT.
Exercisesڕاهێنانەکان
Lab: a second exam with two tablesتاقیگە: تاقیکردنەوەیەکی تر بە دوو خشتە
The other exam gave two tables: t1 with people, and t2 with each person's address, salary and mark, one row per subject. This script builds them, so you can go straight to the questions:
تاقیکردنەوەکەی تر دوو دا: t1 بە خەڵکەوە و t2 بە ناونیشان، مووچە و نمرەی هەر کەسێکەوە، بۆ هەر وانەیەک . ئەم دروستیان دەکات، بۆ ئەوەی ڕاستەوخۆ بچیتە سەر پرسیارەکان:
CREATE TABLE t1 (
id INT NOT NULL PRIMARY KEY,
first_name VARCHAR(30) NOT NULL,
last_name VARCHAR(30) NOT NULL,
age TINYINT UNSIGNED NOT NULL
);
CREATE TABLE t2 (
id INT NOT NULL,
address VARCHAR(30) NOT NULL,
salary DECIMAL(8,2) NOT NULL,
subject VARCHAR(20) NOT NULL,
marks DECIMAL(4,1) NOT NULL
);
INSERT INTO t1 VALUES (1, 'Aram', 'Kamal', 21), (2, 'Dilan', 'Omar', 17),
(3, 'Shilan', 'Aziz', 24), (4, 'Soran', 'Salih', 30);
INSERT INTO t2 VALUES
(1, 'sul', 200, 'C++', 20), (2, 'erb', 170, 'Java', 21), (1, 'sul', 200, 'Db', 11),
(1, 'sul', 200, 'Web', 22), (2, 'erb', 170, 'Java', 21), (1, 'sul', 200, 'Web', 11),
(3, 'duh', 175, 'Db', 21), (3, 'duh', 175, 'IS', 22), (4, 'kirkuk', 210, 'IT', 21);
SELECT * FROM t2;+----+---------+--------+---------+-------+ | id | address | salary | subject | marks | +----+---------+--------+---------+-------+ | 1 | sul | 200.00 | C++ | 20.0 | | 2 | erb | 170.00 | Java | 21.0 | | 1 | sul | 200.00 | Db | 11.0 | | 1 | sul | 200.00 | Web | 22.0 | | 2 | erb | 170.00 | Java | 21.0 | | 1 | sul | 200.00 | Web | 11.0 | | 3 | duh | 175.00 | Db | 21.0 | | 3 | duh | 175.00 | IS | 22.0 | | 4 | kirkuk | 210.00 | IT | 21.0 | +----+---------+--------+---------+-------+
Real output from MariaDB 10.4, the database server in XAMPP.
ڕاستەقینە لە MariaDB 10.4، ناو XAMPP.
t2 چ کێشەیەکی هەیە؟Which facts does t2 store more than once? What does the exam's task give a 20 % raise to everyone whose salary is below 200 do to this table?
t2 کام ڕاستییانە زیاتر لە جارێک هەڵدەگرێت؟ ئەرکی تاقیکردنەوەکە، مووچەی هەر کەسێک کە لە 200 کەمترە 20 % زیاد بکە، چی بەم دەکات؟
Show answerوەڵامەکە پیشان بدە
Address and salary are repeated on every subject row: person 1's sul, 200 is stored four times. The row 2, erb, 170, Java, 21 is there twice, because t2 has no primary key and nothing stops a duplicate. The raise changes four rows for two people:
ناونیشان و مووچە لەسەر هەموو وانەیەک دووبارە دەبنەوە: sul, 200ی کەسی 1 چوار جار هەڵگیراوە. 2, erb, 170, Java, 21 دوو جار هەیە، چونکە t2 نییە و هیچ شتێک ڕێگە لە دووبارەبوونەوە ناگرێت. زیادکردنەکە چوار بۆ دوو کەس دەگۆڕێت:
UPDATE t2 SET salary = salary * 1.2 WHERE salary < 200;
SELECT id, address, salary, subject FROM t2 ORDER BY id;Query OK, 4 rows affected +----+---------+--------+---------+ | id | address | salary | subject | +----+---------+--------+---------+ | 1 | sul | 200.00 | C++ | | 1 | sul | 200.00 | Db | | 1 | sul | 200.00 | Web | | 1 | sul | 200.00 | Web | | 2 | erb | 204.00 | Java | | 2 | erb | 204.00 | Java | | 3 | duh | 210.00 | Db | | 3 | duh | 210.00 | IS | | 4 | kirkuk | 210.00 | IT | +----+---------+--------+---------+
Real output from MariaDB 10.4, the database server in XAMPP.
ڕاستەقینە لە MariaDB 10.4، ناو XAMPP.
If one copy were missed, one person would have two different salaries: an (the anomalies). A better design keeps the address and salary once, in t1, and only id, subject and marks in the marks table, with the primary key (id, subject).
ئەگەر یەک کۆپی لەبیر بکرایە، کەسێک دوو مووچەی جیاوازی دەبوو: ئەمە (ناڕێکییەکان). دیزاینێکی باشتر ناونیشان و مووچە یەک جار لە t1 هەڵدەگرێت و لە نمرەکاندا تەنها id، subject و marks، بە (id, subject).
The pass mark is 12.5. List the people (first and last name) who failed at least one subject, each person once.
نمرەی دەرچوون 12.5ـە. ئەو کەسانە ڕیز بکە (ناوی یەکەم و دووەم) کە لانیکەم لە یەک وانەدا دەرنەچوون، هەر کەسێک یەک جار.
Hintئاماژە
The names are in t1 and the marks in t2: join the two tables on id.
ناوەکان لە t1ـدان و نمرەکان لە t2: هەردوو لەسەر id پێکەوە ببەستە.
Show answerوەڵامەکە پیشان بدە
SELECT DISTINCT t1.id, t1.first_name, t1.last_name
FROM t1
JOIN t2 ON t2.id = t1.id
WHERE t2.marks < 12.5;+----+------------+-----------+ | id | first_name | last_name | +----+------------+-----------+ | 1 | Aram | Kamal | +----+------------+-----------+
Real output from MariaDB 10.4, the database server in XAMPP.
ڕاستەقینە لە MariaDB 10.4، ناو XAMPP.
JOIN … ON puts each mark row next to its person (joins). Aram failed two subjects (Db and the second Web row), so without DISTINCT he would appear twice. With WHERE t2.address = 'sul' the same join answers the exam's who lives in sul?: again only Aram.
JOIN … ON هەر نمرە لە تەنیشت کەسەکەی دادەنێت (لێکبەستنەکان). Aram لە دوو وانەدا دەرنەچوو (Db و دووەمی Web)، بۆیە بەبێ DISTINCT دوو جار دەردەکەوت. بە WHERE t2.address = 'sul' هەمان وەڵامی پرسیاری تاقیکردنەوەکە دەداتەوە، کێ لە sul دەژی؟: دیسان تەنها Aram.
Summary and reviewپوختە و پێداچوونەوە
- A database keeps data after the script ends. It contains tables; a table has columns (fields) and rows (records).
- PHP and MySQL: cross-platform, the most popular pair, good for small and large sites, free. XAMPP actually runs MariaDB, which works the same.
- Pick a type for every column:
INT/TINYINTfor whole numbers,DECIMALfor money,VARCHARfor short text,DATE/DATETIME/TIMESTAMPfor time. TIMESTAMPfollows the time zone (UTC inside, range to 2038);DATETIMEnever changes. Both can be indexed.CREATE DATABASE,USE,CREATE TABLEbuild the structure;INSERT,SELECT,UPDATE,DELETEwork with the rows (CRUD).SHOW TABLESandDESCRIBEshow what exists;ALTER TABLEchanges a table and keeps its rows;INSERT … SELECTcopies rows from another table.- Straight quotes around text, a
WHEREon everyUPDATEandDELETE, and a direction for eachORDER BYcolumn you want reversed.
- زانیارییەکان دوای تەواوبوونی دەپارێزێت. لەخۆ دەگرێت؛ هەر و ڕیز ()ی هەیە.
- PHP و MySQL: ، بەناوبانگترین جووت، بۆ بچووک و گەورە گونجاو، بەخۆڕایی. XAMPP لە ڕاستیدا MariaDB کار پێ دەکات، کە هەمان شێوە کار دەکات.
- بۆ هەر جۆرێک هەڵبژێرە:
INT/TINYINTبۆ ،DECIMALبۆ پارە،VARCHARبۆ دەقی کورت،DATE/DATETIME/TIMESTAMPبۆ کات. TIMESTAMPبەدوای دەڕوات (لە ناوەوە UTC، مەودا تا 2038)؛DATETIMEهەرگیز ناگۆڕێت. بۆ هەردووکیان دەتوانرێت دابنرێت.CREATE DATABASE،USE،CREATE TABLEپێکهاتەکە دروست دەکەن؛INSERT،SELECT،UPDATE،DELETEکار لەگەڵ دەکەن (CRUD).SHOW TABLESوDESCRIBEپیشان دەدەن چی هەیە؛ALTER TABLEدەگۆڕێت و دەپارێزێت؛INSERT … SELECTلە ترەوە کۆپی دەکات.- کۆتەیشنی ڕاست بە دەوری دەقدا،
WHEREلەگەڵ هەمووUPDATEوDELETEێک و ئاڕاستە بۆ هەرORDER BYکە دەتەوێت پێچەوانە بێت.
What is the difference between a table, a row and a column?جیاوازیی نێوان خشتە، ڕیز و ستوون چییە؟
A table holds one kind of thing (students). A row (record) is one of those things (one student). A column (field) is one piece of information that every row has (name, city).
یەک جۆر شت هەڵدەگرێت (خوێندکاران). ڕیز () یەکێکە لەو شتانە (یەک خوێندکار). یەک پارچە زانیارییە کە هەموو هەیەتی (ناو، شار).
Give four reasons why PHP is often used with MySQL.چوار هۆکار بهێنەرەوە بۆ ئەوەی بۆچی PHP زۆرجار لەگەڵ MySQL بەکار دێت.
Both are cross-platform; MySQL is the most popular database used with PHP; it suits both small and large applications; it is free to download and use.
هەردووکیان ؛ MySQL بەناوبانگترین کە لەگەڵ PHP بەکار دێت؛ بۆ بەرنامەی بچووک و گەورە گونجاوە؛ بەخۆڕایی دادەبەزێنرێت و بەکار دێت.
Name the exact and the approximate numeric types.جۆرە ژمارەییە وردەکان و نزیکەییەکان ناو بهێنە.
Exact: INTEGER (INT), SMALLINT, DECIMAL, NUMERIC (and TINYINT, MEDIUMINT, BIGINT). Approximate: FLOAT, REAL, DOUBLE PRECISION.
ورد: INTEGER (INT)، SMALLINT، DECIMAL، NUMERIC (و TINYINT، MEDIUMINT، BIGINT). نزیکەیی: FLOAT، REAL، DOUBLE PRECISION.
How do TIMESTAMP and DATETIME differ?TIMESTAMP و DATETIME چۆن جیاوازن؟
TIMESTAMP is converted from the current time zone to UTC when stored and back when read, so what you see changes with the time-zone setting; its range ends in 2038. DATETIME is stored exactly as given and never changes; its range is 1000–9999. Both can be indexed.
TIMESTAMP لە کاتی هەڵگرتندا لە ئێستاوە بۆ UTC دەگۆڕدرێت و لە کاتی خوێندنەوەدا دەگەڕێنرێتەوە، بۆیە ئەوەی دەیبینیت بە گۆڕینی ڕێکخستنی دەگۆڕێت؛ مەوداکەی لە 2038دا کۆتایی دێت. DATETIME وەک خۆی هەڵدەگیرێت و هەرگیز ناگۆڕێت؛ مەوداکەی 1000–9999ە. بۆ هەردووکیان دەتوانرێت دابنرێت.
What do CREATE DATABASE, USE and CREATE TABLE do?CREATE DATABASE، USE و CREATE TABLE چی دەکەن؟
CREATE DATABASE makes a new empty database, USE selects it for the next statements, and CREATE TABLE makes a table with named, typed columns and rules such as NOT NULL, AUTO_INCREMENT and PRIMARY KEY.
CREATE DATABASE نوێی بەتاڵ دروست دەکات، USE هەڵیدەبژێرێت بۆ فەرمانەکانی دواتر و CREATE TABLE دروست دەکات بە ناودار و جۆردار و یاسای وەک NOT NULL، AUTO_INCREMENT و PRIMARY KEY.
Why is UPDATE without WHERE dangerous?بۆچی UPDATE بەبێ WHERE مەترسیدارە؟
Because it changes every row of the table at once, and there is no undo.
چونکە لە یەک کاتدا هەموو دەگۆڕێت و گەڕانەوەی نییە.
Videoڤیدیۆ
Watch a videoسەیری ڤیدیۆیەک بکە
A video that viewers liked a lot and praised in their comments, chosen for this lesson.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.
A calm four-minute introduction: what SQL is, how it talks to a database, and simple examples of reading, adding and deleting data.
ناساندنێکی ئارامی چوار خولەکی: چییە، چۆن قسە لەگەڵ دەکات و چەند نموونەیەکی سادە بۆ خوێندنەوە، زیادکردن و سڕینەوەی .
Whole video: a gentle overview of the lesson.
هەموو ڤیدیۆکە: کورتەیەکی ئارامی وانەکە.
Lesson complete!وانەکە تەواو بوو!
Well done. Review the key terms, then go on to the next lesson.دەستخۆش. سەیرێکی زاراوە سەرەکییەکان بکەرەوە، پاشان بڕۆ بۆ وانەی داهاتوو.
Key termsزاراوە سەرەکییەکان
Creating a database with phpMyAdminدروستکردنی بنکەدراوە بە phpMyAdmin
Related lecturesوانە پەیوەندیدارەکان
- Creating a database with phpMyAdminدروستکردنی بنکەدراوە بە phpMyAdmin
- Connecting PHP to MySQLپەیوەندیکردنی PHP بە MySQL
- Inserting and selecting data with PHPزیادکردن و هەڵبژاردنی زانیاری بە PHP
- Building a CRUD Application with PHP and MySQLدروستکردنی بەرنامەیەکی CRUD بە PHP و MySQL
- SQLite Basicsبنەماکانی SQLite
References and further readingسەرچاوەکان و خوێندنەوەی زیاتر
- MySQL 8.0 Reference Manual: Numeric Data Types
- MySQL 8.0 Reference Manual: The DATE, DATETIME, and TIMESTAMP Types
- MySQL 8.0 Reference Manual: String Data Types
- MySQL 8.0 Reference Manual: CREATE TABLE Statement
- MySQL 8.0 Reference Manual: SELECT Statement
- MariaDB Knowledge Base: Data Types
- MySQL 8.4 Reference Manual: Identifier Case Sensitivity
- MySQL 8.4 Reference Manual: ALTER TABLE
- MySQL 8.4 Reference Manual: CHECK Constraints
- MySQL 8.0.19 release notes: integer display width no longer shown by DESCRIBE
- SIX: ISO 4217 currency codes (maintenance agency)
This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.