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.

34 min read35 خولەک خوێندنەوە · 25 stepsهەنگاو

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 .

ڕاستەقینە دەبێت شتەکانی لەبیر بێت: ، کاڵا و نرخەکانیان، خوێندکاران و نمرەکانیان، نامەکان. ئەم زانیارییانە لە هەڵدەگرن. زانیارییەکان دوای تەواوبوونی ، دوای دووبارە هەڵکردنەوەی و سبەینێش هەر دەمێننەوە. بەمە دەوترێت .

Definitionپێناسە

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.

کۆمەڵەیەکی ڕێکخراوی دراوەیە کە بە شێوەیەک خەزن کراوە بەرنامەیەک بتوانێت بە خێرایی بیدۆزێتەوە، زیادی بکات، بیگۆڕێت و بیسڕێتەوە. تۆڕگەکان زانیاریی بەکارهێنەر و کاڵا و نامەکانیان تێدا خەزن دەکەن.

Whiteboard or register book?تەختەی پۆل یان دەفتەری ئیدارە؟

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

ئەم بەشە چی لەخۆ دەگرێت

  1. SQL basics (this lecture): tables, data types and the first statements.
  2. Creating a database step by step with XAMPP and phpMyAdmin, screen by screen: phpMyAdmin.
  3. Connecting from PHP, "opening the door to your data": Connecting PHP to MySQL.
  4. Inserting a record and a form that saves what the visitor types, and reading rows back: Insert and select.
  5. 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.
  1. بنەماکانی (ئەم وانەیە): ، و یەکەم فەرمانەکان.
  2. دروستکردنی هەنگاو بە هەنگاو بە XAMPP و phpMyAdmin، شاشە بە شاشە: phpMyAdmin.
  3. پەیوەندیکردن لە PHPـەوە، «کردنەوەی دەرگای زانیارییەکانت»: پەیوەندیکردنی PHP بە MySQL.
  4. زیادکردنی و کە ئەوەی سەردانکەر دەینووسێت هەڵیدەگرێت و خوێندنەوەی : زیادکردن و هەڵبژاردن.
  5. پاراستنی ، بەرنامەیەکی تەواوی و — « کە هەمووی لە یەک فایلدایە»، لەگەڵ پێنج هۆکار کە بۆچی بۆ کۆرسێکی وەک ئەمە گونجاوە: ئاسایشی بنکەدراوە، بەرنامەی CRUD، SQLite.
داتابەیس چییە ؟ - What is Database [ Kurdish ]K97 Media · Kurdish (Sorani)کوردی (سۆرانی) · 28K viewsبینین · 1.3K likesلایک

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 name or city. 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، بۆ ئەوەی هەر بە ژمارەی تایبەتی خۆی بدۆزرێتەوە.
Left: a spreadsheet compared with a database (workbook = database, sheet = table, row = record, column = field, cell = one value). Right: the table students in the database school, with columns id, name, city and marks; the city column, the row for Shilan and one cell are highlighted, and sheet tabs at the bottom show the tables students and courses.لای چەپ: بەراوردی پەڕەی ئێکسڵ لەگەڵ بنکەدراوە (فایلی ئێکسڵ = بنکەدراوە، پەڕە = خشتە، ڕیز = تۆمار، ستوونی ئێکسڵ = ستوون، خانە = یەک نرخ). لای ڕاست: خشتەی students لە بنکەدراوەی school، بە ستوونەکانی id، name، city و marks؛ ستوونی city، ڕیزی Shilan و یەک خانە ڕەنگ کراون و تابەکانی خوارەوە خشتەکانی students و courses پیشان دەدەن.
A database is organized like a spreadsheet file: tables are sheets, records are rows, fields are columns. وەک فایلێکی ئێکسڵ ڕێکخراوە: پەڕەکانن، ڕیزەکانن و وەک ئێکسڵن.CêIT
Columns are the questionsستوونەکان پرسیارەکانن

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.

لە ئێکسڵدا دەتوانیت هەر شتێک لە هەر خانەیەکدا بنووسیت. بەڵام توندترە: هەر جۆرێکی جێگیری هەیە، بۆیە ڕەتی دەکاتەوە ئەگەر لە جیاتی ژمارە وشەیەک بنووسیت.

