MySQL Basics
Connecting to MySQL
To connect to MySQL, use the following command:
mysql -u username -p
Enter the password when prompted.
Creating a Database
CREATE DATABASE my_database;
This command creates a new database named my_database.
Selecting a Database
USE my_database;
This command selects my_database for subsequent operations.
Creating a Table
CREATE TABLE users ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(100) NOT NULL, email VARCHAR(100) UNIQUE NOT NULL, password VARCHAR(255) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP );
This command creates a users table with fields for user ID, name, email, password, and creation time.
Inserting Data
INSERT INTO users (name, email, password) VALUES ('John Doe', 'john@example.com', 'securepassword123');
This inserts a record into the users table.
Querying Data
SELECT * FROM users;
This retrieves all records from the users table.