INSERT INTO sintaksisi
INSERT INTO table_name (column1, column2, column3, ...)
VALUES (value1, value2, value3, ...);
1. Barcha ustunlarga ma'lumot qo'shish
-- Jadval yaratish
CREATE TABLE users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
email TEXT UNIQUE,
age INTEGER
);
-- Ma'lumot qo'shish (barcha ustunlar)
INSERT INTO users VALUES (1, 'Ali Valiyev', '[email protected]', 25);
INSERT INTO users VALUES (2, 'Malika Karimova', '[email protected]', 30);
2. Belgili ustunlarga ma'lumot qo'shish
-- Faqat kerakli ustunlarga
INSERT INTO users (name, email) VALUES ('Bobur Toshmatov', '[email protected]');
-- DEFAULT qiymatlar avtomatik qo'shiladi
INSERT INTO users (name, email, age) VALUES ('Zarina Qodirova', '[email protected]', 28);
3. Bir nechta qator qo'shish
-- Bir vaqtda ko'p qator qo'shish
INSERT INTO users (name, email, age) VALUES
('Ahmad Karimov', '[email protected]', 35),
('Malika Toshmatova', '[email protected]', 22),
('Bobur Valiyev', '[email protected]', 29);
4. DEFAULT qiymatlar bilan ishlash
-- DEFAULT qiymatlar bilan jadval
CREATE TABLE products (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
price REAL DEFAULT 0.0,
is_active BOOLEAN DEFAULT 1,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
-- DEFAULT qiymatlarni ishlatish
INSERT INTO products (name) VALUES ('Laptop');
-- price=0.0, is_active=1, created_at=CURRENT_TIMESTAMP avtomatik qo'shiladi
-- DEFAULT qiymatlarni bekor qilish
INSERT INTO products (name, price, is_active) VALUES ('Phone', 500.0, 0);
5. NULL qiymatlar bilan ishlash
-- NULL qiymat qo'shish
INSERT INTO users (name, email, age) VALUES ('Test User', '[email protected]', NULL);
-- Yoki ustunni umuman ko'rsatmaslik
INSERT INTO users (name, email) VALUES ('Test User 2', '[email protected]');
-- age ustuni NULL bo'ladi
6. Boshqa jadvaldan ma'lumot nusxalash
-- Boshqa jadvaldan ma'lumot nusxalash
INSERT INTO users (name, email, age)
SELECT name, email, age FROM old_users WHERE age > 25;
-- Barcha ma'lumotlarni nusxalash
INSERT INTO users_backup SELECT * FROM users;
7. Murakkab misollar
Blog loyihasi
-- Jadvallar yaratish
CREATE TABLE categories (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE,
description TEXT
);
CREATE TABLE posts (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
content TEXT,
category_id INTEGER,
author TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
is_published BOOLEAN DEFAULT 0,
FOREIGN KEY (category_id) REFERENCES categories(id)
);
-- Kategoriyalar qo'shish
INSERT INTO categories (name, description) VALUES
('Texnologiya', 'IT va texnologiya yangiliklari'),
('Sport', 'Sport yangiliklari va natijalar'),
('Siyosat', 'Siyosiy yangiliklari');
-- Postlar qo'shish
INSERT INTO posts (title, content, category_id, author, is_published) VALUES
('Python dasturlash', 'Python dasturlash tili haqida...', 1, 'Ali Valiyev', 1),
('Futbol natijalari', 'O''zbekiston - Braziliya 2:1', 2, 'Malika Karimova', 1),
('Saylov natijalari', 'Prezident saylovi natijalari...', 3, 'Bobur Toshmatov', 0);
8. Xatolar va ularni hal qilish
Keng tarqalgan xatolar
-- Xato: NOT NULL constraint
INSERT INTO users (email, age) VALUES ('[email protected]', 25);
-- Xato: NOT NULL constraint failed: users.name
-- To'g'ri:
INSERT INTO users (name, email, age) VALUES ('Test User', '[email protected]', 25);
-- Xato: UNIQUE constraint
INSERT INTO users (name, email) VALUES ('User 1', '[email protected]');
INSERT INTO users (name, email) VALUES ('User 2', '[email protected]');
-- Xato: UNIQUE constraint failed: users.email
-- Xato: CHECK constraint
CREATE TABLE products (price REAL CHECK (price > 0));
INSERT INTO products (price) VALUES (-10);
-- Xato: CHECK constraint failed: products
9. INSERT OR IGNORE
-- Xatolarni e'tiborsiz qoldirish
INSERT OR IGNORE INTO users (name, email) VALUES
('Ali Valiyev', '[email protected]'),
('Malika Karimova', '[email protected]'),
('Ali Valiyev', '[email protected]'); -- Bu qator e'tiborsiz qoldiriladi
-- INSERT OR REPLACE
INSERT OR REPLACE INTO users (id, name, email) VALUES
(1, 'Ali Valiyev Yangi', '[email protected]');
10. Amaliy loyiha
-- Kutubxona tizimi
CREATE TABLE authors (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
birth_year INTEGER,
nationality TEXT
);
CREATE TABLE books (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT NOT NULL,
author_id INTEGER,
isbn TEXT UNIQUE,
publication_year INTEGER,
pages INTEGER,
price REAL DEFAULT 0.0,
FOREIGN KEY (author_id) REFERENCES authors(id)
);
-- Mualliflar qo'shish
INSERT INTO authors (name, birth_year, nationality) VALUES
('Alisher Navoiy', 1441, 'O''zbek'),
('Abdulla Avloniy', 1878, 'O''zbek'),
('Chingiz Aytmatov', 1928, 'Qirg''iz'),
('Leo Tolstoy', 1828, 'Rus');
-- Kitoblar qo'shish
INSERT INTO books (title, author_id, isbn, publication_year, pages, price) VALUES
('Xamsa', 1, '978-1234567890', 1495, 500, 25.50),
('Turkiy guliston', 1, '978-1234567891', 1499, 300, 20.00),
('O''zbek tili grammatikasi', 2, '978-1234567892', 1920, 200, 15.75),
('Jamilia', 3, '978-1234567893', 1958, 150, 12.00),
('War and Peace', 4, '978-1234567894', 1869, 1200, 35.00);
-- Ma'lumotlarni tekshirish
SELECT * FROM authors;
SELECT * FROM books;
Eng yaxshi amaliyotlar
- ✅ Ustun nomlarini ko'rsating: Aniqroq va xavfsizroq
- ✅ DEFAULT qiymatlardan foydalaning: Kodni soddalashtirish
- ✅ NULL qiymatlarni tekshiring: Ma'lumotlar to'g'riligini ta'minlash
- ✅ Transaction ishlatish: Ko'p operatsiyalar uchun
- ✅ Xatolarni boshqarish: INSERT OR IGNORE, INSERT OR REPLACE