Quick checkپشکنینی خێراIn the table students, what is one row?لە studentsدا، یەک ڕیز چییە؟

Database, table, row and columnبنکەدراوە، خشتە، تۆمار و ستوون

Three kinds of tableسێ جۆر خشتە

The word table is used for three different things, and they are not the same:

وشەی خشتە بۆ سێ شتی جیاواز بەکار دێت و ئەوانە وەک یەک نین:

A database table, a spreadsheet and an HTML table ، و HTML
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ژماردن و لیستی خێرا بۆ یەک کەس
HTMLNothing: <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.

یاسا توندەکەی ، هەموو هەمان هەیە و هەر یەک جۆری جێگیری هەیە، لەوانەیە وەک بەربەست دیار بێت. بەڵام ڕێک ئەوەیە کە ڕێگە بە دەدات هەموو نوێ بپشکنێت و بە خێرایی لەناو ملیۆنان بگەڕێت.

Quick checkپشکنینی خێراTrue or false? A database table has a fixed number of columns, but it can have any number of rows.ڕاست یان هەڵە؟ ژمارەیەکی جێگیری هەیە، بەڵام دەتوانێت هەر ژمارەیەک هەبێت.

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 بەکار دەهێنرێت:

  1. Both are . You can develop on Windows and serve the finished site on a Unix (Linux) server without changing your code.
  2. MySQL is the most popular database system used with PHP. Most tutorials, hosting companies and PHP applications support it.
  3. MySQL is ideal for both small and large applications, from a class project to sites with millions of users.
  4. 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.
  1. هەردووکیان . دەتوانیت لەسەر Windows گەشەی پێ بدەیت و تەواوکراوەکە لەسەر ⁦Unix (Linux)⁩ دابنێیت، بەبێ گۆڕینی کۆدەکەت.
  2. MySQL بەناوبانگترین سیستەمی کە لەگەڵ PHP بەکار دێت. زۆربەی فێرکارییەکان، کۆمپانیاکانی هۆستینگ و بەرنامەکانی PHP پشتگیری دەکەن.
  3. MySQL بۆ بەرنامەی بچووک و گەورەش گونجاوە، لە پرۆژەیەکی پۆلەوە تا خاوەن ملیۆنان بەکارهێنەر.
  4. MySQL بەخۆڕایی دادەبەزێنرێت و بەکار دەهێنرێت. وەشانە بەخۆڕاییەکە ⁦Community Edition⁩ـە کە ؛ کۆمپانیای Oracle وەشانی پارەداریش دەفرۆشێت کە پشتگیریی زیاتری هەیە.
Quick checkپشکنینی خێراThe XAMPP control panel says MySQL. Which database server does modern XAMPP really start? XAMPP دەڵێت MySQL. XAMPPـی نوێ لە ڕاستیدا کام هەڵدەکات؟

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, FROM and WHERE are 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ـی تایبەتی خۆی هەیە:

The four basic SQL statementsچوار فەرمانە سەرەکییەکەی SQL
CRUDSQL statementفەرمانی SQLWhat it doesچی دەکات
CreateCreate (دروستکردن)INSERTadds a new row نوێ زیاد دەکات
ReadRead (خوێندنەوە)SELECTreads rows دەخوێنێتەوە
UpdateUpdate (نوێکردنەوە)UPDATEchanges rows that already existئەو دەگۆڕێت کە هەن
DeleteDelete (سڕینەوە)DELETEremoves 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 دروست دەکرێت.

Quick checkپشکنینی خێراWhich statement reads data from a table?کام فەرمان زانیاری لە دەخوێنێتەوە؟

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.

هەر هەیە. جۆرەکە دەڵێت چی دەکرێت لەوێ هەڵبگیرێت (، دەق، بەروار) و چەند شوێن دەگرێت. هەڵبژاردنی جۆری دروست ڕێگە لە زانیاریی هەڵە دەگرێت و بچووکتر و خێراتر دەکات.

