// SELECT * FROM knowledge;

Talking to a database with SQLදත්ත සමුදායක් සමඟ SQL මඟින් කතා කිරීම

SQL is the language used to create data files, store data, remove it, update it, query it and calculate with it. This page covers the whole A/L syllabus — constraints, DDL, DML, functions, grouping, wildcards and joins — with the query and its result side by side.

දත්ත ගොනු සැකසීම, දත්ත ගබඩා කිරීම, ඉවත් කිරීම, යාවත්කාලීන කිරීම, විමසීම සහ ගණනය කිරීම සඳහා භාවිතා කරන භාෂාව SQL වේ. මෙම පිටුව සම්පූර්ණ A/L විෂය නිර්දේශය ආවරණය කරයි — සම්බාධක, DDL, DML, ශ්‍රිත, කාණ්ඩ කිරීම, wildcards සහ join — විමසුම සහ එහි ප්‍රතිඵලය එකට දක්වමින්.

1 DDL · structureව්‍යූහය 2 DML · dataදත්ත 3 Queries & JOINsවිමසුම් සහ JOIN MySQL
▸
How to read this page.මෙම පිටුව කියවන ආකාරය. Each idea shows the general syntax first, then a real example, then the result that MySQL gives back. Use the copy button on any query to try it in MySQL, phpMyAdmin, or an online editor. එක් එක් සංකල්පය සඳහා පළමුව සාමාන්‍ය syntax එක, ඉන්පසු සැබෑ උදාහරණයක්, ඉන්පසු MySQL ලබා දෙන ප්‍රතිඵලය දැක්වේ. ඕනෑම විමසුමක් copy බොත්තමෙන් පිටපත් කර MySQL, phpMyAdmin හෝ online editor එකක උත්සාහ කරන්න.
1

Database Management Systemsදත්ත සමුදාය කළමනාකරණ පද්ධති

A DBMS is a system built to create data files and keep them in a form that is easy to use. It sits between the user and the stored data.

දත්ත ගොනු නිර්මාණය කර එම දත්ත පහසුවෙන් භාවිතා කළ හැකි ලෙස පවත්වාගෙන යාම සඳහා නිර්මාණය කර ඇති පද්ධති DBMS ලෙස හැඳින්වේ.

Facilitiesපහසුකම්

What a DBMS lets you doDBMS එකකින් කළ හැකි දේ

  • Data can be inserted.දත්ත ඇතුළත් කළ හැක.
  • Inserted data can be removed.ඇතුළත් කළ දත්ත ඉවත් කළ හැක.
  • Data can be updated.දත්ත යාවත්කාලීන කළ හැක.
  • Data can be queried in many different ways.විවිධ අවශ්‍යතා අනුව දත්ත විමසීම් සිදු කළ හැක.
  • Data can be kept secure.දත්ත සුරක්ෂිත කළ හැක.
Examplesඋදාහරණ
OracleSybaseSQL‑Server MySQLDb2MS‑Access
Characteristicsලක්ෂණ

What a good DBMS must haveදත්ත කළමනාකරණ පද්ධතියක තිබිය යුතු ලක්ෂණ

  • Security of the data.දත්තවල සුරක්ෂිතතාව.
  • The ability to build software that handles the data.දත්ත හැසිරවීමට මෘදුකාංග සැකසීමේ හැකියාව.
  • The ability for several people to use the data at once.එකවර කිහිප දෙනෙකුට දත්ත භාවිතා කිරීමේ හැකියාව.
  • Keeping the data accurate and consistent.දත්තවල නිවැරදිතාව පවත්වාගෙන යාම.
2

Structured Query Languageව්‍යූහගත විමසුම් භාෂාව

SQL is a language used to set up data files, store data, remove stored data, update it, query it and perform calculations on it. It was introduced by IBM in 1970.

දත්ත ගොනු සැකසීම, දත්ත ගබඩා කිරීම, ගබඩා කළ දත්ත ඉවත් කිරීම, යාවත්කාලීන කිරීම, විමසීම හා දත්ත ගණනය කිරීම ආදී කාර්යයන් ඉටු කර ගැනීමට භාවිතා කළ හැකි භාෂාවකි. 1970 දී IBM සමාගම විසින් හඳුන්වා දෙන ලදී.

Two families of commandsකේත වර්ග 2ක්

DDL and DMLDDL සහ DML

Every SQL statement in the syllabus belongs to one of these two groups.

විෂය නිර්දේශයේ සෑම SQL විධානයක්ම මෙම කාණ්ඩ දෙකෙන් එකකට අයත් වේ.

