mysql cơ sở thao tác ngữ cú, bao quát sổ cư khố đích tăng, san, thiết hoán, dĩ cập biểu đích tăng, san, cải, tra, phục chế.
Sang kiến sổ cư khố
mysql> create database tem;
Sử dụng sổ cư khố
mysql> use tem;
San trừ sổ cư khố
mysql> drop database drop_test;
Sang kiến sổ cư biểu
- student biểu
mysql> CREATE TABLE student (
-> id INT(10) NOT NULL UNIQUE PRIMARY KEY,
-> name VARCHAR(20) NOT NULL,
-> sex VARCHAR(4),
-> birth YEAR,
-> department VARCHAR(20),
-> address VARCHAR(50)
-> );
- Sang kiến score biểu
mysql> CREATE TABLE score (
-> id INT(10) NOT NULL UNIQUE PRIMARY KEY AUTO_INCREMENT,
-> stu_id INT(10) NOT NULL,
-> c_name VARCHAR(20),
-> grade INT(10)
-> );
Sáp nhập biểu sổ cư
- student biểu
mysql> INSERT INTO student VALUES( 001,'RooneyMara', 'F',1985,'Psychology', 'American');
mysql> INSERT INTO student VALUES( 002,'ChrisHemsworth', 'M',1983,'CS', 'Australia');
mysql> INSERT INTO student VALUES( 003,'EllenPage', 'F',1987,'Music', 'Canada');
mysql> INSERT INTO student VALUES( 004,'TomHolland', 'M',1996,'CS', 'England');
mysql> INSERT INTO student VALUES( 005,'ScarlettJohansson', 'F',1984,'Music', 'American');
mysql> INSERT INTO student VALUES( 006,'BenedictCumberbatch','M',1976,'Psychology', 'England');
- score biểu
mysql> INSERT INTO score VALUES(NULL,001, 'Psychology',98);
mysql> INSERT INTO score VALUES(NULL,001, 'Music', 80);
mysql> INSERT INTO score VALUES(NULL,002, 'Psychology',65);
mysql> INSERT INTO score VALUES(NULL,002, 'CS',88);
mysql> INSERT INTO score VALUES(NULL,003, 'CS',95);
mysql> INSERT INTO score VALUES(NULL,004, 'Psychology',70);
mysql> INSERT INTO score VALUES(NULL,004, 'Music',92);
mysql> INSERT INTO score VALUES(NULL,005, 'Music',94);
mysql> INSERT INTO score VALUES(NULL,006, 'Psychology',90);
mysql> INSERT INTO score VALUES(NULL,006, 'Music',85);
Tra tuân biểu sổ cư
Tra tuân student biểu trung đích toàn bộ sổ cư
mysql> select * from student;
Tra tuân student biểu đích đệ 3 điều đáo 5 điều ký lục
mysql> select * from student limit 2,3;
Tra tuân student biểu đích sở hữu học sinh đích id, name hòa department tín tức
mysql> select id,name,department from student;
Tra tuân student biểu trung department vi CS hòa Music đích học sinh tín tức
mysql> select * from student where department in ('CS','Music');
Tra tuân student biểu trung niên linh vi 23~32 tuế đích học sinh tín tức
mysql> select id,name,sex,2019-birth as age,department,address
-> from student
-> where 2019-birth between 23 and 32;
Tra tuân student biểu trung mỗi cá department hữu đa thiếu nhân
mysql> select department,count(id) from student group by department;
Tra tuân score biểu trung mỗi cá khoa mục (c_name) đích tối cao phân (grade)
mysql> select c_name,max(grade) from score group by c_name;
Tra tuân ‘RooneyMara’ đích khoa mục ( c_name ) hòa thành tích ( grade )
mysql> select c_name,grade from score
-> where stu_id=(select id from student where name='RooneyMara');
Tra tuân sở hữu học sinh đích tín tức hòa khảo thí tín tức ( dụng liên tiếp đích phương thức )
mysql> select student.id,name,sex,birth,department,address,c_name,grade
-> from student,score
-> where student.id=score.stu_id;
Kế toán mỗi cá học sinh đích tổng thành tích
mysql> select student.id,name,sum(grade) from student,score
-> where student.id=score.stu_id
-> group by id;
Kế toán mỗi cá khảo thí khoa mục đích bình quân thành tích
mysql> select c_name,avg(grade) from score group by c_name;
Tra tuân Psychology thành tích đê vu 95 đích học sinh tín tức
mysql> select * from student
-> where id in (
-> select stu_id from score
-> where c_name='Psychology' and grade<95);
Tra tuân đồng thời tham gia Psychology hòa CS khảo thí đích học sinh đích tín tức
Phương pháp 1:
mysql> select * from student
-> where id =any
-> (select stu_id from score where stu_id in
-> (select stu_id from score where c_name='Psychology')
-> and c_name='CS');
Phương pháp 2:
mysql> select a.* from student a,score b,score c
-> where a.id=b.stu_id
-> and b.c_name='Psychology'
-> and a.id=c.stu_id
-> and c.c_name='CS';
Tương Psychology khảo thí thành tích án tòng cao đáo đê tiến hành bài tự
mysql> select stu_id,grade from score where c_name='Psychology'
-> order by grade desc;
Tra tuân student biểu hòa score biểu trung học sinh đích học hào ( id ), nhiên hậu hợp tịnh tra tuân kết quả
mysql> select id from student union select stu_id from score;
Tra tuân name trung khai đầu tự mẫu thị “R” hoặc giả “E” đích đồng học đích tính danh ( name ), viện hệ ( department ) hòa khảo thí khoa mục ( c_name ) cập thành tích ( grade )
mysql> select student.id,name,sex,birth,department,address,c_name,grade
-> from student,score
-> where (name like 'R%' or name like 'E%')
-> and student.id=score.stu_id;
Tra tuân address đô thị England đích học sinh đích tính danh ( name ), niên linh, viện hệ ( department ) hòa khảo thí khoa mục ( c_name ) cập thành tích ( grade )
mysql> select student.id,name,sex,2019-birth as age,department,address,c_name,grade
-> from student,score
-> where address='England'
-> and student.id=score.stu_id;
San trừ biểu sổ cư
mysql> delete from score where id=10;
Canh tân biểu sổ cư
mysql> update score set c_name='CS' where id=11;
Phục chế biểu cách thức
1. Tra khán sổ cư biểu đích hoàn chỉnh kết cấu
mysql> show create table student;
2. Tu cải sổ cư biểu danh, tịnh chấp hành SQL ngữ cú
mysql> CREATE TABLE `clone_student` (
-> `id` int(10) NOT NULL,
-> `name` varchar(20) NOT NULL,
-> `sex` varchar(4) DEFAULT NULL,
-> `birth` year(4) DEFAULT NULL,
-> `department` varchar(20) DEFAULT NULL,
-> `address` varchar(50) DEFAULT NULL,
-> PRIMARY KEY (`id`),
-> UNIQUE KEY `id` (`id`)
-> ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;
Phục chế biểu sổ cư
mysql> insert into clone_student(id,name,sex,birth,department,address)
-> select id,name,sex,birth,department,address from student;