How To Use SQL DROP AND TRUNCATE STATEMENT Using PHP

admin_img Posted By Bajarangi soft , Posted On 18-09-2020

DROP TABLE statement is used to drop an existing table in a database. DROP TABLE statement is used to delete a table from database. DROP TABLE statement allows you to remove or delete a table from the SQL Server database. TRUNCATE TABLE Statement just empty data inside the existing table TRUNCATE TABLE Statement just removes or delete the data in existing table but not itself

How to use SQL DROP and TRUNCATE TABLE Statement

Syntax SQL DROP TABLE Statement
 

DROP TABLE table_name;
 

Example(1)

DROP TABLE student;

above SQL statement drops or delete all data from existing student table

 

Syntax SQL TRUNCATE TABLE

TRUNCATE TABLE table_name;​​

 

Example(1)

TRUNCATE TABLE student;​

above SQL statement deletes all data inside the student table

after we DROP the table we can not TRUNCATE the table because there will no data or table to do it

2.Complete code of SQL DROP and TRUNCATE Statement  in PHP script.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>How to use SQL DROP and TRUNCATE TABLE Statement</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <br>
    <br>
    <div class="text-center">
        <h1>SQL DROP and TRUNCATE TABLE Statement</h1>
    </div>
    <br>
    <div class="well">
        <?php
        $connect = mysqli_connect("localhost", "root", "", "school_management");
        if($connect === false){
            die("ERROR: Could not connect. " . mysqli_connect_error());
        }
        $sql_1 = "DROP TABLE backupstudents";
//        $sql_2 = "TRUNCATE TABLE student";
        if(mysqli_query($connect, $sql_1)){
            echo "<br><h1 class='text-success'>Table Dropped successfully.</h1>";
        } else{
            echo "ERROR: Could not able to execute $sql_1. " . mysqli_error($connect);
        }
        mysqli_close($connect);
        ?>
    </div>
    <br>
</div>
</body>
</html>

Related Post