DDL
Data Definition Language.දත්ත නිර්වචන භාෂාව. Commands used to create databases, create the tables that store data, and change the structure of those tables. දත්ත සමුදායන් නිර්මාණයට, දත්ත ගබඩා කිරීමට අවශ්‍ය වගු නිර්මාණයට හා වගුවල ව්‍යූහය වෙනස් කිරීමට භාවිතා කරන කේත.
CREATE · ALTER · DROP · SHOW · USE
DML
Data Manipulation Language.දත්ත හැසිරවීමේ භාෂාව. Commands used to insert data into a table, remove it, update it, and query it in different ways. වගුවකට දත්ත ඇතුළත් කිරීමට, ඉවත් කිරීමට, යාවත්කාලීන කිරීමට හා විවිධ ආකාරයෙන් විමසීමට භාවිතා කරන කේත.
INSERT · SELECT · UPDATE · DELETE
3

DDL — working with databasesDDL — දත්ත සමුදාය සමඟ වැඩ කිරීම

Before any table exists you need a database to hold it. These four commands create one, list them, open one, and delete one.

වගුවක් තැනීමට පෙර එය රඳවා ගැනීමට දත්ත සමුදායක් අවශ්‍ය වේ. මෙම විධාන හතරෙන් එකක් නිර්මාණය, ලැයිස්තුගත කිරීම, විවෘත කිරීම හා ඉවත් කිරීම සිදු කරයි.

Syntax

Create, list, open, dropනිර්මාණය, ලැයිස්තුව, විවෘත කිරීම, ඉවත් කිරීම

-- create a new database
CREATE DATABASE database_name;

-- see the databases that already exist
SHOW DATABASES;

-- go inside a database before using its tables
USE database_name;

-- delete a database completely
DROP DATABASE database_name;
Exampleඋදාහරණය
CREATE DATABASE school;
USE school;
!
DROP DATABASE removes the database and every table inside it. There is no undo — this is a favourite exam trap. DROP DATABASE මඟින් දත්ත සමුදාය සහ එහි ඇති සියලු වගු ඉවත් වේ. නැවත ලබා ගත නොහැක — මෙය විභාගයේ බහුලව අසන කරුණකි.
4

Data typesදත්ත ප්‍රරූප

Every column must be told what kind of data it will hold. Choosing the right type is what stops letters being typed into a salary column.

සෑම උපලැකියකටම එහි රඳවන දත්ත වර්ගය දැක්විය යුතුය. නිවැරදි ප්‍රරූපය තෝරා ගැනීමෙන් වැටුප් උපලැකියකට අකුරු ඇතුළත් වීම වළක්වයි.

Int(Size)
Positive or negative whole numbers.ධන නිඛිල හෝ ඍණ නිඛිල සංඛ්‍යා සඳහා.
Float(Size)
Numbers with a decimal part.දශම සහිත සංඛ්‍යා සඳහා.
Date
Data that holds a date.දිනයක් සහිත දත්ත සඳහා.
DateTime
Stores a date together with a time.දිනය හා වේලාව තැන්පත් කිරීමට.
Varchar(Size)
Letters, numbers and special characters. Size ranges from 0 to 65535. Only the space actually used is stored.අකුරු, සංඛ්‍යා, විශේෂ සලකුණු සහිත දත්ත සඳහා. Size හි පරාසය 0 සිට 65535 දක්වා.
Char(Size)
Letters, numbers and special characters. Size ranges from 0 to 255. Always reserves the full length.අකුරු, සංඛ්‍යා, විශේෂ සලකුණු සහිත දත්ත සඳහා. Size හි පරාසය 0 සිට 255 දක්වා.
▸
Varchar vs Char. Use Char when the length never changes (a grade, a code like 'S01') and Varchar when it varies (names, addresses). Varchar සහ Char. දිග කිසිදා නොවෙනස් වන විට (ශ්‍රේණියක්, 'S01' වැනි කේතයක්) Char ද, දිග වෙනස් වන විට (නම්, ලිපින) Varchar ද භාවිතා කරන්න.
5

Creating tablesවගු නිර්මාණය කිරීම

Syntax

CREATE TABLECREATE TABLE

CREATE TABLE table_name(column_name data_type(size),
                        column_name data_type(size),
                        column_name data_type(size));
Exampleඋදාහරණය
CREATE TABLE Student(Roll_No int,
                     Stu_Name varchar(40),
                     Address varchar(80));
See the tables in the databaseදත්ත සමුදායේ ඇති වගු බැලීමට
SHOW TABLES;
From another tableවෙනත් වගුවකින්

Building a new table out of an existing oneපවතින වගුවක් උපයෝගී කරගෙන නව වගුවක්

Copy some columns of a table (optionally only the rows that match a condition) into a brand new table.

වගුවක ක්ෂේත්‍ර කිහිපයක් (අවශ්‍ය නම් කොන්දේසියකට ගැළපෙන පේළි පමණක්) නව වගුවකට පිටපත් කිරීම.

-- keep the same column names
CREATE TABLE new_table_name AS
    SELECT column_name, column_name, ...
    FROM existing_table_name WHERE condition;

