DATABASE
-- Create a new database CREATE DATABASE IF NOT EXISTS mydatabase; USE mydatabase; -- Create a table CREATE TABLE IF NOT EXISTS users ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50) NOT NULL, email VARCHAR(50) NOT NULL ); -- Insert data into the table INSERT INTO users (name, email) VALUES ('John Doe', 'john@example.com'), ('Jane Smith', 'jane@example.com'), ('Mike Johnson', 'mike@example.com'); -- Query the data SELECT * FROM users; -- Update data in the table UPDATE users SET email = 'john.doe@example.com' WHERE id = 1; -- Delete data from the table DELETE FROM users WHERE id = 3; -- Add a new column to the table ALTER TABLE users ADD COLUMN phone VARCHAR(20); -- Update data in the table to include phone numbers UPDATE users SET phone = '+1234567890' WHERE id IN (1, 2); -- Query the updated data SELECT * FROM users;
This covers the following operations:
- It creates a database called "mydatabase" (if it doesn't exist) and sets it as the current database.
- It creates a table named "users" with three columns: "id" (an auto-incremented primary key), "name" (a non-null VARCHAR column), and "email" (a non-null VARCHAR column).
- It inserts three rows of data into the "users" table, providing values for the "name" and "email" columns.
- It queries the data from the "users" table and displays the results.
- It updates the email of the user with id 1 to 'john.doe@example.com' using the UPDATE statement.
- It deletes the user with id 3 from the table using the DELETE statement.
- It adds a new column named "phone" of type VARCHAR(20) to the "users" table using the ALTER TABLE statement.
- It updates the data in the table to include phone numbers for the users with id 1 and 2.
- Finally, it queries the updated data from the "users" table and displays the results.
0 Comments