PHP MySQL Create Table
In this page:
CREATE TABLE table_name (
id INT AUTO_INCREMENT PRIMARY KEY,
column1 VARCHAR(100) NOT NULL,
column2 VARCHAR(100) UNIQUE,
column3 INT DEFAULT 0
);
एक Basic Table Structure Define करना
एक CREATE TABLE statement हर column का name और data type list करता है -- id INT, name VARCHAR(100), email VARCHAR(100) -- exactly describe करते हुए table किस shape का data रखेगा, कोई भी rows insert होने से पहले।
उदाहरण: Defining a Basic Table Structure
<?php
// Create a new `SQLite3` instance with ':memory:', stored in `$db`
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE users (id INTEGER, name TEXT, email TEXT)");
// Print "Table created with 3 columns" to the output
echo "Table created with 3 columns";
?>
Login to try C/C++/Java/PHP code in the editor
एक Primary Key चुनना
एक primary key किसी table की हर row को uniquely identify करती है, और एक auto-incrementing integer id column (INT AUTO_INCREMENT PRIMARY KEY) सबसे common choice है -- MySQL हर नए row को automatically अगला available number assign करता है, बिना किसी extra काम के uniqueness guarantee करते हुए।
उदाहरण: Choosing a Primary Key
<?php
// Create a new `SQLite3` instance with ':memory:', stored in `$db`
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT)");
$db->exec("INSERT INTO users (name) VALUES ('Alice')");
// Print `"Auto-assigned id: " . $db->lastInsertRowID()` to the output
echo "Auto-assigned id: " . $db->lastInsertRowID();
?>
Login to try C/C++/Java/PHP code in the editor
Column Constraints: NOT NULL, DEFAULT, UNIQUE
सिर्फ एक type से आगे, एक column constraints carry कर सकता है: NOT NULL माँग करता है कि हर row में वहाँ एक value हो, DEFAULT कोई value न दिए जाने पर एक automatic value set करता है, और UNIQUE ensure करता है कि कोई दो rows उस column में same value share न करें -- जैसे यह guarantee करना कि कोई दो users same email address share न करें।
उदाहरण: Column Constraints: NOT NULL, DEFAULT, UNIQUE
<?php
// Create a new `SQLite3` instance with ':memory:', stored in `$db`
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE users (
id INTEGER PRIMARY KEY,
email TEXT NOT NULL UNIQUE,
role TEXT DEFAULT 'member'
)");
// Print "Constraints applied" to the output
echo "Constraints applied";
?>
Login to try C/C++/Java/PHP code in the editor
Table Creation को Repeatable बनाना
CREATE TABLE IF NOT EXISTS exactly अपने database-level counterpart जैसा काम करता है -- MySQL सिर्फ तभी table बनाता है जब यह पहले से exist न करे, नहीं तो चुपचाप कुछ नहीं करता, जो एक setup script को बिना error के एक से ज़्यादा बार चलाने के लिए safe बनाता है।
उदाहरण: Making Table Creation Repeatable
<?php
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE IF NOT EXISTS users (id INTEGER)");
$db->exec("CREATE TABLE IF NOT EXISTS users (id INTEGER)"); // safe to run again
echo "No error on second run";
?>
Login to try C/C++/Java/PHP code in the editor
एक Table का Structure Verify करना
एक table बनाने के बाद, DESCRIBE tableName (या equivalent SHOW COLUMNS query) हर column का name, type, और constraints return करता है -- PHP से यह confirm करने का एक तेज़ तरीका कि एक table exactly intended के हिसाब से बनी थी।
उदाहरण: Verifying a Table's Structure
<?php
$db = new SQLite3(':memory:');
$db->exec("CREATE TABLE users (id INTEGER, name TEXT)");
$result = $db->query("PRAGMA table_info(users)"); // DESCRIBE users; in MySQL
while ($col = $result->fetchArray()) {
echo $col['name'] . "\n";
}
?>
Login to try C/C++/Java/PHP code in the editor
- एक नई table पर एक primary key define करना भूल जाना, बाद में specific rows को reliably update या delete करना कहीं ज़्यादा मुश्किल बनाते हुए।
- एक overly generic column type (जैसे हर चीज़ के लिए TEXT) चुनना बजाय एक उपयुक्त type (INT, VARCHAR, DATE) के जो actual data से match करे और proper validation तथा indexing enable करे।
- एक ऐसी script में IF NOT EXISTS के बिना CREATE TABLE चलाना जो एक से ज़्यादा बार चल सकती है, दूसरी बार run पर एक error का कारण बनते हुए।
- CREATE TABLE tableName (column definitions...) एक नई table का structure define करता है, mysqli_query() के through किसी भी दूसरे SQL statement की तरह चलाया जाता है।
- एक primary key column (अक्सर एक auto-incrementing id) हर row को uniquely identify करता है और reliable updates और deletes के लिए ज़रूरी है।
- IF NOT EXISTS table creation को अगर table पहले से exist करे तो error दिए बिना दोबारा चलाने के लिए safe बनाता है।
Chapter Quiz — Complete all 21 topics to unlock
0/21 topics done
Complete these topics first:
- PHP MySQL Introduction
- PHP MySQLi Connection
- PHP PDO Introduction
- PHP CRUD Operations
- PHP Prepared Statements
- PHP Stored Procedures
- PHP Transactions
- PHP Error Handling in DB
- PHP MySQL Connect
- PHP MySQL Create DB
- PHP MySQL Create Table
- PHP MySQL Insert Data
- PHP MySQL Get Last ID
- PHP MySQL Insert Multiple
- PHP MySQL Prepared Statements
- PHP MySQL Select Data
- PHP MySQL Where
- PHP MySQL Order By
- PHP MySQL Delete Data
- PHP MySQL Update Data
- PHP MySQL Limit Data