-- give the new table its own column names
CREATE TABLE new_table_name(new_column_name, new_column_name) AS
    SELECT column_name, column_name, ...
    FROM existing_table_name WHERE condition;
Exampleඋදාහරණය
CREATE TABLE accounts_staff AS
    SELECT emp_no, name, salary
    FROM employee WHERE department = 'Accounts';
6

Changing a table — ALTER & DROPවගුවක් වෙනස් කිරීම — ALTER සහ DROP

Once a table exists you can still add columns, remove them, change their data type, or throw the whole table away.

වගුවක් නිර්මාණය කළ පසුවත් ක්ෂේත්‍ර එකතු කිරීම, ඉවත් කිරීම, දත්ත වර්ගය වෙනස් කිරීම හෝ මුළු වගුවම ඉවත් කිරීම කළ හැක.

Syntax

The ALTER familyALTER විධාන

-- add a new column
ALTER TABLE table_name ADD column_name data_type(size);

-- remove a column
ALTER TABLE table_name DROP COLUMN column_name;

-- change the data type of an existing column
ALTER TABLE table_name MODIFY COLUMN column_name data_type(size);

-- remove the primary key
ALTER TABLE table_name DROP PRIMARY KEY;

-- remove a foreign key
ALTER TABLE table_name DROP FOREIGN KEY column_name;

-- delete the whole table
DROP TABLE table_name;
Exampleඋදාහරණය
ALTER TABLE employee ADD email varchar(60);
ALTER TABLE employee MODIFY COLUMN gender varchar(10);
ALTER TABLE Marks DROP FOREIGN KEY Index_No;
!
DROP removes the structure itself. DELETE (in the DML section) removes only rows and leaves the table standing. Do not mix them up. DROP මඟින් ව්‍යූහයම ඉවත් වේ. DELETE (DML කොටසේ) මඟින් පේළි පමණක් ඉවත් වන අතර වගුව එසේම පවතී. මේ දෙක පටලවා නොගන්න.
7

Constraintsසම්බාධක

A constraint is a rule that controls what may be entered into a table. Without them a table quietly fills up with rubbish.

වගුවක් තුළට පාලනයකින් යුක්තව දත්ත ඇතුළත් කිරීම සඳහා භාවිතා කරන ක්‍රමවේදයන් සම්බාධක ලෙස හඳුන්වයි.

Why they matterවැදගත්කම

Faults in a table built without constraintsසම්බාධක නොමැතිව නිර්මාණය කරන වගුවල පැවතිය හැකි දෝෂ

  • The same data being entered again and again (data duplication).එකම දත්තය නැවත නැවත ඇතුළත් වීම (දත්ත පුනර්කරණය).
  • Character data being entered into a column meant for numbers.සංඛ්‍යා දත්ත ඇතුළත් විය යුතු උපලැකියකට අක්ෂර දත්ත ඇතුළත් වීම.
  • Data falling outside the range it should be in.කිසියම් දත්තයක් ඇතුළත් විය යුතු පරාසයෙන් පිට පැවතීම.
  • Empty data in a column where a value is compulsory.අනිවාර්යයෙන් දත්ත ඇතුළත් විය යුතු උපලැකියක හිස් දත්ත පැවතීම.
NOT NULL

Not-null constraintඅභිශූන්‍ය නොවන සම්බාධක

Stops empty values being entered into a column.

දත්ත ඇතුළත් කිරීමේදී හිස් දත්ත ඇතුළත් වීම වැළැක්වීම සඳහා යොදන සම්බාධකයි.

CREATE TABLE Student(Roll_No int NOT NULL,
                     Stu_Name varchar(40) NOT NULL,
                     Address varchar(80));
UNIQUE

Unique constraintඅනන්‍ය සම්බාධක

Every value entered in that column must be different from the others — it prevents the same value appearing twice.

වගුවක උපලැකියකට ඇතුළත් කරන දත්ත එකිනෙකට අනන්‍ය වීම සඳහා යොදන සම්බාධකයි (එකම දත්තය කිහිප වතාවක් ඇතුළත් වීම වළක්වයි).

CREATE TABLE Student(Roll_No int UNIQUE,
                     Stu_Name varchar(40) UNIQUE,
                     Address varchar(80));
PRIMARY KEY

Primary key constraintප්‍රාථමික යතුරු සම්බාධක

A primary key is a column used to identify each row of a table uniquely. It contains both NOT NULL and UNIQUE inside it.

ප්‍රාථමික යතුරක් යනු වගුවක එක් එක් උපලැකියේ අඩංගු දත්ත අනන්‍යව හඳුනා ගැනීම සඳහා භාවිතා කරන උපලැකියකි. ප්‍රාථමික යතුරු සම්බාධකයේ NOT NULL හා UNIQUE යන සම්බාධක දෙකම අඩංගුව පවතී.

Method 1 — inlineතීරුව සමඟම
CREATE TABLE Student(Roll_No int PRIMARY KEY,
                     Stu_Name varchar(40),
                     Address varchar(80));