Definitionپێناسە

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, DECIMAL and NUMERIC.
  • Approximate types store a very close value, which can be a tiny bit wrong: FLOAT, REAL and DOUBLE PRECISION (DOUBLE).
  • جۆرە وردەکان ژمارەکە بە تەواوی وەک خۆی هەڵدەگرن: INTEGER (INT)، SMALLINT، DECIMAL و NUMERIC.
  • جۆرە نزیکەییەکان نرخێکی زۆر نزیک هەڵدەگرن کە لەوانەیە کەمێک جیاواز بێت: FLOAT، REAL و DOUBLE PRECISION (DOUBLE).
MySQL numeric types (the slides give the types and byte sizes; ranges and uses added)جۆرە ژمارەییەکانی MySQL (سلایدەکان جۆر و قەبارەکان دەدەن؛ مەوداکان و بەکارهێنانەکان زیاد کراون)
TypeجۆرStorageقەبارەSigned rangeمەودا (بە نیشانە)UNSIGNED rangeمەودا ⁦(UNSIGNED)⁩Good forگونجاو بۆ
TINYINT1 byte1 بایت−128 … 1270 … 255age, yes/no (0 or 1)تەمەن، بەڵێ/نەخێر (⁦0⁩ یان ⁦1⁩)
SMALLINT2 bytes2 بایت−32,768 … 32,7670 … 65,535a year, small countsساڵ، ژمارەی بچووک
MEDIUMINT3 bytes3 بایت−8,388,608 … 8,388,6070 … 16,777,215medium countsژمارەی مامناوەند
INT4 bytes4 بایت−2,147,483,648 … 2,147,483,6470 … 4,294,967,295id columns, most whole numbers id، زۆربەی
BIGINT8 bytes8 بایت−2⁶³ … 2⁶³−10 … 2⁶⁴−1very large numbersژمارەی زۆر گەورە
DECIMAL(p,s) / NUMERICdepends on the digitsبەپێی ژمارەی ڕەقەمەکانexactورد—money: DECIMAL(10,2)پارە: DECIMAL(10,2)
FLOAT4 bytes4 بایتapproximateنزیکەیی—measurementsپێوانەکان
DOUBLE / DOUBLE PRECISION / REAL8 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:

سلایدەکان تەنها ژمارە و بەروار ڕیز دەکەن، بەڵام نزیکەی هەموو دەقیش هەڵدەگرێت. ئەمانە ئەو جۆرە دەقییانەن کە لەم کۆرسەدا بەکاریان دەهێنیت:

Common MySQL text typesجۆرە دەقییە باوەکانی MySQL
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)
TEXTlong text, up to 65,535 bytesدەقی درێژ، تا ⁦65,535⁩ بایتcomment TEXT
BLOBbinary 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'.

MySQL date and time types (formats and ranges added to the slide's list)جۆرەکانی بەروار و کات لە MySQLدا (شێوە و مەوداکان زیاد کراون بۆ لیستی سلایدەکە)
TypeجۆرFormatشێوەExampleنموونەRangeمەودا"Zero" value on the slideنرخی «سفر» لە سلایدەکەدا
DATETIMEYYYY-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'
DATEYYYY-MM-DD'2026-10-16'1000-01-01 … 9999-12-31'0000-00-00'
TIMESTAMPYYYY-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'
TIMEhh:mm:ss'14:30:00'−838:59:59 … 838:59:59'00:00:00'
YEARYYYY20261901 … 21550000

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 compared with DATETIMEبەراوردی TIMESTAMP لەگەڵ DATETIME
TIMESTAMPDATETIME
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_TIMESTAMPbirthdays, exam dates, appointments, dates after 2038ڕۆژی لەدایکبوون، بەرواری تاقیکردنەوە، ژوانەکان، بەرواری دوای ⁦2038⁩
See the differenceجیاوازییەکە ببینەSQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
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;
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, ts is shown 3 hours later; dt does not move.

دوای گۆڕینی ، ts بە 3 کاتژمێر دواتر پیشان دەدرێت؛ dt ناجووڵێت.

World time and notebook timeکاتی جیهانی و کاتی دەفتەر

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 پەیوەندییەکە بەکار دەهێنێت (زۆرجار هی )، نەک هی سەردانکەر.

Quick checkپشکنینی خێراWhich statement about TIMESTAMP and DATETIME is true?کام قسە دەربارەی TIMESTAMP و DATETIME ڕاستە؟

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 ( ... ); دروست دەکات و ڕیز دەکات، هەر یەکەیان لەگەڵ جۆر و یاساکانی.
The example from the lectureنموونەکەی وانەکەSQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
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_corp from 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), and auto_increment gives 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

