0% found this document useful (0 votes)
9 views7 pages

MySQL Database and Table Management Guide

The document provides a series of SQL commands for managing databases and tables in MySQL. It includes commands to create, select, insert, update, and delete databases and tables, specifically focusing on a 'USERS' and 'POSTS' table. Additionally, it demonstrates how to use various SQL operators for querying data.

Uploaded by

bale1122rmcf
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)
9 views7 pages

MySQL Database and Table Management Guide

The document provides a series of SQL commands for managing databases and tables in MySQL. It includes commands to create, select, insert, update, and delete databases and tables, specifically focusing on a 'USERS' and 'POSTS' table. Additionally, it demonstrates how to use various SQL operators for querying data.

Uploaded by

bale1122rmcf
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

Databases

create DATABASE CLASSDB; -- Create a database using CREATE DATABASE command

SHOW DATABASES; -- List all the DBs in your MYSQL Server

CREATE DATABASE IF NOT EXISTS CLASSDB; -- it will only create the database
if it doesn't exits

DROP DATABASE CLASSDB; -- Deleting a database

USE CLASSDB; -- Select a DB to work

SHOW TABLES; -- List all the tables in the selected DB

CREATE DATABASE FBDB; -- Create a new database

USE FBDB; -- Select the new database

CREATE TABLE USERS (

EMAIL VARCHAR(50) ,

PASSWORD VARCHAR(50) ,

USERNAME VARCHAR(50) ,
ID INT PRIMARY KEY AUTO_INCREMENT

); -- Create a table

SHOW TABLES; -- List all the tables in the selected DB

DESC USERS; -- Describe the table

INSERT INTO USERS (USERNAME, EMAIL, PASSWORD) VALUES

('SANKET', 'SANKET@[Link]', '123456'); -- Insert data into the table

INSERT INTO USERS (USERNAME, EMAIL, PASSWORD) VALUES

('SARTHAK', 'SJ@[Link]', '123456'); -- Insert data into the table

SELECT ID, EMAIL, USERNAME FROM USERS; -- Select data from the table

SELECT * FROM USERS; -- Select all the data from the table

INSERT INTO USERS (USERNAME, EMAIL, PASSWORD) VALUES

('JD', 'JD@[Link]', '123456'),

('RIYA', 'RY@[Link]', '123456'),

('ROHIT', 'RR@[Link]', '123456') ; -- Insert multiple data into the table

-- CREATE A POSTS TABLE WITH ID, CONTENT, USER_ID, CREATED_AT COLUMNS


CREATE TABLE POSTS (

ID INT PRIMARY KEY AUTO_INCREMENT,

CONTENT VARCHAR(255),

USER_ID INT, -- TO WHOM THE POST BELONGS

CREATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP

);

INSERT INTO POSTS (CONTENT, USER_ID) VALUES

('HELLO WORLD', 1); -- Insert data into the table

INSERT INTO POSTS (CONTENT, USER_ID) VALUES

('HELLO WORLD', 1); -- Insert data into the table

INSERT INTO POSTS (CONTENT, CREATED_AT, USER_ID) VALUES

('HELLO WORLD AGAIN', '2021-01-01 12:00:00', 1); -- Insert data into the
table

SELECT * FROM POSTS; -- Select all the data from the table

SELECT * FROM USERS WHERE ID = 3; -- Select all the data from the table

SELECT * FROM POSTS WHERE USER_ID = 1 AND CONTENT = 'HELLO WORLD'; -- Select
all the data from the table

-- OPERATOR IN MYSQL: =, !=, <, >, <=, >=, AND, OR, NOT, IN, BETWEEN, LIKE,
IS NULL, IS NOT NULL

SELECT * FROM POSTS WHERE CONTENT LIKE '%AGAIN'; -- Select all the data from
the table

-- %AGAIN% SUBSTRING MATCH

-- %AGAIN STARTS WITH ANYTHING BUT ENDS WITH AGAIN

-- AGAIN% STARTS WITH AGAIN BUT CAN HAVE ANYTHING AFTER THAT

SELECT * FROM POSTS WHERE CONTENT LIKE '%WORLD' ORDER BY CREATED_AT ASC;

DELETE FROM POSTS WHERE ID = 1; -- Delete a row from the table

DROP TABLE POSTS; -- Delete a table

UPDATE POSTS SET CONTENT = 'MY WORLD' WHERE ID = 2; -- Update a row in the
table

Common questions

Powered by AI

The DROP TABLE command in MySQL is used to delete an entire table, including its definition and all data contained within it. For instance, 'DROP TABLE POSTS;' removes the 'POSTS' table and all data stored in it . The consequence of using DROP TABLE is that it permanently deletes the table, making its recovery impossible unless backed up beforehand. This command is useful when tables are no longer needed, but precautions should be taken to ensure critical data is not lost inadvertently .