Method 2 — declared at the endඅවසානයේ දක්වා
CREATE TABLE Student(Roll_No int,
                     Stu_Name varchar(40),
                     Address varchar(80),
                     PRIMARY KEY(Roll_No));
FOREIGN KEY

Foreign key constraintආගන්තුක යතුරු සම්බාධක

Used to create a relationship between two tables by naming a column of one table as a foreign key pointing at another table.

වගු දෙකක් අතර සම්බන්ධතාවය ඇති කිරීම සඳහා එක් වගුවක උපලැකියක් ආගන්තුක යතුරක් ලෙස නම් කිරීමට භාවිතා කරයි.

CREATE TABLE Marks(Index_No int,
                   Subject varchar(40),
                   Mark varchar(5),
                   FOREIGN KEY(Index_No) REFERENCES Student(Stu_No));
Index_No
the column in this table being named as the foreign keyආගන්තුක යතුර ලෙස නම් කරන උපලැකියේ නම
Student
the name of the other tableඅනෙක් වගුවෙහි නම
Stu_No
the matching column as it is named in that other tableඑම වගුවේ දක්වා ඇති අනුරූප උපලැකියේ නම
CHECK

Table check constraintවගු පරීක්ෂා සම්බාධක

Data is only allowed in if it satisfies the condition you give.

වගුවක උපලැකියකට ඇතුළත් කරන දත්ත දී ඇති කොන්දේසියට අනුකූලව පවතින්නේ නම් පමණක් ඇතුළත් කිරීමට ඉඩ සලසයි.

CREATE TABLE Student(Roll_No int CHECK(Roll_No > 1000),
                     Stu_Name varchar(40),
                     Address varchar(80));
8

Our demo tablesඋදාහරණ වගු

Every query from here on runs against these tables, so you can always check the answer against the data.

මෙතැන් සිට සෑම විමසුමක්ම මෙම වගු මත ක්‍රියා කරයි, එබැවින් පිළිතුර දත්ත සමඟ සැම විටම පරීක්ෂා කළ හැක.

Table 1

employee

emp_nonamedepartmentsalary
1AmaraAccounts15000
2KumariSales18000
3FernandoAccounts22000
4KamalManagement17000
5NimaliSales16000
CREATE TABLE employee(emp_no int PRIMARY KEY,
                      name varchar(40) NOT NULL,
                      department varchar(30),
                      salary int);
Table 2 & 3

Student & Marks

Used later for the JOIN examples. Marks.Index_No is a foreign key onto Student.Stu_No.

පසුව JOIN උදාහරණ සඳහා භාවිතා වේ. Marks.Index_No යනු Student.Stu_No වෙත යොමු වන ආගන්තුක යතුරයි.

Student
Stu_NoStu_NameAddress
S01KamalColombo
S02NimalGampaha
S03SeethaKalutara
Marks
Index_NoSubjectMark
S01ICT75
S01Maths68
S02ICT82
S04Science55
▸
Notice S03 has no marks and S04 has marks but is not a student. Those two odd rows are exactly what makes LEFT and RIGHT JOIN easy to see. S03 හට ලකුණු නොමැති බවත් S04 හට ලකුණු ඇතත් ශිෂ්‍යයෙකු නොවන බවත් සලකන්න. LEFT සහ RIGHT JOIN පැහැදිලිව දැකීමට මෙම පේළි දෙක උපකාරී වේ.
9

DML — INSERTDML — දත්ත ඇතුළත් කිරීම

Putting rows into a table. Several rows can be added in one statement by separating each bracket with a comma.

වගුවකට දත්ත ඇතුළත් කිරීම. එක් විධානයකින් පේළි කිහිපයක් එකතු කළ හැක — එක් එක් වරහන කොමාවකින් වෙන් කරන්න.

Syntax

INSERT INTO

INSERT INTO table_name(column_name1, column_name2, column_name3)
VALUES (DataForColumn1, DataForColumn2, DataForColumn3),
       (DataForColumn1, DataForColumn2, DataForColumn3),
       (DataForColumn1, DataForColumn2, DataForColumn3);
Exampleඋදාහරණය
INSERT INTO employee(emp_no, name, department, salary)
VALUES (1, 'Amara', 'Accounts', 15000),
       (2, 'Kumari', 'Sales', 18000),
       (3, 'Fernando', 'Accounts', 22000);
▸
Text and dates go inside single quotes; numbers do not. අකුරු හා දින තනි උද්ධෘත තුළ ලිවිය යුතුය; සංඛ්‍යා එසේ නොවේ.
10

SELECT — reading dataSELECT — දත්ත විමසීම

Chosen columnsක්ෂේත්‍ර කිහිපයක්

Only some columnsක්ෂේත්‍ර කිහිපයක අඩංගු දත්ත බලා ගැනීමට