id is the : no two rows may have the same id.

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).

SQL Full Course for free 🐬Bro Code · Englishئینگلیزی · 4.1M viewsبینین · 85K likesلایک · Partبەشی 10:29–22:38 (ofلە 3:00:00)

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)⁩

Column rules you will meet oftenئەو یاسایانەی کە زۆر دەیانبینیت
RuleیاساMeaningواتا
NOT NULLThe column must always have a value; it may not be empty (NULL). دەبێت هەمیشە نرخێکی هەبێت؛ نابێت بەتاڵ (NULL) بێت.
AUTO_INCREMENTMySQL fills in the next number by itself. Use it on the id column. See .MySQL خۆی ژمارەی دواتر دادەنێت. لەسەر id بەکاری بهێنە. بڕوانە .
PRIMARY KEYThe column that identifies each row; its values are unique and never empty.ئەو هەر دەناسێنێت؛ نرخەکانی دووبارە نابنەوە و هەرگیز بەتاڵ نین.
UNIQUENo two rows may have the same value (useful for email).هیچ دوو ناتوانن هەمان نرخیان هەبێت (بۆ email بەسوودە).
DEFAULT valueThe value used when an INSERT does not give one.ئەو نرخەی بەکار دێت کاتێک INSERT نرخێک نادات.
UNSIGNEDNo 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ـیش جێبەجێی دەکات. ڕێسای زیاتری لەم جۆرە: ڕێساکانی دروستی.
Quick checkپشکنینی خێراWhat does INT(11) mean in MySQL?INT(11) لە MySQLدا واتای چییە؟

Creating a database and a tableدروستکردنی بنکەدراوە و خشتە

The same table in modern styleهەمان خشتە بە شێوازی نوێ

The same idea in modern styleهەمان بیرۆکە بە شێوازی نوێSQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
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

utf8mb4 is the that can store every language, Kurdish included (and emoji). Always use it.

utf8mb4 ئەو کە دەتوانێت هەموو زمانێک هەڵبگرێت، کوردیش (و ئیمۆجی). هەمیشە بەکاری بهێنە.

Lineدێڕی 4

All the rules for id on 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.

SQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
SHOW TABLES;
DESCRIBE students;
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

MariaDB still shows the old display widths, int(10) and tinyint(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).

Quick checkپشکنینی خێراWhich statement shows the columns of students and their types?کام students و جۆرەکانیان پیشان دەدات؟

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 پێکهاتەکە دەگۆڕێت و دەپارێزێت:

SQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
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
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

A new column at the end. The rows that already exist get NULL in it.

نوێ لە کۆتاییدا. ئەو هەن NULL تێیدا وەردەگرن.

Lineدێڕی 2

No two students may have the same phone number (several NULLs are allowed).

هیچ دوو خوێندکارێک ناتوانن هەمان ژمارەی تەلەفۆنیان هەبێت (چەند NULLێک ڕێگەپێدراوە).

Lineدێڕی 3

MODIFY repeats the whole column definition: the new type and every rule you want to keep.

MODIFY هەموو پێناسەی دووبارە دەکاتەوە: جۆرە نوێیەکە و هەموو ڕێسایەک کە دەتەوێت بیهێڵیتەوە.

Lineدێڕی 6

DROP COLUMN deletes the column with all its values. There is no undo.

DROP COLUMN لەگەڵ هەموو دەسڕێتەوە. گەڕانەوەی نییە.

Quick checkپشکنینی خێراA table already holds 500 students and now needs an email column. What do you do? پێشتر ⁦500⁩ خوێندکاری تێدایە و ئێستا پێویستی بە email هەیە. چی دەکەیت؟

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ی بەشی پێشوو کار دەکەن. دوای هەر فەرمانێک ئەو ئەنجامە دێت کە دەیگەڕێنێتەوە.

