peri hokiperihokiclarustologisticsrimashaop.compola taktis menang gates of olympus x1000 perihokipemain perihoki bongkar trik jepep starlight princess x1000perihoki beri panduan lengkap bermain mahjong ways 2 pgsofttips cara penghasil uang pgsoft mahjong ways 2 perihokimahjong wins 3 perihoki memberikan penggali terbesarfenomena gates of olympus buka peluang besar bagi duta76cara dapat pola emas rahasia di duta7 wild west goldtrik menang pgsoft mahjong ways 2 modal receh maxwin duta76duta76 main pgsoft mahjong ways 2 di sela istirahatscatter hitam mahjong wins 3 spin pasti jepe duta76perihoki spin mahjong ways 2 pgsoft scatter hitam maxwinsimbol mahjong wins 3 yang sering muncul di awal perihokitrik gampang menang mahjong winscuan mahjong wins 3 tanpa hentipola spin taruhan kecil mahjong winsstrategi kunci scatter hitam beruntungacor total mahjong wayskemenangan maksimal mahjong wayskemenangan pria pekanbaru mahjong waysmahjong wins3 fenomena baru sumatera baratmenangkan mahjong dengan lincahmenembus batas keberuntungan mahjong waysmenjajal keberuntungan di mahjong wayspola gratis mahjong ways1scatter hitam senjata rahasia mahjong winsteknik spinning menguntungkan untuk scatter hitamanalisis rtp tertinggi mahjong winsfenomena scatter hitam mahjong winsibu rumah tangga dan keberuntungan mahjongkeseruan main mahjong wins3menang mahjong pengguna baru di sosial mediamenghargai setiap momen di mahjong waysserunya main mahjong di warung internetspin slow motion mancing scatterstrategi jitu maxwin mahjong waysstrategi jitu menang mahjong modal kecil jackpot besaranak kampung papua menggemparkan mahjongkeajaiban scatter hitam mahjong wayskeberhasilan joni mahjong winskejaiban mahjong ways di ujung jalanketua mahjong wins3 kehadiran kemenangan dimulai dari balimahjong kehadiran yang menguntungkanmahjong menggila adat88panduan kilat raih 108 jutatips memenangkan mahjongbarista kopi menang 110 juta spin mahjongdakota76 scatter hitam mahjong wins 3gagal cpns menang mahjong wins 3 rp120 jutakisah karyawan minimarket dapat thr lebih awal mahjong winskisah pelajar sma kuasai mahjong wins 3 inspirasi sekolahkisah penjaga parkir tanah abang motor baru mahjong winskisah sukses ibu rumah tangga bandung mahjong winsmahasiswa kkn beli iphone15 mahjong wins3 dakotamantan anak band influencer mahjong scatter hitampola rtp senin gates of olympus guru matematikaidola scatter hitam gampang meledakidola sensasi jackpotidola pola scatter hitamidola hasilkan jackpot dalam waktu singkatidola mahjong wins 3 bocortukang cuci motor menemukan keberuntungan di mahjong

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 282

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_cookie_params(): Cannot change session cookie parameters when headers already sent

Filename: Session/Session.php

Line Number: 294

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 304

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 314

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 315

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 316

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 317

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: Session/Session.php

Line Number: 375

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: ini_set(): Headers already sent. You cannot change the session module's ini settings at this time

Filename: drivers/Session_files_driver.php

Line Number: 108

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_set_save_handler(): Cannot change save handler when headers already sent

Filename: Session/Session.php

Line Number: 110

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

A PHP Error was encountered

Severity: Warning

Message: session_start(): Cannot start session when headers already sent

Filename: Session/Session.php

Line Number: 143

Backtrace:

File: /home/blogchuabenh/domains/freetuts.net/public_html/site/controllers/Main_controller.php
Line: 10
Function: __construct

File: /home/blogchuabenh/domains/freetuts.net/public_html/sources/index.php
Line: 299
Function: require_once

File: /home/blogchuabenh/domains/freetuts.net/public_html/index.php
Line: 26
Function: require_once

Các ràng buộc Constraint trong SQLite - Freetuts
CĂN BẢN
NÂNG CAO
CÁC CHỦ ĐỀ
BÀI MỚI NHẤT
MỚI CẬP NHẬT

Các ràng buộc Constraint trong SQLite

Các ràng buộc (Constraints) là các quy tắc được thi hành trên một cột dữ liệu trên bảng. Chúng được sử dụng để giới hạn loại dữ liệu có thể đi vào bảng. Điều này đảm bảo tính chính xác và độ tin cậy của dữ liệu trong cơ sở dữ liệu.

test php

banquyen png
Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

1. Constraint trong SQLite là gì?