SELECT column_name, column_name, ... FROM table_name;
Exampleඋදාහරණය
SELECT name, salary FROM employee;
Resultප්‍රතිඵලය
namesalary
Amara15000
Kumari18000
Fernando22000
Kamal17000
Nimali16000
All columnsසියලු ක්ෂේත්‍ර

The star means "all columns"* මඟින් All Columns අඟවයි

SELECT * FROM table_name;
Exampleඋදාහරණය
SELECT * FROM employee;
Result — the whole tableප්‍රතිඵලය — මුළු වගුවම
emp_nonamedepartmentsalary
1AmaraAccounts15000
2KumariSales18000
3FernandoAccounts22000
4KamalManagement17000
5NimaliSales16000
11

WHERE — asking with a conditionකොන්දේසි සහිතව දත්ත විමසීම

Syntax

WHERE

SELECT * FROM table_name WHERE condition;
-- or
SELECT column_name1, column_name2, ... FROM table_name WHERE condition;
Only the Accounts department staffගිණුම් අංශයේ සේවකයින්ගේ දත්ත පමණක්
SELECT * FROM employee WHERE department = 'Accounts';
emp_nonamedepartmentsalary
1AmaraAccounts15000
3FernandoAccounts22000
AND · OR · BETWEEN

Combining conditionsකොන්දේසි එකතු කිරීම

AND
Shows only the data for which all the conditions separated by AND are true.AND වලින් වෙන් වී ඇති Condition සියල්ල සත්‍ය වන දත්ත පමණක් පෙන්වයි.
OR
Shows the data for which at least one of the conditions separated by OR is true.OR වලින් වෙන් වී ඇති Condition වලින් එකක් හෝ සත්‍ය වන දත්ත පෙන්වයි.
BETWEEN
Used to take data inside a range of numbers (both ends included).සංඛ්‍යා පරාසයක් තුළ දත්ත ලබා ගැනීමට භාවිතා කරයි (කෙළවර දෙකම ඇතුළත්ව).
Accounts staff earning more than 15000ගිණුම් අංශයේ වැටුප 15000ට වැඩි සේවකයින්
SELECT * FROM employee
WHERE department = 'Accounts' AND salary > 15000;
emp_nonamedepartmentsalary
3FernandoAccounts22000
Staff in Accounts or Salesගිණුම් අංශයේ හෝ විකුණුම් අංශයේ සේවකයින්
SELECT * FROM employee
WHERE department = 'Accounts' OR department = 'Sales';
emp_nonamedepartmentsalary
1AmaraAccounts15000
2KumariSales18000
3FernandoAccounts22000
5NimaliSales16000
Salary between 17000 and 20000වැටුප 17000ත් 20000ත් අතර සේවකයින්
SELECT * FROM employee WHERE salary BETWEEN 17000 AND 20000;
emp_nonamedepartmentsalary
2KumariSales18000
4KamalManagement17000
12

LIMIT

Shows only the first few rows of a table.

වගුවක ඇති පළමු දත්ත කිහිපය පමණක් බලා ගැනීමට.

SELECT * FROM table_name LIMIT number;
Exampleඋදාහරණය
SELECT * FROM employee LIMIT 3;
emp_nonamedepartmentsalary
1AmaraAccounts15000
2KumariSales18000
3FernandoAccounts22000
13

Aggregate functionsශ්‍රිත

These five functions squeeze a whole column down into one answer.

මෙම ශ්‍රිත පහෙන් මුළු උපලැකියක්ම එක් පිළිතුරකට ලඝු කරයි.

AVG( )
Used to get the average value of the data.දත්තවල මධ්‍යන්‍ය අගය ලබා ගැනීමට භාවිතා කරයි.
COUNT( )
Used to get the number of rows.පේළි ගණන ලබා ගැනීමට භාවිතා කරයි.
MAX( )
Used to get the largest value.විශාලම අගය ලබා ගැනීමට භාවිතා කරයි.
MIN( )
Used to get the smallest value.කුඩාම අගය ලබා ගැනීමට භාවිතා කරයි.
SUM( )
Used to get the total of numeric data.සංඛ්‍යා දත්තවල එකතුව ලබා ගැනීමට භාවිතා කරයි.
Examplesඋදාහරණ

Working on the salary columnවැටුප් උපලැකිය මත

-- total of all salaries
SELECT SUM(salary) FROM employee;                 -- 88000

-- the same thing, but name the answer column "Total"
SELECT SUM(salary) AS Total FROM employee;        -- Total = 88000

-- average salary
SELECT AVG(salary) FROM employee;                 -- 17600

-- highest and lowest salary
SELECT MAX(salary) FROM employee;                 -- 22000
SELECT MIN(salary) FROM employee;                 -- 15000

-- how many rows are in the salary column
SELECT COUNT(salary) FROM employee;               -- 5

