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

Lệnh Delete trong SQL Server - Freetuts
GETTING STARTED
DATA DEFINITION
DATA QUERIES
CONDITION & OPERATORS
EXPLORED
CÁC CHỦ ĐỀ
BÀI MỚI NHẤT
MỚI CẬP NHẬT

Lệnh Delete trong SQL Server

Trong bài này chúng ta sẽ tìm hiểu lệnh Delete trong SQL Server. Delete được dùng để xóa một hoặc nhiều dòng dữ liệu trong bảng, thường kết hợp với lệnh Where để lọc 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. Giới thiệu lệnh Delete trong SQL Server

Hầu như ứng dụng nào cũng có tính năng xóa dữ liệu. Ví dụ trong ứng dụng quản lý nhân viên sẽ có chức năng thêm / xóa / sửa.

  • Thêm thì sử dụng lệnh Insert
  • Xóa thì sử dụng lệnh Delete
  • Sửa thì sử dụng lệnh Update

Cú pháp của nó trong SQL Server như sau:

DELETE FROM table_name
WHERE condition;

Nếu bạn không truyền vào condition thì nó sẽ xóa toàn bộ dữ liệu của bảng. Vì vậy bạn nên test kỹ câu lệnh Delete trước khi thực hiện câu truy vấn.

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

Thông thường ta hay xóa theo khóa chính Primary Key. Ví dụ muốn xóa nhân viên có mã số sinh viên là SV002, đây thường là khóa chính của table SINHVIEN.

Trường hợp xóa dữ liệu mà có ràng buộc khóa ngoại thì bạn phải xóa toàn bộ dữ liệu của bảng con có khóa ngoại trùng với khóa chính của nó đã nhé.

Ví dụ bạn muốn xóa phòng ban PB001 thì bước đầu tiên phải xóa toàn bộ nhân viên trong phòng, hoặc chuyển sang phòng ban khác để tránh bị thất lạc dữ liệu.

Nếu muốn đơn giản hơn thì sử dụng option ON DELETE CASCADE trong lúc tạo khóa ngoại nhé, nó sẽ giúp bạn tự động delete dữ liệu ở bảng con.

CREATE TABLE rooms (
    room_no INT PRIMARY KEY AUTO_INCREMENT,
    room_name VARCHAR(255) NOT NULL,
    building_no INT NOT NULL,
    FOREIGN KEY (building_no)
        REFERENCES buildings (building_no)
        ON DELETE CASCADE
);

2. Cách sử dụng Delete trong SQL Server

Chúng ta sẽ tiếp tục sử dụng table Employees làm ví dụ mẫu nhé.

Cấu trúc của nó như sau:

employees table png

Lời khuyên: Bạn nên backup lại dữ liệu trước khi kiểm thử câu lệnh Delete nhé, bởi vì nó sẽ xóa vĩnh viễn và bạn không thể phục hồi được.

Giả sử mình muốn xóa tất cả nhân viên nằm trong phòng ban có mã officeCode = 4.

DELETE FROM employees 
WHERE
    officeCode = 4;

Xóa nhân viên có mã số employeeNumber = 200.

DELETE FROM employees 
WHERE
    employeeNumber = 200;

Xóa tất cả nhân viên trong bảng employees.

DELETE FROM employees;

Lệnh cuối này nó gần giống như lệnh Truncate, điểm khác duy nhất là Delete xóa nhưng không reset lại chỉ số, còn Truncate thì có.

3. Delete kết hợp Limit trong SQL Server

Một điều khá đặc biệt mà ít người chú ý đến đó là bạn có thể kết hợp lệnh Order By và Limit để tạo ra khoảng giới hạn cần xóa.

Cú pháp như sau:

DELETE FROM table
LIMIT row_count;

Và để sắp xếp thì ta sẽ thêm câu lệnh Order By.

DELETE FROM table_name
ORDER BY c1, c2, ...
LIMIT row_count;

Ví dụ: Xóa 10 nhân viên đầu tiên và danh sách được sắp xếp theo tên nhân viên.

DELETE FROM customers
ORDER BY customerName
LIMIT 10;

Hoặc xóa 20 nhân viên đầu tiên sắp xếp theo employeeNumber.

DELETE FROM customers
ORDER BY employeeNumber
LIMIT 20;

Rất đơn giản phải không các bạn?

Như vậy là mình đã hướng dẫn xong cách sử dụng lệnh Delete trong SQL Server. Đây là lệnh khá là quan trọng, bởi nó được sử dụng ở hầu hết các ứng dụng để đưa vào tính năng xóa dữ liệu.

Cùng chuyên mục:

Hàm CHAR trong SQL Server

Hàm CHAR trong SQL Server

Cách sử dụng hàm CHAR trong SQL Server

Hàm CHARINDEX trong SQL Server

Hàm CHARINDEX trong SQL Server

Cách sử dụng hàm CHARINDEX trong SQL Server

Hàm CONCAT trong SQL Server

Hàm CONCAT trong SQL Server

Cách sử dụng hàm CONCAT trong SQL Server

Hàm + trong SQL Server

Hàm + trong SQL Server

Cách sử dụng hàm + trong SQL Server

Hàm DATALENGTH trong SQL Server

Hàm DATALENGTH trong SQL Server

Cách sử dụng hàm DATALENGTH trong SQL Server

Hàm LEFT trong SQL Server

Hàm LEFT trong SQL Server

Cách sử dụng hàm LEFT trong SQL Server

Hàm LEN trong SQL Server

Hàm LEN trong SQL Server

Cách sử dụng hàm LEN trong SQL Server

Hàm LOWER trong SQL Server

Hàm LOWER trong SQL Server

Cách sử dụng hàm LOWER trong SQL Server

Hàm LTRIM trong SQL Server

Hàm LTRIM trong SQL Server

Cách sử dụng hàm LTRIM trong SQL Server

Hàm NCHAR trong SQL Server

Hàm NCHAR trong SQL Server

Cách sử dụng hàm NCHAR trong SQL Server

Hàm PATINDEX trong SQL Server

Hàm PATINDEX trong SQL Server

Cách sử dụng hàm PATINDEX trong SQL Server

Hàm REPLACE trong SQL Server

Hàm REPLACE trong SQL Server

Cách sử dụng hàm REPLACE trong SQL Server

Hàm RIGHT trong SQL Server

Hàm RIGHT trong SQL Server

Cách sử dụng hàm RIGHT trong SQL Server

Hàm RTRIM trong SQL Server

Hàm RTRIM trong SQL Server

Cách sử dụng hàm RTRIM trong SQL Server

Hàm SPACE trong SQL Server

Hàm SPACE trong SQL Server

Cách sử dụng hàm SPACE trong SQL Server

Hàm STR trong SQL Server

Hàm STR trong SQL Server

Cách sử dụng hàm STR trong SQL Server

Hàm UPPER trong SQL Server

Hàm UPPER trong SQL Server

Cách sử dụng hàm UPPER trong SQL Server

Hàm SUBSTRING trong SQL Server

Hàm SUBSTRING trong SQL Server

Cách sử dụng hàm SUBSTRING trong SQL Server

Hàm STUFF trong SQL Server

Hàm STUFF trong SQL Server

Cách sử dụng hàm STUFF trong SQL Server

Hàm ABS trong SQL Server

Hàm ABS trong SQL Server

Cách sử dụng hàm ABS trong SQL Server

Top