Các ràng buộc (Constraints) có thể là cấp cột hoặc cấp bảng. Các ràng buộc mức cột chỉ được áp dụng cho một cột, trong khi các ràng buộc mức bảng được áp dụng cho toàn bộ bảng.

Sau đây là các ràng buộc có sẵn thường được sử dụng trong SQLite.

  • NOT NULL Constraint: Đảm bảo rằng một cột không thể có giá trị NULL.
  • DEFAULT Constraint: Cung cấp một giá trị mặc định cho một cột khi không được chỉ định.
  • UNIQUE Constraint: Đảm bảo rằng tất cả các giá trị trong một cột là khác nhau.
  • PRIMARY Key: Xác định duy nhất mỗi hàng/bản ghi trong bảng cơ sở dữ liệu.
  • CHECK Constraint: Đảm bảo rằng tất cả các giá trị trong một cột thỏa mãn các điều kiện nhất định.

2. Constraint NOT NULL trong SQLite

Theo mặc định, một cột có thể giữ các giá trị NULL. Nếu bạn không muốn một cột có giá trị NULL, thì bạn cần xác định ràng buộc đó trên cột này chỉ định rằng NULL hiện không được phép cho cột đó.

Bài viết này được đăng tại [free tuts .net]

Một NULL không giống như không có dữ liệu, thay vào đó, nó đại diện cho dữ liệu không xác định.

Ví dụ: câu lệnh SQLite sau đây tạo một bảng mới gọi là COMPANY và thêm 5 cột, 3 trong số đó là ID và NAME và AGE, chỉ định không chấp nhận NULL.

CREATE TABLE COMPANY(
   ID INT PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL,
   ADDRESS        CHAR(50),
   SALARY         REAL
);

3. Constraint DEFAULT trong SQLite

Constraint DEFAULT cung cấp một giá trị mặc định cho một cột khi câu lệnh INSERT INTO không cung cấp một giá trị cụ thể.

Ví dụ: câu lệnh SQLite sau đây tạo một bảng mới gọi là COMPANY và thêm 5 cột. Ở đây, cột SALARY được đặt thành 5000.00 theo mặc định, do đó, trong trường hợp câu lệnh INSERT INTO không cung cấp giá trị cho cột này, thì theo mặc định, cột này sẽ được đặt thành 5000.00.

CREATE TABLE COMPANY(
   ID INT PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL,
   ADDRESS        CHAR(50),
   SALARY         REAL    DEFAULT 50000.00
);

4. Constraint UNIQUE trong SQLite

Constraint UNIQUE ngăn hai bản ghi có các giá trị giống hệt nhau trong một cột cụ thể. Ví dụ, trong bảng COMPANY, bạn có thể muốn ngăn hai hoặc nhiều người có cùng độ tuổi.

Ví dụ: câu lệnh SQLite sau đây tạo một bảng mới gọi là COMPANY và thêm năm cột. Tại đây, cột AGE được đặt thành UNIQUE, do đó bạn không thể có hai bản ghi có cùng độ tuổi

CREATE TABLE COMPANY(
   ID INT PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL UNIQUE,
   ADDRESS        CHAR(50),
   SALARY         REAL    DEFAULT 50000.00
);

5. Constraint PRIMARY KEY trong SQLite

Constraint PRIMARY KEY xác định duy nhất mỗi bản ghi trong bảng cơ sở dữ liệu. Có thể có nhiều cột UNIQUE hơn, nhưng chỉ có một khóa chính trong một bảng. Các khóa chính rất quan trọng khi thiết kế các bảng cơ sở dữ liệu. Khóa chính là ID duy nhất.

Chúng ta sử dụng chúng để chỉ các hàng của bảng. Khóa chính trở thành khóa ngoại trong các bảng khác, khi tạo quan hệ giữa các bảng. Do 'longstanding coding oversight', các khóa chính có thể là NULL trong SQLite. Đây không phải là trường hợp với các cơ sở dữ liệu khác.

Khóa chính là một trường trong bảng xác định duy nhất từng hàng / bản ghi trong bảng cơ sở dữ liệu. Khóa chính phải chứa các giá trị duy nhất. Một cột khóa chính không thể có giá trị NULL.

Nếu một bảng có khóa chính được xác định trên bất kỳ trường nào, thì bạn không thể có hai bản ghi có cùng giá trị của trường đó.

Bạn đã thấy các ví dụ khác nhau ở trên, chúng ta đã tạo bảng COMPANY với ID làm khóa chính.

CREATE TABLE COMPANY(
   ID INT PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL,
   ADDRESS        CHAR(50),
   SALARY         REAL
);

6. Constraint CHECK trong SQLite

Constraint CHECK cho phép một điều kiện để kiểm tra giá trị được nhập vào một bản ghi. Nếu điều kiện là sai, bản ghi sẽ vi phạm ràng buộc và không được nhập vào bảng.