SQL Full Course for free 🐬Bro Code · Englishئینگلیزی · 4.1M viewsبینین · 85K likesلایک · Partبەشی 22:38–37:03 (ofلە 3:00:00)

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)⁩

FormatشێوەSQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
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 خۆی پڕی دەکاتەوە. نرخە دەقییەکان لە ناو کۆتەیشنی تاکدان؛ ژمارەکان نا. دەتوانیت یەک زیاد بکەیت، یان چەند کە بە کۆما جیا کراونەتەوە:

SQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
INSERT INTO students (name, city, marks) VALUES ('Aram', 'Erbil', 85);
SQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
INSERT INTO students (name, city, marks) VALUES
    ('Shilan', 'Sulaymaniyah', 92),
    ('Karwan', 'Duhok', 78),
    ('Dilan', 'Erbil', 92),
    ('Rezan', 'Halabja', 66);

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⁩ نمرە یان زیاتریان هێناوە دەچنە لیستی شانازی:

SQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
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;

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): بڕوانە چوار جۆرەکەی پرسی گۆڕانکاری.

Quick checkپشکنینی خێراAfter INSERT INTO honours (id, name, marks) SELECT id, name, marks FROM students WHERE marks >= 90;, how many rows does honours have?دوای INSERT INTO honours (id, name, marks) SELECT id, name, marks FROM students WHERE marks >= 90;، honours چەند هەیە؟

The four basic statementsچوار فەرمانە سەرەکییەکە

SELECT: read rows (Read)SELECT: خوێندنەوەی تۆمار ⁦(Read)⁩

FormatشێوەSQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
SELECT * FROM table_name
WHERE column1 = 'some_text'
ORDER BY column1, column2 ASC;
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

* means "all columns". FROM names 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 by column2.

ئەنجامەکە ڕیز دەکات: سەرەتا بەپێی column1، پاشان بەپێی column2.

Read the whole table:

هەموو بخوێنەوە:

SQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
SELECT * FROM students;

Choose columns and rows. Instead of *, list the columns you need, and add WHERE:

و هەڵبژێرە. لە جیاتی *، ئەو بنووسە کە پێویستتن و WHERE زیاد بکە:

SQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
SELECT name, marks FROM students WHERE city = 'Erbil';
Common operators in WHERE باوەکان لە WHERE
OperatorMeaningواتاExampleنموونە
=equalیەکسانcity = 'Erbil'
<> or !=not equalیەکسان نییەcity <> 'Erbil'
< > <= >=smaller / greaterبچووکتر / گەورەترmarks >= 80
AND, ORcombine conditionsتێکەڵکردنی مەرجەکانcity = 'Erbil' AND marks > 90
LIKEpattern: % 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 بنەڕەتییە.

SQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
SELECT * FROM students WHERE marks >= 80 ORDER BY marks DESC, name ASC;
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

Highest marks first; when two students have the same mark (92), their names decide the order.

سەرەتا بەرزترین نمرە؛ کاتێک دوو خوێندکار هەمان نمرەیان هەیە ⁦(92)⁩، ناوەکانیان ڕیزبەندییەکە دیاری دەکەن.

SQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
SELECT name, city, marks FROM students ORDER BY city, marks DESC;
Quick checkپشکنینی خێراIn ORDER BY city, marks DESC, how is city sorted?لە ORDER BY city, marks DESCدا، city چۆن ڕیز دەکرێت؟

The four basic statementsچوار فەرمانە سەرەکییەکە

UPDATE: change rows (Update)UPDATE: گۆڕینی تۆمار ⁦(Update)⁩

FormatشێوەSQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
UPDATE table_name
SET column1 = 'some_text'
WHERE id = 1;
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

SET gives the new value. Change several columns with commas: SET city = 'Erbil', marks = 90.

SET نرخە نوێیەکە دەدات. بۆ گۆڕینی چەند کۆما بەکار بهێنە: SET city = 'Erbil', marks = 90.

Lineدێڕی 3

WHERE chooses which rows change. Using the primary key id changes exactly one row.

WHERE دیاری دەکات کام بگۆڕدرێت. بەکارهێنانی id ڕێک یەک دەگۆڕێت.

