MySQL是一款并發(fā)性、高可靠性、高可擴(kuò)展性的數(shù)據(jù)庫管理系統(tǒng),可用于處理大量數(shù)據(jù)的存儲和管理。為了更好地學(xué)習(xí)MySQL數(shù)據(jù)庫,我們需要進(jìn)行一些實(shí)際的練習(xí)。
以下是一些MySQL數(shù)據(jù)庫的實(shí)際練習(xí)示例:
CREATE DATABASE employee; USE employee; CREATE TABLE employees ( id INT(11) NOT NULL AUTO_INCREMENT, first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL, gender ENUM('male', 'female') NOT NULL, email VARCHAR(100) NOT NULL, hire_date DATE NOT NULL, birth_date DATE NOT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB; INSERT INTO employees (first_name, last_name, gender, email, hire_date, birth_date) VALUES ('John', 'Doe', 'male', 'john.doe@email.com', '2021/01/01', '1990/01/01'), ('Jane', 'Doe', 'female', 'jane.doe@email.com', '2021/01/01', '1992/01/01'); SELECT * FROM employees;
此示例演示了創(chuàng)建名為“employee”的數(shù)據(jù)庫,以及在該數(shù)據(jù)庫中創(chuàng)建“employees”表。然后我們插入兩條記錄。最后,我們使用SELECT語句從“employees”表中選擇所有記錄。
CREATE DATABASE university; USE university; CREATE TABLE students ( id INT(11) NOT NULL AUTO_INCREMENT, first_name VARCHAR(50) NOT NULL, last_name VARCHAR(50) NOT NULL, email VARCHAR(100) NOT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB; CREATE TABLE courses ( id INT(11) NOT NULL AUTO_INCREMENT, name VARCHAR(50) NOT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB; CREATE TABLE enrollments ( student_id INT(11) NOT NULL, course_id INT(11) NOT NULL, PRIMARY KEY (student_id, course_id), FOREIGN KEY (student_id) REFERENCES students(id), FOREIGN KEY (course_id) REFERENCES courses(id) ) ENGINE=InnoDB; INSERT INTO students (first_name, last_name, email) VALUES ('John', 'Doe', 'john.doe@email.com'), ('Jane', 'Doe', 'jane.doe@email.com'); INSERT INTO courses (name) VALUES ('Math'), ('Science'), ('History'); INSERT INTO enrollments (student_id, course_id) VALUES (1, 1), (1, 2), (2, 1); SELECT s.first_name, s.last_name, c.name FROM students s INNER JOIN enrollments e ON s.id = e.student_id INNER JOIN courses c ON e.course_id = c.id;
此示例演示使用MySQL創(chuàng)建一個大學(xué)數(shù)據(jù)庫。我們創(chuàng)建了三張表:學(xué)生表,課程表和注冊表。 最后,我們插入了一些示例數(shù)據(jù),并通過JOIN操作查找每個學(xué)生所注冊的所有課程。
通過這些實(shí)例,您可以更好地了解如何使用MySQL處理數(shù)據(jù)。練習(xí)可以幫助您更好地理解MySQL的語法和功能。祝您盡可能多練習(xí)并加深您的MySQL知識!