-- total of the salaries above 16000 only
SELECT SUM(salary) FROM employee WHERE salary > 16000;   -- 57000
▸
AS renames the answer column. It is called an alias and it makes results far easier to read. AS මඟින් ප්‍රතිදාන උපලැකියට නමක් ලබා දේ. මෙය alias ලෙස හැඳින්වේ; ප්‍රතිඵලය කියවීම පහසු කරයි.
14

ORDER BY — sortingදත්ත අනුපිළිවෙළට ලබා ගැනීම

Ascending orderආරෝහණ පිළිවෙළට (Ascending Order)
SELECT * FROM table_name ORDER BY column_name;
-- or, written out in full
SELECT * FROM table_name ORDER BY column_name ASC;
Descending orderඅවරෝහණ පිළිවෙළට (Descending Order)
SELECT * FROM table_name ORDER BY column_name DESC;
Example — salaries, biggest firstඋදාහරණය — වැඩිම වැටුප මුලින්
SELECT name, salary FROM employee ORDER BY salary DESC;
namesalary
Fernando22000
Kumari18000
Kamal17000
Nimali16000
Amara15000
▸
If you leave out ASC or DESC, MySQL sorts ascending by default. ASC හෝ DESC නොදැක්වුවහොත් MySQL පෙරනිමියෙන් ආරෝහණ පිළිවෙළට සකසයි.
15

GROUP BY — grouping dataදත්ත කාණ්ඩ කිරීම

GROUP BY collects rows that share a value and lets an aggregate function answer per group instead of once for the whole table.

GROUP BY මඟින් එකම අගයක් ඇති පේළි එකතු කර, මුළු වගුවටම එක් පිළිතුරක් වෙනුවට එක් එක් කාණ්ඩය සඳහා පිළිතුරක් ලබා දේ.

SELECT column1, column2, ... FROM table_name GROUP BY column_name;
Total salary of each department, separatelyඑක් එක් අංශවල සේවකයින්ගේ වැටුප්වල එකතුව වෙන වෙනම
SELECT department, SUM(salary) AS Total
FROM employee GROUP BY department;
departmentTotal
Accounts37000
Management17000
Sales34000
Each student's total marks, with their nameඑක් එක් සිසුවාගේ ලකුණුවල එකතුව නම සමඟ
SELECT Index_No, SUM(Mark) AS Total
FROM Marks GROUP BY Index_No;
Index_NoTotal
S01143
S0282
S0455
16

DISTINCT

Shows each different value only once — repeats are thrown away.

එකිනෙකට වෙනස් දත්ත බලා ගැනීමට — නැවත නැවත එන අගයන් ඉවත් වේ.

SELECT DISTINCT column_name1, column_name2, ... FROM table_name;
What departments are there?දෙපාර්තමේන්තු මොනවාද?
SELECT DISTINCT department FROM employee;
department
Accounts
Sales
Management
▸
Without DISTINCT the same query returns 5 rows (Accounts and Sales appear twice). With DISTINCT it returns 3. DISTINCT නොමැතිව එම විමසුම පේළි 5ක් ලබා දේ (Accounts සහ Sales දෙවරක් එයි). DISTINCT සමඟ 3ක් ලබා දේ.
17

LIKE and wildcardsLIKE සහ Wildcard Characters

LIKE searches for a pattern instead of an exact value. The % sign stands for "any characters at all".

LIKE මඟින් නිශ්චිත අගයක් වෙනුවට රටාවක් සොයයි. % ලකුණින් කියවෙන්නේ "ඕනෑම අකුරු" යන්නයි.

SELECT column1, column2, ... FROM table_name WHERE column LIKE pattern;
'A%'
The first letter must be A; the percent sign after it means the rest can be anything.මුලින්ම තිබෙන්නේ A අකුරයි. ඉන්පසු ඇති ප්‍රතිශත සලකුණින් කියවෙන්නේ අනිත් අකුරු ඕනෑම අකුරක් විය හැකිය යන්නයි.
'%i'
The last letter must be i; whatever comes before it does not matter.රටාවේ අවසානයට තිබෙන්නේ i අකුරයි. ඊට පෙර ඇති අකුරු ඕනෑම අකුරක් විය හැක.
'%ma%'
ma appears somewhere in the middle — anywhere at all.නමෙහි මැද කොතැනක හෝ ma තිබිය යුතුය.
Names starting with AA අකුරින් නම පටන් ගන්නා සේවකයින්
SELECT * FROM employee WHERE name LIKE 'A%';
emp_nonamedepartmentsalary
1AmaraAccounts15000
Names ending with ii අකුරින් නම අවසන් වන සේවකයින්
SELECT * FROM employee WHERE name LIKE '%i';
emp_nonamedepartmentsalary
2KumariSales18000
5NimaliSales16000
Names with "ma" somewhere insideනමෙහි මැද කොතැනක හෝ "ma" ඇති සේවකයින්
SELECT * FROM employee WHERE name LIKE '%ma%';
emp_nonamedepartmentsalary
1AmaraAccounts15000
4KamalManagement17000
18

