0% found this document useful (0 votes)
12 views3 pages

SQL Database Operations Guide

The document outlines SQL commands for creating and deleting databases, including error handling and best practices. It emphasizes the importance of unique naming, permissions, and the use of IF NOT EXISTS and IF EXISTS clauses to prevent errors. Additionally, it provides a PHP script example for connecting to a MySQL database and inserting a record.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

SQL Database Operations Guide

The document outlines SQL commands for creating and deleting databases, including error handling and best practices. It emphasizes the importance of unique naming, permissions, and the use of IF NOT EXISTS and IF EXISTS clauses to prevent errors. Additionally, it provides a PHP script example for connecting to a MySQL database and inserting a record.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

DATABASE OPREATION

1) SHOW DATABASE;
2) CREATE DATABASE “DATABASENAME”;
3) SHOW CREATE DATABASE “DATABASENAME”;
4) CREATE DATABASE IF NOT EXISTS “DATABASENAME”;

Common Errors and How to Avoid Them


1. Database Already Exists
Creating a database with an existing name causes an error. Use IF NOT EXISTS to avoid this:
CREATE DATABASE IF NOT EXISTS “DATABASENAME”;
2. Insufficient Privileges
Ensure you have the required permissions (usually admin rights) to create a database.
Key Points About CREATE DATABASE
• Unique Naming: Ensure database names are unique and consistent. Use underscores (_)
instead of spaces (e.g., my_database).
• Name Length: Keep names concise; max length is typically 128 characters.
• Permissions Required: Only users with the right privileges can create databases.
• Case Insensitivity: Most SQL systems treat names as case-insensitive—use consistent
formatting.
• Cross-DB Compatibility: Syntax may slightly differ between SQL systems (MySQL,
PostgreSQL, SQL Server), so check documentation.

Deleting a Database (DROP DATABASE): DROP DATABASE “DATABASENAME” ;


SQL DROP DATABASE IF EXISTS
To avoid any error while running the DROP DATABASE command use the IF EXISTS clause, which
will delete the database only if it exists in the system.
Syntax
DROP DATABASE IF EXISTS “DATABASENAME”;
This command will check if the database exists before attempting to drop it, making it safer to run in
scripts or on production systems.
Important Points About SQL DROP DATABASE Statement
• Deleting a Database: Deleting a database is a permanent action and will result in the loss of all
information stored in the database.
• Backup: Its always been a good practice to take proper backup of the database before
performing any delete operation on it.
• Privileges: Make sure you have the necessary privilege to delete the database. Usually an
admin can delete the database.
• Database State: A database can be dropped regardless of its state: offline, read-only, suspect,
and so on.
• Database Snapshots: Dropping a database snapshot deletes the database snapshot from an
instance of SQL Server and deletes the physical NTFS File System sparse files used by the
snapshot.
• Replication: To drop a database published for transactional replication, or published or
subscribed to merge replication, you must first remove replication from the database.
• System Databases: System databases cannot be directly dropped or edited by the user.

PHP CONNECTION:

<?php

$servername = "[Link]";
$username = "root";
$password = ""; // Typo fixed here from $passwrod to $password
$dbname = "MYdb3";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connection Successful<br>";

// Uncomment to create database if needed


/*
$mysqli = "CREATE DATABASE MYdb3";
if ($conn->query($mysqli) === TRUE) {
echo "Database created successfully<br>";
} else {
echo "Error creating database: " . $conn->error . "<br>";
}
*/

// Uncomment to create table if needed


/*
$crtable = "CREATE TABLE STUDENT (
stId INT(9) PRIMARY KEY,
StName VARCHAR(20),
Stclass VARCHAR(20),
Stmark INT(9)
)";
if ($conn->query($crtable) === TRUE) {
echo "Table created successfully<br>";
} else {
echo "Error creating table: " . $conn->error . "<br>";
}
*/

// Insert record
$inrecord = "INSERT INTO STUDENT (stId, StName, Stclass, Stmark)
VALUES (1, 'Ajit Shirgire', 'SYComputer', 99)";

if ($conn->query($inrecord) === TRUE) {


echo "Record inserted successfully";
} else {
echo "Error inserting record: " . $conn->error;
}

$conn->close();

?>

You might also like