Ví dụ: SQLite sau đây tạo một bảng mới gọi là COMPANY và thêm 5 cột. Ở đây, chúng ta thêm một CHECK với cột SALARY, để bạn không thể có bất kỳ giá trị 0 nào trong cột SALARY.

CREATE TABLE COMPANY3(
   ID INT PRIMARY KEY     NOT NULL,
   NAME           TEXT    NOT NULL,
   AGE            INT     NOT NULL,
   ADDRESS        CHAR(50),
   SALARY         REAL    CHECK(SALARY > 0)
);

7. Constraint Dropping trong SQLite

SQLite hỗ trợ một subset giới hạn của ALTER TABLE. Lệnh ALTER TABLE trong SQLite cho phép người dùng đổi tên bảng hoặc thêm cột mới vào bảng hiện có. Không thể đổi tên một cột, xóa một cột hoặc thêm hoặc xóa các ràng buộc khỏi một bảng.

Cùng chuyên mục:

Các hàm hữu ích trong SQLite

Các hàm hữu ích trong SQLite

SQLite có nhiều hàm dựng sẵn để thực hiện xử lý trên dữ liệu chuỗi…

Date & Time trong SQLite

Date & Time trong SQLite

SQLite hỗ trợ năm hàm ngày và giờ như date(timestring, modifiers...), time(timestring, modifiers...), datetime(timestring, modifiers...),…

VACUUM trong SQLite

VACUUM trong SQLite

Lệnh VACUUM trong SQLite làm sạch cơ sở dữ liệu chính bằng cách sao chép…

EXPLAIN trong SQLite

EXPLAIN trong SQLite

Câu lệnh SQLite có thể được bắt đầu bằng từ khóa "EXPLAIN" hoặc bằng cụm…

SQL Injection trong SQLite

SQL Injection trong SQLite

Nếu bạn nhận user input thông qua một Webpage và chèn nó vào trong một…

Tăng tự động AUTOINCREMENT trong SQLite

Tăng tự động AUTOINCREMENT trong SQLite

AUTOINCREMENT trong SQLite là một từ khóa được sử dụng để tự động tăng giá…

Subquery trong SQLite

Subquery trong SQLite

Subquery hoặc Inner query hoặc Nested query là một truy vấn bên trong một truy…

Transaction trong SQLite

Transaction trong SQLite

Một transaction là một đơn vị công việc được thực hiện đối với một cơ…

Tìm hiểu View trong SQLite

Tìm hiểu View trong SQLite

Một view không có gì khác hơn một câu lệnh SQLite được lưu trữ trong…

Lệnh TRUNCATE TABLE trong SQLite

Lệnh TRUNCATE TABLE trong SQLite

Thật không may, không có lệnh TRUNCATE TABLE trong SQLite nhưng bạn có thể sử…

Lệnh ALTER TABLE trong SQLite

Lệnh ALTER TABLE trong SQLite

Lệnh ALTER TABLE trong SQLite dùng để sửa đổi một bảng hiện có mà không…

Mệnh đề INDEXED BY trong SQLite

Mệnh đề INDEXED BY trong SQLite

Mệnh đề "INDEXED BY index-name" chỉ định rằng index-name phải được sử dụng để tìm…

Index trong SQLite

Index trong SQLite

Index là bảng tra cứu đặc biệt mà database search engine có thể sử dụng…

Tìm hiểu Trigger trong SQLite

Tìm hiểu Trigger trong SQLite

Trigger trong SQLite là các hàm callback, mà tự động được thực hiện/gọi (performed/invoked) khi…

Cú pháp ALIAS trong SQLite

Cú pháp ALIAS trong SQLite

Bạn có thể đổi tên tạm thời một bảng hoặc một cột bằng cách đặt…

Toán tử UNION trong SQLite

Toán tử UNION trong SQLite

Toán tử UNION trong SQLite được sử dụng để kết hợp các kết quả của…

Lệnh JOIN trong SQLite

Lệnh JOIN trong SQLite

Lệnh JOIN trong SQLite được sử dụng để kết hợp các bản ghi từ hai…

Lệnh PRAGMA trong SQLite

Lệnh PRAGMA trong SQLite

Lệnh PRAGMA trong SQLite là một lệnh đặc biệt được sử dụng để kiểm soát…

Từ khóa DISTINCT trong SQLite

Từ khóa DISTINCT trong SQLite

Từ khóa DISTINCT trong SQLite được sử dụng cùng với câu lệnh SELECT để loại…

Mệnh đề HAVING trong SQLite

Mệnh đề HAVING trong SQLite

Mệnh đề HAVING trong SQLite cho phép bạn chỉ định các điều kiện lọc kết…

Top