UPDATE — changing dataවගුවක අඩංගු දත්ත සංශෝධනය කිරීම

UPDATE table_name SET column1 = value1, column2 = value2, ...
WHERE condition;
Change Perera's name to Senaratneපෙරේරාගේ නම සේනාරත්න ලෙස වෙනස් කිරීමට
UPDATE employee SET name = 'Senaratne' WHERE name = 'Perera';
Add 3000 to employee number 2's salaryඅංක 2 සේවකයාගේ වැටුපට 3000ක් එකතු කිරීමට
UPDATE employee SET salary = salary + 3000 WHERE emp_no = 2;
Move every Accounts employee to Managementගිණුම් අංශයේ සියලුම සේවකයින්ගේ අංශය කළමනාකරණ ලෙස වෙනස් කිරීමට
UPDATE employee SET department = 'Management'
WHERE department = 'Accounts';
!
Forget the WHERE and you change every row. Always write the WHERE clause first, then the SET. WHERE අමතක වුවහොත් සෑම පේළියක්ම වෙනස් වේ. සැම විටම WHERE කොටස මුලින් ලියා පසුව SET ලියන්න.
19

DELETE — removing rowsවගුවක ඇති දත්ත ඉවත් කිරීම

DELETE FROM table_name WHERE condition;
Remove employee number 1සේවක අංක 1 අයත් සේවකයාගේ දත්ත ඉවත් කිරීමට
DELETE FROM employee WHERE emp_no = 1;
Remove all Accounts staffගිණුම් අංශයේ සේවකයින්ගේ දත්ත ඉවත් කිරීමට
DELETE FROM employee WHERE department = 'Accounts';
!
DELETE FROM employee; with no WHERE empties the entire table. The table structure survives, but every row is gone. WHERE නොමැති DELETE FROM employee; මඟින් මුළු වගුවම හිස් වේ. වගුවේ ව්‍යූහය පවතී, නමුත් සියලු පේළි ඉවත් වේ.
20

JOINs — querying across tablesවගු කිහිපයක සම්බන්ධතා මත දත්ත විමසීම

A JOIN matches rows of one table against rows of another using a column they share. All three examples below use Student and Marks.

JOIN එකකින් වගු දෙකක් ඒවා අතර පොදු උපලැකියක් භාවිතයෙන් ගළපයි. පහත උදාහරණ තුනම Student සහ Marks වගු භාවිතා කරයි.

INNER JOIN

Only what is common to bothවගු දෙකටම පොදු දත්ත පමණි

Returns only the data belonging to values that appear in the matching columns of both tables.

වගු දෙකෙහි සමාන උපලැකි දෙකෙහි පොදුවේ ඇති දත්තවලට අයත් දත්ත පමණක් ලබා ගත හැක.

SELECT table1.column1, column2, table2.column1, column2
FROM table1 INNER JOIN table2
ON table1.common_column_name = table2.common_column_name;
Exampleඋදාහරණය
SELECT Student.Stu_No, Student.Stu_Name, Marks.Subject, Marks.Mark
FROM Student INNER JOIN Marks
ON Student.Stu_No = Marks.Index_No;
Stu_NoStu_NameSubjectMark
S01KamalICT75
S01KamalMaths68
S02NimalICT82
▸
Seetha (S03) is missing because she has no marks, and S04 is missing because there is no such student. සීතා (S03) හට ලකුණු නොමැති නිසාත්, S04 එවැනි ශිෂ්‍යයෙකු නොමැති නිසාත් ඒවා ප්‍රතිදානයේ නැත.
LEFT JOIN

Everything on the leftවම් පස වගුවේ සියල්ල

Shows all the data in the left table, together with the matching data from the right table. Where the right table has no match, NULL is shown.

වම් පස දක්වන වගුවෙහි ඇති සියලු දත්ත සමඟ ඒවාට පොදු දකුණු පස වගුවේ දත්ත පෙන්වයි. වම් පස දත්තවලට පොදු දත්ත දකුණු පස නොමැති නම් එම ස්ථානයේ NULL ලෙස පෙන්වයි.

SELECT table1.column1, column2, table2.column1, column2
FROM left_table LEFT JOIN right_table
ON table1.common_column_name = table2.common_column_name;
Exampleඋදාහරණය
SELECT Student.Stu_No, Student.Stu_Name, Marks.Subject, Marks.Mark
FROM Student LEFT JOIN Marks
ON Student.Stu_No = Marks.Index_No;
Stu_NoStu_NameSubjectMark
S01KamalICT75
S01KamalMaths68
S02NimalICT82
S03SeethaNULLNULL
RIGHT JOIN

Everything on the rightදකුණු පස වගුවේ සියල්ල