Karwan's mark was entered wrongly. Fix it, then check:

نمرەی Karwan بە هەڵە نووسرابوو. چاکی بکە، پاشان بیپشکنە:

SQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
UPDATE students SET marks = 81 WHERE id = 3;
SQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
SELECT * FROM students WHERE id = 3;

The four basic statementsچوار فەرمانە سەرەکییەکە

DELETE: remove rows (Delete)DELETE: سڕینەوەی تۆمار ⁦(Delete)⁩

FormatشێوەSQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
DELETE FROM table_name
WHERE id = 1;
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 2

Only the rows that match WHERE are deleted.

تەنها ئەو دەسڕدرێنەوە کە لەگەڵ WHERE دەگونجێن.

SQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
DELETE FROM students WHERE id = 5;
SQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
SELECT * FROM students;
Explain the codeڕوونکردنەوەی کۆدەکە
Lineدێڕی 1

Rezan (id 5) is gone. The next new row will get id 6, not 5: AUTO_INCREMENT does not reuse numbers.

⁦Rezan (id 5)⁩ نەما. نوێی داهاتوو ⁦id 6⁩ وەردەگرێت، نەک 5: AUTO_INCREMENT ژمارەکان دووبارە بەکار ناهێنێتەوە.

Quick checkپشکنینی خێراWhat does DELETE FROM students; do?DELETE FROM students; چی دەکات؟

Common mistakesهەڵە باوەکان

Mistakes to avoid in SQLئەو هەڵانەی لە SQLدا خۆتیان لێ بپارێزە
MistakeهەڵەWhat happensچی ڕوو دەداتFixچارەسەر
WHERE city = ‘Erbil’Syntax error #1064 (curly quotes) ⁦#1064⁩ (کۆتەیشنی چەماوە)WHERE city = 'Erbil'
WHERE city = ErbilMySQL 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 DESCOnly marks is descending; city stays ascendingتەنها marks نزمبوونەوەیە؛ city بەرزبوونەوە دەمێنێتەوەWrite a direction for each column you want reversedبۆ هەر کە دەتەوێت پێچەوانە بێت ئاڕاستە بنووسە
price FLOATRounding 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 studentsWorks 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 دایە.

Exerciseڕاهێنان1. Choose the data types1. جۆرەکانی دراوە هەڵبژێرە

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وەڵامەکە پیشان بدە
SQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
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⁩ دەدات.

Exerciseڕاهێنان2. Write the CRUD statements2. فەرمانەکانی CRUD بنووسە

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وەڵامەکە پیشان بدە
SQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
-- (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'، چونکە دەکرێت دوو خوێندکار هەمان ناویان هەبێت.

Exerciseڕاهێنان3. Spot the mistakes3. هەڵەکان بدۆزەرەوە

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.
  • Duhok is text, so it needs straight single quotes: 'Duhok'.
  • There is no WHERE, so every student would change.
  • کۆتەیشنی چەماوە بە دەوری ⁦95⁩دا: ژمارە هیچ کۆتەیشنێکی پێویست نییە.
  • Duhok دەقە، بۆیە پێویستی بە کۆتەیشنی تاکی ڕاستە: 'Duhok'.
  • WHERE نییە، بۆیە هەموو خوێندکارەکان دەگۆڕێن.
SQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
UPDATE students SET marks = 95, city = 'Duhok' WHERE id = 3;
Exerciseڕاهێنان4. Dates4. بەروارەکان

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یش نرخەکە جێگیر دەهێڵێتەوە، بەڵام بەشی کاتژمێرەکەی پێویست نییە.)

ExerciseڕاهێنانDatabase or spreadsheet?بنکەدراوە یان بەرنامەی خشتەیی؟

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, UNIQUE and 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 دەیپرسن.
ExerciseڕاهێنانA school football tournamentپاڵەوانێتیی تۆپی پێی قوتابخانە

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وەڵامەکە پیشان بدە
SQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
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);
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 (ناوەکان گۆڕدراون):

