அட்டவணையை நீக்குதல்
ஏற்கனவே உள்ள அட்டவணையை "DROP TABLE" அறிக்கையைப் பயன்படுத்தி நீக்கலாம்:
எடுத்துக்காட்டு
"customers" அட்டவணையை நீக்கவும்:
let mysql = require('mysql');
let con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
let sql = "DROP TABLE customers";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Table deleted");
});
});
மேலே உள்ள குறியீட்டை "demo_db_drop_table.js" என்ற கோப்பில் சேமித்து கோப்பை இயக்கவும்:
C:\Users\Your Name>node demo_db_drop_table.js
இது உங்களுக்கு இந்த முடிவைக் கொடுக்கும்:
Table deleted
முக்கியமான எச்சரிக்கை:
- மீளமுடியாதது: DROP TABLE செயல்பாடு மீளமுடியாதது
- அனைத்து தரவும்: அட்டவணையும் அதில் உள்ள அனைத்து தரவும் நிரந்தரமாக நீக்கப்படும்
- கட்டமைப்பு: அட்டவணை கட்டமைப்பு, குறியீடுகள் மற்றும் கட்டுப்பாடுகள் அனைத்தும் நீக்கப்படும்
- பாதுகாப்பு நகல்: முக்கியமான அட்டவணைகளை நீக்குவதற்கு முன் பாதுகாப்பு நகல் எடுக்கவும்
இருந்தால் மட்டும் நீக்கவும்
நீக்க விரும்பும் அட்டவணை ஏற்கனவே நீக்கப்பட்டிருந்தால், அல்லது வேறு எந்த காரணத்திற்காகவும் இல்லையென்றால், பிழை ஏற்படாமல் இருக்க IF EXISTS முக்கியச்சொல்லைப் பயன்படுத்தலாம்.
எடுத்துக்காட்டு
"customers" அட்டவணை இருந்தால் மட்டும் நீக்கவும்:
let mysql = require('mysql');
let con = mysql.createConnection({
host: "localhost",
user: "yourusername",
password: "yourpassword",
database: "mydb"
});
con.connect(function(err) {
if (err) throw err;
let sql = "DROP TABLE IF EXISTS customers";
con.query(sql, function (err, result) {
if (err) throw err;
console.log(result);
});
});
மேலே உள்ள குறியீட்டை "demo_db_drop_table_if.js" என்ற கோப்பில் சேமித்து கோப்பை இயக்கவும்:
C:\Users\Your Name>node demo_db_drop_table_if.js
அட்டவணை இருந்தால், முடிவு பொருள் இப்படித் தெரிகிறது:
{
fieldCount: 0,
affectedRows: 0,
insertId: 0,
serverStatus: 2,
warningCount: 0,
message: '',
protocol41: true,
changedRows: 0
}
அட்டவணை இல்லையென்றால், முடிவு பொருள் இப்படித் தெரிகிறது:
{
fieldCount: 0,
affectedRows: 0,
insertId: 0,
serverStatus: 2,
warningCount: 1,
message: '',
protocol41: true,
changedRows: 0
}
நீங்கள் பார்க்க முடியும் என, ஒரே வித்தியாசம் என்னவென்றால், அட்டவணை இல்லையென்றால் warningCount பண்பு 1 ஆக அமைக்கப்படும்.
அட்டவணை உள்ளது
- warningCount: 0
- affectedRows: 0
- message: ''
- நிலை: வெற்றிகரமாக நீக்கப்பட்டது
அட்டவணை இல்லை
- warningCount: 1
- affectedRows: 0
- message: ''
- நிலை: அட்டவணை இல்லை, ஆனால் பிழை இல்லை
மேம்பட்ட DROP TABLE நுட்பங்கள்
பல அட்டவணைகளை நீக்குதல்
// Drop multiple tables
let sql = "DROP TABLE customers, orders, products";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Multiple tables deleted");
});
// Drop multiple tables with IF EXISTS
let sql = "DROP TABLE IF EXISTS customers, orders, products";
con.query(sql, function (err, result) {
if (err) throw err;
console.log("Tables deleted if they existed");
});
அட்டவணை இருப்பதை முதலில் சரிபார்க்கவும்
// Check if table exists before dropping
con.query("SHOW TABLES LIKE 'customers'", function (err, result) {
if (err) throw err;
if (result.length > 0) {
console.log("Table exists, proceeding with drop...");
// Drop the table
con.query("DROP TABLE customers", function (err, dropResult) {
if (err) throw err;
console.log("Table dropped successfully");
});
} else {
console.log("Table does not exist, no action needed");
}
});
பரிவர்த்தனைகளுடன் பாதுகாப்பான நீக்கல்
// Safe drop with transaction
con.beginTransaction(function(err) {
if (err) throw err;
// First create backup or log the action
let logSql = "INSERT INTO drop_log (table_name, dropped_at) VALUES (?, NOW())";
con.query(logSql, ['customers'], function (err, logResult) {
if (err) {
return con.rollback(function() {
throw err;
});
}
// Then drop the table
let dropSql = "DROP TABLE IF EXISTS customers";
con.query(dropSql, function (err, dropResult) {
if (err) {
return con.rollback(function() {
throw err;
});
}
console.log("Table dropped with logging");
// Commit the transaction
con.commit(function(err) {
if (err) {
return con.rollback(function() {
throw err;
});
}
console.log("Transaction completed successfully");
});
});
});
});
DROP TABLE vs பிற செயல்பாடுகள்
| செயல்பாடு | விளக்கம் | தரவு | கட்டமைப்பு | மீளக்கூடியது |
|---|---|---|---|---|
| DROP TABLE | முழு அட்டவணையையும் நீக்குகிறது | அனைத்து தரவும் நீக்கப்படும் | கட்டமைப்பு நீக்கப்படும் | இல்லை |
| DELETE FROM | அட்டவணையில் இருந்து பதிவுகளை நீக்குகிறது | தரவு மட்டும் நீக்கப்படும் | கட்டமைப்பு உள்ளது | ஆம் (ROLLBACK உடன்) |
| TRUNCATE TABLE | அனைத்து பதிவுகளையும் நீக்குகிறது | அனைத்து தரவும் நீக்கப்படும் | கட்டமைப்பு உள்ளது | இல்லை |
| ALTER TABLE DROP | அட்டவணையில் இருந்து நெடுவரிசையை நீக்குகிறது | நெடுவரிசை தரவு நீக்கப்படும் | கட்டமைப்பு மாற்றப்படும் | இல்லை |
DROP TABLE சிறந்த நடைமுறைகள்
பாதுகாப்பு
- எப்போதும் IF EXISTS பயன்படுத்தவும்
- உற்பத்தி சூழலில் நேரடி DROP தவிர்க்கவும்
- அனுமதிகள் மற்றும் அணுகலைக் கட்டுப்படுத்தவும்
- DROP கட்டளைகளை ஆடிட்டு செய்யவும்
தரவு மேலாண்மை
- முக்கியமான அட்டவணைகளை நீக்குவதற்கு முன் பாதுகாப்பு நகல் எடுக்கவும்
- வெளிநாட்டு விசை உறவுகளைச் சரிபார்க்கவும்
- சார்பு அட்டவணைகளைக் கருத்தில் கொள்ளவும்
- DROP க்கு முன் தரவு ஏற்றுமதி செய்யவும்
குறியீடு தரம்
- நீக்குவதற்கு முன் அட்டவணை இருப்பதைச் சரிபார்க்கவும்
- சரியான பிழை கையாளுதலையும் கண்காணிப்பையும் பயன்படுத்தவும்
- பரிவர்த்தனைகளைப் பயன்படுத்தி அண்மைத்தன்மையை உறுதிப்படுத்தவும்
- DROP செயல்பாடுகளை ஆவணப்படுத்தவும்
முழுமையான எடுத்துக்காட்டு
அனைத்து DROP TABLE நுட்பங்களையும் உள்ளடக்கிய முழுமையான எடுத்துக்காட்டு:
const mysql = require('mysql');
// Create connection
const con = mysql.createConnection({
host: "localhost",
user: "root",
password: "password",
database: "company_db"
});
// Connect to MySQL
con.connect((err) => {
if (err) {
console.error('Error connecting to MySQL:', err.message);
return;
}
console.log('Connected to MySQL database');
// Example 1: Basic DROP TABLE
console.log('\n1. Basic DROP TABLE:');
con.query("DROP TABLE IF EXISTS temp_table", (err, result) => {
if (err) {
console.error('Error in drop 1:', err.message);
return;
}
console.log('Basic drop result:', result);
});
// Example 2: Safe drop with existence check
console.log('\n2. Safe drop with check:');
const tableName = 'customers';
// First check if table exists
con.query("SHOW TABLES LIKE ?", [tableName], (err, checkResult) => {
if (err) {
console.error('Error checking table existence:', err.message);
return;
}
if (checkResult.length > 0) {
console.log(`Table '${tableName}' exists, proceeding with drop...`);
// Drop the table with IF EXISTS for safety
con.query("DROP TABLE IF EXISTS ?", [tableName], (err, dropResult) => {
if (err) {
console.error('Error dropping table:', err.message);
return;
}
console.log('Table dropped successfully');
console.log('Drop result:', dropResult);
});
} else {
console.log(`Table '${tableName}' does not exist`);
}
});
// Example 3: Multiple table drop
console.log('\n3. Multiple table drop:');
setTimeout(() => {
const tables = ['backup_customers', 'temp_orders', 'old_products'];
tables.forEach((table, index) => {
con.query("DROP TABLE IF EXISTS ??", [table], (err, result) => {
if (err) {
console.error(`Error dropping table ${table}:`, err.message);
return;
}
if (result.warningCount > 0) {
console.log(`Table '${table}' did not exist (warning count: ${result.warningCount})`);
} else {
console.log(`Table '${table}' dropped successfully`);
}
// Close connection after last operation
if (index === tables.length - 1) {
setTimeout(() => {
con.end((err) => {
if (err) {
console.error('Error closing connection:', err.message);
return;
}
console.log('\nConnection closed');
});
}, 1000);
}
});
});
}, 2000);
});