In MySQL, you can perform several operations to manage databases: - CREATE DATABASE: This command is used to create a new database. For example, 'CREATE DATABASE CLASSDB;' creates a new database named 'CLASSDB' . - SHOW DATABASES: Lists all databases available in the MySQL server . - CREATE DATABASE IF NOT EXISTS: Creates a database only if it doesn't already exist, helping prevent errors from duplication . - DROP DATABASE: Deletes an existing database, which irreversibly removes all data within it. For instance, 'DROP DATABASE CLASSDB;' deletes the 'CLASSDB' database . - USE: Selects a specific database to work with. Executing 'USE CLASSDB;' sets 'CLASSDB' as the active database .

To create a table in MySQL, use the CREATE TABLE command specifying column names and data types. For instance, 'CREATE TABLE USERS (EMAIL VARCHAR(50), PASSWORD VARCHAR(50), USERNAME VARCHAR(50), ID INT PRIMARY KEY AUTO_INCREMENT);' creates a 'USERS' table with an auto-incrementing ID . To insert data, use INSERT INTO followed by the table name and columns. For example, 'INSERT INTO USERS (USERNAME, EMAIL, PASSWORD) VALUES ('SANKET', 'SANKET@GMAIL.COM', '123456');' adds a new user . Using AUTO_INCREMENT on a primary key ensures each record has a unique identifier that automatically increases with each new entry, simplifying data management and ensuring data integrity .

The DELETE statement in MySQL deletes rows from a table, which is a crucial operation with significant implications as it permanently removes data. For example, 'DELETE FROM POSTS WHERE ID = 1;' deletes the row in the 'POSTS' table where the ID is 1 . Precautions include ensuring the correct rows are targeted to avoid accidental data loss. Backup data before deletion and use WHERE conditions to specify exactly which rows to delete. Another precaution is to perform a SELECT query with the same WHERE condition to verify the correct rows are affected before executing DELETE. Irregular use of DELETE without WHERE can remove all table data, leading to data loss .

The SELECT statement in MySQL is used to query data from one or more tables, allowing you to retrieve specific data sets. You can specify which columns to select using the column names or use '*' to select all columns. For example, 'SELECT * FROM USERS;' retrieves all data from the 'USERS' table . To filter results, use WHERE with conditions. For example, 'SELECT * FROM USERS WHERE ID = 3;' retrieves records where the ID equals 3 . Sorting can be achieved using ORDER BY. For example, 'SELECT * FROM POSTS WHERE CONTENT LIKE '%WORLD' ORDER BY CREATED_AT ASC;' retrieves posts containing 'WORLD' and sorts them in ascending order of creation date .

The TIMESTAMP data type in MySQL is advantageous for storing date and time information because it automatically updates to the current date and time upon record creation or modification, aiding in tracking changes. For instance, 'CREATED_AT TIMESTAMP DEFAULT CURRENT_TIMESTAMP' sets the default to the current time when a record is inserted, making it ideal for creating audit trails . TIMESTAMP is also timezone-aware, converting values to the server's timezone, which is useful for applications with global users needing consistent time representation .

The LIKE operator in MySQL enhances text data queries by allowing pattern matching using wildcards. The '%' wildcard matches any sequence of characters. For example, 'SELECT * FROM POSTS WHERE CONTENT LIKE '%AGAIN';' retrieves posts with any string ending in 'AGAIN' . Patterns identified using LIKE can include strings that start, end, or contain a specific sequence of characters. For instance, '%WORLD' matches any string ending with 'WORLD', while 'AGAIN%' matches strings starting with 'AGAIN' . This flexibility is essential for searching within text data where exact matches may not suffice.

MySQL handles multiple row insertions efficiently using a single INSERT INTO statement with multiple VALUES expressions. This reduces network traffic and speeds up the insertion process. The syntax involves listing all values for each row within parentheses and separating them with commas. For example, 'INSERT INTO USERS (USERNAME, EMAIL, PASSWORD) VALUES ('JD', 'JD@GMAIL.COM', '123456'), ('RIYA', 'RY@GMAIL.COM', '123456'), ('ROHIT', 'RR@GMAIL.COM', '123456');' inserts three rows into the 'USERS' table simultaneously . This method optimizes performance by executing one command instead of multiple separate insertions.

In a MySQL LIKE query, the pattern %AGAIN% searches for any substring that contains 'AGAIN' surrounded by any characters or no character at all. It is used to match strings where 'AGAIN' appears anywhere within the text. For example, 'SELECT * FROM POSTS WHERE CONTENT LIKE '%AGAIN%';' would match texts like 'Say AGAIN soon!', which has 'AGAIN' in between words . In contrast, AGAIN% looks for strings starting with 'AGAIN' followed by any sequence of characters or no character. For instance, it matches 'AGAINST the odds' but not 'We meet AGAIN', since the latter doesn't start with 'AGAIN' .

The UPDATE statement in MySQL modifies existing data in tables by setting new values for specified columns. Its role is crucial for maintaining and correcting data. For example, 'UPDATE POSTS SET CONTENT = 'MY WORLD' WHERE ID = 2;' changes the content of the post with ID 2 . Using WHERE clauses with UPDATE is necessary to target specific records, preventing unintentional changes across the entire table. Omitting the WHERE clause would update all rows in the table, leading to potentially undesirable and extensive changes .

You might also like