The table employees employees
employee_idfull_nameageaddressbasic_salarydepartment
100Soran Ahmed26sul825HR
101Hana Kamal34erb560Finance
102Karwan Omar22sul400Customer Service
103Shilan Aziz20sul825IT
104Rezan Salih20erb700Sales
ExerciseڕاهێنانLab 1: no empty valuesتاقیگە 1: هیچ نرخێکی بەتاڵ نەبێت

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وەڵامەکە پیشان بدە
SQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
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);

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 مووچەکان بە وردی دەپارێزێت.

ExerciseڕاهێنانLab 2: a 20 % raiseتاقیگە 2: زیادکردنی ⁦20 %⁩

Increase by 20 % the salary of every employee whose basic salary is below 600.

مووچەی هەموو فەرمانبەرێک کە مووچە بنەڕەتییەکەی لە ⁦600⁩ کەمترە ⁦20 %⁩ زیاد بکە.

Show answerوەڵامەکە پیشان بدە
SQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
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;

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 جێبەجێ بکە، بۆ ئەوەی بزانیت کام دەگۆڕێن.

ExerciseڕاهێنانLab 3: a full city nameتاقیگە 3: ناوی تەواوی شار

Replace every address sul with Sulaymaniyah.

هەموو ناونیشانێکی sul بە Sulaymaniyah بگۆڕە.

Show answerوەڵامەکە پیشان بدە
SQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
UPDATE employees SET address = 'Sulaymaniyah' WHERE address = 'sul';

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 بە ناونیشان، مووچە و نمرەی هەر کەسێکەوە، بۆ هەر وانەیەک . ئەم دروستیان دەکات، بۆ ئەوەی ڕاستەوخۆ بچیتە سەر پرسیارەکان:

SQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
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;
ExerciseڕاهێنانLab 4: what is wrong with t2?تاقیگە 4: 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 نییە و هیچ شتێک ڕێگە لە دووبارەبوونەوە ناگرێت. زیادکردنەکە چوار بۆ دوو کەس دەگۆڕێت:

SQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
UPDATE t2 SET salary = salary * 1.2 WHERE salary < 200;
SELECT id, address, salary, subject FROM t2 ORDER BY id;

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).

ExerciseڕاهێنانLab 5: who failed?تاقیگە 5: کێ دەرنەچووە؟

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وەڵامەکە پیشان بدە
SQLRuns in the databaseلە ناو بنکەدراوە کار دەکات
SELECT DISTINCT t1.id, t1.first_name, t1.last_name
FROM t1
JOIN t2 ON t2.id = t1.id
WHERE t2.marks < 12.5;

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/TINYINT for whole numbers, DECIMAL for money, VARCHAR for short text, DATE/DATETIME/TIMESTAMP for time.
  • TIMESTAMP follows the time zone (UTC inside, range to 2038); DATETIME never changes. Both can be indexed.
  • CREATE DATABASE, USE, CREATE TABLE build the structure; INSERT, SELECT, UPDATE, DELETE work with the rows (CRUD).
  • SHOW TABLES and DESCRIBE show what exists; ALTER TABLE changes a table and keeps its rows; INSERT … SELECT copies rows from another table.
  • Straight quotes around text, a WHERE on every UPDATE and DELETE, and a direction for each ORDER BY column 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 کە دەتەوێت پێچەوانە بێت.
Review questionsپرسیارەکانی پێداچوونەوە
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.ڤیدیۆیەک کە بینەران زۆریان پێ خۆش بووە و لە کۆمێنتەکانیاندا ستایشیان کردووە، بۆ ئەم وانەیە هەڵبژێردراوە.

What is SQL? [in 4 minutes for beginners]Danielle Thé · Englishئینگلیزی · 2.3M viewsبینین · 55K likesلایک

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زاراوە سەرەکییەکان

Next lessonوانەی داهاتوو

Creating a database with phpMyAdminدروستکردنی بنکەدراوە بە phpMyAdmin

References and further readingسەرچاوەکان و خوێندنەوەی زیاتر

This lecture is a first edition. The Kurdish text and terminology are waiting for review by Kurdish IT teachers.ئەم وانەیە چاپی یەکەمە. دەقە کوردییەکە و زاراوەکانی چاوەڕێی پێداچوونەوەی مامۆستایانی کوردی بواری IT ن.