DEV Community

Cover image for SQL tilida ma'lumot kiritish, tahrirlash, o'chirish va o'qish.
Azizbek Tursunov
Azizbek Tursunov

Posted on

SQL tilida ma'lumot kiritish, tahrirlash, o'chirish va o'qish.

Bugun SQLda oddiy CRUD (Create, Read, Update, Delete) query, yani so'rov yozamiz. Bu ma'lumotlar bazasida bajarilishi mumkin bo'lgan asosiy operatsiyalardir.

Ma'lumot kiritish (Insert)

INSERT INTO jadval_nomi 
(ustun1, ustun2, ustun3) 
VALUES 
(qiymat1, qiymat2, qiymat3);
Enter fullscreen mode Exit fullscreen mode

O'qish (Select)

SELECT ustun1, ustun2, ustun3 FROM jadval_nomi;
Enter fullscreen mode Exit fullscreen mode

Yoki jadvaldagi barcha ma'lumotni o'qish quyidagicha ko'rinishda bo'ladi:

SELECT * FROM jadval_nomi;
Enter fullscreen mode Exit fullscreen mode

Yangilash (Update)

UPDATE jadval_nomi 
SET ustun1 = yangi_qiymat1, ustun2 = yangi_qiymat2 
WHERE ustun = qiymat;
Enter fullscreen mode Exit fullscreen mode

O'chirish (Delete)

DELETE FROM jadval_nomi 
WHERE ustun = qiymat;
Enter fullscreen mode Exit fullscreen mode

UPDATE va DELETE dagi WHERE bandiga e'tibor bering. WHERE bandi qaysi qator(lar)ni yangilash/o'chirish kerakligini belgilaydi. Agar siz WHERE bandini yozmasangiz, jadvaldagi barcha qatorlar yangilanadi yoki o'chib ketadi!


Keling endi bitta misolda ko'rib chiqsak.

'Talabalar' nomli jadval yaratamiz, uning talaba_id, ism, familiya, fakultet nomli ustunlari bo'lsin:

CREATE TABLE Talabalar (
    talaba_id INT,
    ism VARCHAR(255),
    familiya VARCHAR(255),
    fakultet VARCHAR(255)
);
Enter fullscreen mode Exit fullscreen mode

Bu yerda int son, varchar esa matn ma'lumot turlari.
Keling unga ozgina ma'lumot kiritamiz:

INSERT INTO Talabalar
(talaba_id, ism, familiya, fakultet)
VALUES (1, 'Diyorbek', 'Qahramonov', 'Jismoniy madaniyat'),
       (2, 'Mubosher', 'Abdurashidov', 'Amaliy matematika'),
       (3, 'Samandar', 'Tursunov', 'Kiberxavfsizlik');
Enter fullscreen mode Exit fullscreen mode

Ma'lumot kiritdik. Endi buni ko'rish uchun quyidagicha kod yozamiz:

SELECT * FROM Talabalar;
Enter fullscreen mode Exit fullscreen mode

Biz bu yerda 'Talabalar' jadvalidagi barcha ma'lumotni ko'rsat demoqdamiz.
Natija:

Image description

Hmm, biz ozgina xato ma'lumot kiritganga o'xshaymiz. Mubosherni boshqa fakultetga yozib qo'yibmiz, keling uni tahrirlaymiz:

UPDATE Talabalar 
SET fakultet = 'Jahon tillar' 
WHERE talaba_id = 2;
Enter fullscreen mode Exit fullscreen mode

Natija:

Image description

Bizga endi Samandar haqidagi ma'lumotlar kerak emas, demak o'chirib tashlaymiz:

DELETE FROM Talabalar 
WHERE talaba_id = 3;
Enter fullscreen mode Exit fullscreen mode

Endi ko'radigan bo'lsak bizda quyidagi natija chiqadi:

Image description

Bu amalga oshirilga amallar oddiy amallar edi. Qolganini o'zim o'rganishim davomida ulashib boraman, rahmat.

Top comments (0)