The mirror image: all the data in the right table, plus the matching data from the left. Missing left-hand data shows as NULL.

දකුණු පස දක්වන වගුවෙහි ඇති සියලු දත්ත සමඟ ඒවාට පොදු වම් පස වගුවේ දත්ත පෙන්වයි. දකුණු පස දත්තවලට පොදු දත්ත වම් පස නොමැති නම් එම ස්ථානයේ NULL ලෙස පෙන්වයි.

SELECT table1.column1, column2, table2.column1, column2
FROM left_table RIGHT JOIN right_table
ON table1.common_column_name = table2.common_column_name;
Exampleඋදාහරණය
SELECT Student.Stu_No, Student.Stu_Name, Marks.Subject, Marks.Mark
FROM Student RIGHT JOIN Marks
ON Student.Stu_No = Marks.Index_No;
Stu_NoStu_NameSubjectMark
S01KamalICT75
S01KamalMaths68
S02NimalICT82
NULLNULLScience55
21

UNION

UNION stacks the results of several queries into one output. It returns distinct values only — if you want duplicates as well, write UNION ALL.

මෙමඟින් වගු කිහිපයක ඇති දත්ත එකට ප්‍රතිදානය කළ හැක. මෙමඟින් ප්‍රතිදානය වන්නේ Distinct values පමණි. Duplicate values ද ප්‍රතිදානය කරගැනීමට අවශ්‍ය නම් UNION යන්න UNION ALL ලෙස දැක්විය යුතුය.

SELECT column_name FROM table1
UNION
SELECT column_name FROM table2;

SELECT column_name FROM table1
UNION ALL
SELECT column_name FROM table2;
Rulesනීති
  • The number of columns on both sides of UNION must be the same.UNION දෙපස දක්වන උපලැකි ප්‍රමාණය සමාන විය යුතුය.
  • Their data types must also match.ඒවායේ දත්ත ප්‍රරූපද සමාන විය යුතුය.
Exampleඋදාහරණය
SELECT Stu_Name FROM Student
UNION
SELECT name FROM employee;
22

Exam cheat sheetවිභාග cheat sheet

Everything on one screen for the night before the paper.

විභාගයට පෙර දින සඳහා සියල්ල එක් තිරයක.

DDL

Structure commandsව්‍යූහ විධාන

CREATE DATABASE name;            SHOW DATABASES;
USE name;                        DROP DATABASE name;
SHOW TABLES;                     DROP TABLE name;

CREATE TABLE t(col type(size), col type(size));

ALTER TABLE t ADD col type(size);
ALTER TABLE t DROP COLUMN col;
ALTER TABLE t MODIFY COLUMN col type(size);
ALTER TABLE t DROP PRIMARY KEY;
ALTER TABLE t DROP FOREIGN KEY col;
Constraints

The five rulesසම්බාධක පහ

col int NOT NULL          -- no empty values
col int UNIQUE            -- no repeated values
col int PRIMARY KEY       -- NOT NULL + UNIQUE together
PRIMARY KEY(col)          -- the same, declared at the end
FOREIGN KEY(col) REFERENCES other_table(other_col)
col int CHECK(col > 1000) -- must satisfy a condition
DML

Data commandsදත්ත විධාන

INSERT INTO t(c1, c2) VALUES (v1, v2), (v1, v2);

SELECT * FROM t;
SELECT c1, c2 FROM t WHERE condition;
SELECT * FROM t WHERE c1 = 'x' AND c2 > 10;
SELECT * FROM t WHERE c BETWEEN 10 AND 20;
SELECT * FROM t WHERE c LIKE 'A%';
SELECT DISTINCT c FROM t;
SELECT * FROM t LIMIT 5;
SELECT * FROM t ORDER BY c DESC;
SELECT c, SUM(x) AS Total FROM t GROUP BY c;

UPDATE t SET c1 = v1 WHERE condition;
DELETE FROM t WHERE condition;
Joins

Across tablesවගු අතර

SELECT ... FROM a INNER JOIN b ON a.k = b.k;   -- common rows only
SELECT ... FROM a LEFT  JOIN b ON a.k = b.k;   -- all of a, NULLs on the right
SELECT ... FROM a RIGHT JOIN b ON a.k = b.k;   -- all of b, NULLs on the left

SELECT c FROM a UNION     SELECT c FROM b;     -- distinct values
SELECT c FROM a UNION ALL SELECT c FROM b;     -- keeps duplicates
▸
Want to practise live? Type any of these queries into the W3Schools SQL editor at w3schools.com/sql — it runs a real database in the browser and shows the result instantly. සජීවීව පුහුණු වීමට අවශ්‍යද? මෙම විමසුම් ඕනෑම එකක් w3schools.com/sql හි SQL editor එකට ඇතුළත් කරන්න — එය browser එකේම සැබෑ දත්ත සමුදායක් ධාවනය කර ප්‍රතිඵලය වහාම පෙන්වයි.