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 — විමසුම සහ එහි ප්රතිඵලය එකට දක්වමින්.
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 ලෙස හැඳින්වේ.
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.දත්ත සුරක්ෂිත කළ හැක.
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.දත්තවල නිවැරදිතාව පවත්වාගෙන යාම.
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 සමාගම විසින් හඳුන්වා දෙන ලදී.
DDL and DMLDDL සහ DML
Every SQL statement in the syllabus belongs to one of these two groups.
විෂය නිර්දේශයේ සෑම SQL විධානයක්ම මෙම කාණ්ඩ දෙකෙන් එකකට අයත් වේ.
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.
වගුවක් තැනීමට පෙර එය රඳවා ගැනීමට දත්ත සමුදායක් අවශ්ය වේ. මෙම විධාන හතරෙන් එකක් නිර්මාණය, ලැයිස්තුගත කිරීම, විවෘත කිරීම හා ඉවත් කිරීම සිදු කරයි.
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;
CREATE DATABASE school; USE school;
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.
සෑම උපලැකියකටම එහි රඳවන දත්ත වර්ගය දැක්විය යුතුය. නිවැරදි ප්රරූපය තෝරා ගැනීමෙන් වැටුප් උපලැකියකට අකුරු ඇතුළත් වීම වළක්වයි.
Creating tablesවගු නිර්මාණය කිරීම
CREATE TABLECREATE TABLE
CREATE TABLE table_name(column_name data_type(size),
column_name data_type(size),
column_name data_type(size));CREATE TABLE Student(Roll_No int,
Stu_Name varchar(40),
Address varchar(80));SHOW TABLES;
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;CREATE TABLE accounts_staff AS
SELECT emp_no, name, salary
FROM employee WHERE department = 'Accounts';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.
වගුවක් නිර්මාණය කළ පසුවත් ක්ෂේත්ර එකතු කිරීම, ඉවත් කිරීම, දත්ත වර්ගය වෙනස් කිරීම හෝ මුළු වගුවම ඉවත් කිරීම කළ හැක.
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;
ALTER TABLE employee ADD email varchar(60); ALTER TABLE employee MODIFY COLUMN gender varchar(10); ALTER TABLE Marks DROP FOREIGN KEY Index_No;
Constraintsසම්බාධක
A constraint is a rule that controls what may be entered into a table. Without them a table quietly fills up with rubbish.
වගුවක් තුළට පාලනයකින් යුක්තව දත්ත ඇතුළත් කිරීම සඳහා භාවිතා කරන ක්රමවේදයන් සම්බාධක ලෙස හඳුන්වයි.
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 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 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 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 යන සම්බාධක දෙකම අඩංගුව පවතී.
CREATE TABLE Student(Roll_No int PRIMARY KEY,
Stu_Name varchar(40),
Address varchar(80));CREATE TABLE Student(Roll_No int,
Stu_Name varchar(40),
Address varchar(80),
PRIMARY KEY(Roll_No));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));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));Our demo tablesඋදාහරණ වගු
Every query from here on runs against these tables, so you can always check the answer against the data.
මෙතැන් සිට සෑම විමසුමක්ම මෙම වගු මත ක්රියා කරයි, එබැවින් පිළිතුර දත්ත සමඟ සැම විටම පරීක්ෂා කළ හැක.
employee
| emp_no | name | department | salary |
|---|---|---|---|
| 1 | Amara | Accounts | 15000 |
| 2 | Kumari | Sales | 18000 |
| 3 | Fernando | Accounts | 22000 |
| 4 | Kamal | Management | 17000 |
| 5 | Nimali | Sales | 16000 |
CREATE TABLE employee(emp_no int PRIMARY KEY,
name varchar(40) NOT NULL,
department varchar(30),
salary int);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 වෙත යොමු වන ආගන්තුක යතුරයි.
| Stu_No | Stu_Name | Address |
|---|---|---|
| S01 | Kamal | Colombo |
| S02 | Nimal | Gampaha |
| S03 | Seetha | Kalutara |
| Index_No | Subject | Mark |
|---|---|---|
| S01 | ICT | 75 |
| S01 | Maths | 68 |
| S02 | ICT | 82 |
| S04 | Science | 55 |
DML — INSERTDML — දත්ත ඇතුළත් කිරීම
Putting rows into a table. Several rows can be added in one statement by separating each bracket with a comma.
වගුවකට දත්ත ඇතුළත් කිරීම. එක් විධානයකින් පේළි කිහිපයක් එකතු කළ හැක — එක් එක් වරහන කොමාවකින් වෙන් කරන්න.
INSERT INTO
INSERT INTO table_name(column_name1, column_name2, column_name3)
VALUES (DataForColumn1, DataForColumn2, DataForColumn3),
(DataForColumn1, DataForColumn2, DataForColumn3),
(DataForColumn1, DataForColumn2, DataForColumn3);INSERT INTO employee(emp_no, name, department, salary)
VALUES (1, 'Amara', 'Accounts', 15000),
(2, 'Kumari', 'Sales', 18000),
(3, 'Fernando', 'Accounts', 22000);SELECT — reading dataSELECT — දත්ත විමසීම
Only some columnsක්ෂේත්ර කිහිපයක අඩංගු දත්ත බලා ගැනීමට
SELECT column_name, column_name, ... FROM table_name;
SELECT name, salary FROM employee;
| name | salary |
|---|---|
| Amara | 15000 |
| Kumari | 18000 |
| Fernando | 22000 |
| Kamal | 17000 |
| Nimali | 16000 |
The star means "all columns"* මඟින් All Columns අඟවයි
SELECT * FROM table_name;
SELECT * FROM employee;
| emp_no | name | department | salary |
|---|---|---|---|
| 1 | Amara | Accounts | 15000 |
| 2 | Kumari | Sales | 18000 |
| 3 | Fernando | Accounts | 22000 |
| 4 | Kamal | Management | 17000 |
| 5 | Nimali | Sales | 16000 |
WHERE — asking with a conditionකොන්දේසි සහිතව දත්ත විමසීම
WHERE
SELECT * FROM table_name WHERE condition; -- or SELECT column_name1, column_name2, ... FROM table_name WHERE condition;
SELECT * FROM employee WHERE department = 'Accounts';
| emp_no | name | department | salary |
|---|---|---|---|
| 1 | Amara | Accounts | 15000 |
| 3 | Fernando | Accounts | 22000 |
Combining conditionsකොන්දේසි එකතු කිරීම
SELECT * FROM employee WHERE department = 'Accounts' AND salary > 15000;
| emp_no | name | department | salary |
|---|---|---|---|
| 3 | Fernando | Accounts | 22000 |
SELECT * FROM employee WHERE department = 'Accounts' OR department = 'Sales';
| emp_no | name | department | salary |
|---|---|---|---|
| 1 | Amara | Accounts | 15000 |
| 2 | Kumari | Sales | 18000 |
| 3 | Fernando | Accounts | 22000 |
| 5 | Nimali | Sales | 16000 |
SELECT * FROM employee WHERE salary BETWEEN 17000 AND 20000;
| emp_no | name | department | salary |
|---|---|---|---|
| 2 | Kumari | Sales | 18000 |
| 4 | Kamal | Management | 17000 |
LIMIT
Shows only the first few rows of a table.
වගුවක ඇති පළමු දත්ත කිහිපය පමණක් බලා ගැනීමට.
SELECT * FROM table_name LIMIT number;
SELECT * FROM employee LIMIT 3;
| emp_no | name | department | salary |
|---|---|---|---|
| 1 | Amara | Accounts | 15000 |
| 2 | Kumari | Sales | 18000 |
| 3 | Fernando | Accounts | 22000 |
Aggregate functionsශ්රිත
These five functions squeeze a whole column down into one answer.
මෙම ශ්රිත පහෙන් මුළු උපලැකියක්ම එක් පිළිතුරකට ලඝු කරයි.
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
ORDER BY — sortingදත්ත අනුපිළිවෙළට ලබා ගැනීම
SELECT * FROM table_name ORDER BY column_name; -- or, written out in full SELECT * FROM table_name ORDER BY column_name ASC;
SELECT * FROM table_name ORDER BY column_name DESC;
SELECT name, salary FROM employee ORDER BY salary DESC;
| name | salary |
|---|---|
| Fernando | 22000 |
| Kumari | 18000 |
| Kamal | 17000 |
| Nimali | 16000 |
| Amara | 15000 |
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;
SELECT department, SUM(salary) AS Total FROM employee GROUP BY department;
| department | Total |
|---|---|
| Accounts | 37000 |
| Management | 17000 |
| Sales | 34000 |
SELECT Index_No, SUM(Mark) AS Total FROM Marks GROUP BY Index_No;
| Index_No | Total |
|---|---|
| S01 | 143 |
| S02 | 82 |
| S04 | 55 |
DISTINCT
Shows each different value only once — repeats are thrown away.
එකිනෙකට වෙනස් දත්ත බලා ගැනීමට — නැවත නැවත එන අගයන් ඉවත් වේ.
SELECT DISTINCT column_name1, column_name2, ... FROM table_name;
SELECT DISTINCT department FROM employee;
| department |
|---|
| Accounts |
| Sales |
| Management |
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;
SELECT * FROM employee WHERE name LIKE 'A%';
| emp_no | name | department | salary |
|---|---|---|---|
| 1 | Amara | Accounts | 15000 |
SELECT * FROM employee WHERE name LIKE '%i';
| emp_no | name | department | salary |
|---|---|---|---|
| 2 | Kumari | Sales | 18000 |
| 5 | Nimali | Sales | 16000 |
SELECT * FROM employee WHERE name LIKE '%ma%';
| emp_no | name | department | salary |
|---|---|---|---|
| 1 | Amara | Accounts | 15000 |
| 4 | Kamal | Management | 17000 |
UPDATE — changing dataවගුවක අඩංගු දත්ත සංශෝධනය කිරීම
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
UPDATE employee SET name = 'Senaratne' WHERE name = 'Perera';
UPDATE employee SET salary = salary + 3000 WHERE emp_no = 2;
UPDATE employee SET department = 'Management' WHERE department = 'Accounts';
DELETE — removing rowsවගුවක ඇති දත්ත ඉවත් කිරීම
DELETE FROM table_name WHERE condition;
DELETE FROM employee WHERE emp_no = 1;
DELETE FROM employee WHERE department = 'Accounts';
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 වගු භාවිතා කරයි.
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;
SELECT Student.Stu_No, Student.Stu_Name, Marks.Subject, Marks.Mark FROM Student INNER JOIN Marks ON Student.Stu_No = Marks.Index_No;
| Stu_No | Stu_Name | Subject | Mark |
|---|---|---|---|
| S01 | Kamal | ICT | 75 |
| S01 | Kamal | Maths | 68 |
| S02 | Nimal | ICT | 82 |
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;
SELECT Student.Stu_No, Student.Stu_Name, Marks.Subject, Marks.Mark FROM Student LEFT JOIN Marks ON Student.Stu_No = Marks.Index_No;
| Stu_No | Stu_Name | Subject | Mark |
|---|---|---|---|
| S01 | Kamal | ICT | 75 |
| S01 | Kamal | Maths | 68 |
| S02 | Nimal | ICT | 82 |
| S03 | Seetha | NULL | NULL |
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;
SELECT Student.Stu_No, Student.Stu_Name, Marks.Subject, Marks.Mark FROM Student RIGHT JOIN Marks ON Student.Stu_No = Marks.Index_No;
| Stu_No | Stu_Name | Subject | Mark |
|---|---|---|---|
| S01 | Kamal | ICT | 75 |
| S01 | Kamal | Maths | 68 |
| S02 | Nimal | ICT | 82 |
| NULL | NULL | Science | 55 |
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;
- The number of columns on both sides of UNION must be the same.UNION දෙපස දක්වන උපලැකි ප්රමාණය සමාන විය යුතුය.
- Their data types must also match.ඒවායේ දත්ත ප්රරූපද සමාන විය යුතුය.
SELECT Stu_Name FROM Student UNION SELECT name FROM employee;
Exam cheat sheetවිභාග cheat sheet
Everything on one screen for the night before the paper.
විභාගයට පෙර දින සඳහා සියල්ල එක් තිරයක.
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;
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
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;
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