Python MySQL Create Table
In this page:
cursor.execute("CREATE TABLE table_name (column1 type, column2 type)")
Basic Table बनाना
cursor.execute("CREATE TABLE customers (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), address VARCHAR(255))") तीन columns वाला एक नया table define करता है: एक auto-incrementing integer primary key, और name व address के लिए दो text columns।
उदाहरण: Basic Table Creation
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute(
"CREATE TABLE customers (id INT AUTO_INCREMENT PRIMARY KEY, " # auto-incrementing primary key
"name VARCHAR(255), address VARCHAR(255))"
)
print("Table created")
Column Data Types चुनना
MySQL अलग-अलग तरह के data के लिए specific column types देता है -- पूरे numbers के लिए INT, n characters तक की variable-length text के लिए VARCHAR(n), लंबे text के लिए TEXT, dates के लिए DATE/DATETIME, और currency जैसे exact-precision numbers के लिए DECIMAL।
उदाहरण: Choosing Column Data Types
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute(
"CREATE TABLE orders (id INT, price DECIMAL(10,2), placed_on DATE)" # DECIMAL for exact-precision currency values
)
print("DECIMAL used for money, not FLOAT")
जाँचना कि Table पहले से मौजूद है या नहीं
CREATE TABLE IF NOT EXISTS tableName (...) CREATE DATABASE IF NOT EXISTS का table-level equivalent है -- अगर उस नाम का table पहले से मौजूद है तो यह error raise करने की बजाय चुपचाप कुछ नहीं करता, जो repeatable setup scripts में उपयोगी है।
उदाहरण: Checking If a Table Already Exists
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS customers (id INT PRIMARY KEY)") # no error if the table already exists
print("Safe to run more than once")
Creation Confirm करने के लिए SHOW TABLES इस्तेमाल करना
cursor.execute("SHOW TABLES") और फिर cursor पर iterate करना currently selected database के हर table को list करता है, यह जल्दी confirm करने का एक तरीका है कि CREATE TABLE statement असल में सफल हुआ या नहीं।
उदाहरण: Using SHOW TABLES to Confirm Creation
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute("SHOW TABLES") # lists every table in the current database
cursor.__iter__.return_value = iter([("customers",), ("orders",)])
for table in cursor:
print(table)
Foreign Keys और Table Relationships
एक FOREIGN KEY constraint एक table के किसी column को दूसरे table की primary key से जोड़ता है, referential integrity लागू करते हुए -- MySQL किसी ऐसे insert को reject कर देगा जो किसी non-existent parent row को reference करता है, जिससे related tables एक-दूसरे के साथ consistent रहती हैं।
उदाहरण: Foreign Keys and Table Relationships
from unittest.mock import MagicMock
conn = MagicMock()
cursor = conn.cursor()
cursor.execute(
"CREATE TABLE orders (id INT PRIMARY KEY, customer_id INT, "
"FOREIGN KEY (customer_id) REFERENCES customers(id))" # enforces that customer_id must reference a real customer
)
print("orders.customer_id must reference an existing customers.id")
Chapter Quiz — Complete all 12 topics to unlock
0/12 topics done
Complete these topics first:
- Python MySQL Get Started
- Python MySQL Create Database
- Python MySQL Create Table
- Python MySQL Insert Into Table
- Python MySQL Select From Table
- Python MySQL Select With a Filter
- Python MySQL Order By
- Python MySQL Delete Record
- Python MySQL Drop Table
- Python MySQL Update Table
- Python MySQL Limit
- Python MySQL Join