sql: structured query language
creates a database named mydb1.
create database mydb1;
view all databases
show databases;
create a mydb2 database using the utf-8 character set.
create database mydb2 character set utf8;
create a mydb3 database using utf-8 character set with collation rules.
create database mydb3 character set utf8 collate utf8_general_ci;
show the creation information of the
database show create database mydb3;
delete the mydb1 database created earlier
drop database mydb1;
view the database in the server, and change the character set of one of the libraries to gb2312;
alter database mydb2 character set gb2312;
show create database mydb2;
backup database
1, prepare database data
create database mydb1;
use mydb1;
create table test
(
id int
);
insert into test(id) values(1);
select * from test;
2. Backup database
2.1 Exit the mysql client: quit
2.2 Execute in the windows command line window: mysqldump -uroot -p mydb1>c:\test .sql
3. Delete the database: drop database mydb1;
4. Restore the database (1):
4.1 Create the database: create database mydb1;
4.2 source c:\test.sql (implemented by executing the script file)
5. Restore the database (2): mysql -uroot -p mydb1<c:\test.sql (window command)
create an employee table
use mydb1; enter the database
create table employee
(
id int,
name varchar(20),
gender varchar(4),
birthday date,
entry_date date ,
job varchar(40),
salary double,
resume text
)character set utf8 collate utf8_general_ci;
View all tables in the library
show tables;
view the creation details of the
table show create table employee;
view the structure of the table
desc employee;
basically add an image column to the above employee table.
alter table employee add image blob;
modify the job column to have a length of 60.
alter table employee modify job varchar(60);
delete the sex column.
alter table employee drop gender;
the table name is changed to user.
rename table employee to user;
modify the character set of the table to utf-8
alter table user character set gb2312;
show create table user;
modify the column name name to username
alter table user change column name username varchar(20);
use the insert statement to Insert an employee's information.
insert into employee(id,username,birthday,entry_date,job,salary,resume) values(1,'aaa','1980-09-09','1980-09-09','bbb',1000,'bbbbbbbb ');
View the inserted data
select * from employee;
Use the insert statement to insert an employee's information into the table.
insert into employee(id,username,birthday,entry_date,job,salary,resume) values(2,'Little plum','1980-09-09','1980-09-09','bbb',1000,' bbbbbbbb');
the solution after insert failure
show variables like 'chara%';
set character_set_client=gb2312;
the solution after display failure
set character_set_results=gb2312;
modify the salary of all employees to 5000 yuan.
update employee set salary=5000;
Modify the salary of the employee named 'aaa' to 3000 yuan.
update employee set salary=3000 where username='aaa';
update employee set salary=4000, job='ccc' where username='aaa';
increase aaa's salary by 1000 yuan on the original basis.
update employee set salary=salary+1000 where username='aaa';
delete the record named 'zs' in the table.
delete from employee where username='little plum';
delete all records in the table.
delete from employee;
use truncate to delete records in the table.
truncate table employee;
query the information of all students in the table.
select id,name,chinese,english,math from student;
select * from student;
query the names and corresponding English scores of all students in the table.
select name,english from student;
filter duplicate data in the table.
select distinct english from student;
add 10 special points to all students' English scores.
select name,english+10 from student;
count the total score of each student.
select name,(english+chinese+math) from student;
use an alias for student grades.
select name as name,(english+chinese+math) as total score from student;
select name name,(english+chinese+math) total score from student;
query the score of the student whose name is Wang
Wuselect * from student where name=' Wang Wu';
Query students whose English score is greater than 90
select * from student where english>90;
query all students whose total score is greater than 200
select * from student where (english+chinese+math)>200;
query English score is 80 - Classmates between 90.
select * from student where english>80 and english<90;
select * from student where english between 80 and 90;
query students whose math scores are 89,90,91.
select * from student where math=80 or math=90 or math=91;
select * from student where math in(80,90,91);
Check the grades of all students with the last name Li.
select * from student where name like 'Li%';
sort the math scores and output.
select name, math from student order by math;
sort the total score and output, and then output in descending order
select name from student order by (math+english+chinese) desc;
sort and output the scores of students with the last name Li
select name name,(math+english+chinese) total score from student where name like 'Li%' order by (math+english+chinese) desc;
How many students are there in a class?
select count(*) from student;
select count(name) from student;
How many students have a statistical math score greater than 90?
select count(*) from student where math>90;
How many people have a total score greater than 250?
select count(*) from student where (math+english+chinese)>250;
Count the total math scores of a class?
select sum(math) from student;
Count the total grades of Chinese, English, and mathematics in a class
select sum(math), sum(chinese), sum(english) from student;
count the sum of the grades of Chinese, English, and mathematics in a class
select sum(chinese+math+english ) from student; Calculate the
average score of a class in Chinese
select sum(chinese)/count(chinese) from student;
Find the average score of mathematics in a class?
select avg(math) from student;
find the average total score of a class
select avg(chinese+english+math) from student;
find the highest and lowest scores of the class
select max(chinese+english+math),min(chinese+english+ math) from student;
after classifying the products in the order table, display the total price of each type of product
select product from orders group by product;
select product, sum(price) from orders group by product;
query how many types of products have been purchased, And each category of products with a total price greater than 100
select product from orders group by product having sum(price)>100;
Define a table with primary key constraints
create table test1
(
id int primary key,
name varchar(20),
password varchar(20)
);
define a table with primary key auto-growth
create table test2
(
id int primary key auto_increment,
name varchar(20 ),
password varchar(20)
);
create table test3
(
id int primary key auto_increment,
name varchar(20) unique
);
create table test4
(
id int primary key auto_increment,
name varchar(20) unique not null
);
//what is a foreign key constraint
create table husband
(
id int primary key,
name varchar(20)
);
create table wife
(
id int primary key,
name varchar(20),
husband_id int,
constraint husband_id_FK foreign key(husband_id) references husband(id)
);
//一对多或多对一
create table department
(
)
create table employee
(
)
//多对多
create table teacher
(
id int primary key,
name varchar(20),
salary double
);
create table student
(
id int primary key,
name varchar(20) ); create table teacher_student ( teacher_id int, student_id int,
primary key(teacher_id,student_id),
constraint teacher_id_FK foreign key(teacher_id) references teacher(id),
constraint student_id_FK foreign key(student_id) references student(id) ); //一对一 create table person ( id int primary key, name varchar(20) ); create table idcard ( id int primary key, address varchar(40), constraint id_FK foreign key(id) references person(id) );
creates a database named mydb1.
create database mydb1;
view all databases
show databases;
create a mydb2 database using the utf-8 character set.
create database mydb2 character set utf8;
create a mydb3 database using utf-8 character set with collation rules.
create database mydb3 character set utf8 collate utf8_general_ci;
show the creation information of the
database show create database mydb3;
delete the mydb1 database created earlier
drop database mydb1;
view the database in the server, and change the character set of one of the libraries to gb2312;
alter database mydb2 character set gb2312;
show create database mydb2;
backup database
1, prepare database data
create database mydb1;
use mydb1;
create table test
(
id int
);
insert into test(id) values(1);
select * from test;
2. Backup database
2.1 Exit the mysql client: quit
2.2 Execute in the windows command line window: mysqldump -uroot -p mydb1>c:\test .sql
3. Delete the database: drop database mydb1;
4. Restore the database (1):
4.1 Create the database: create database mydb1;
4.2 source c:\test.sql (implemented by executing the script file)
5. Restore the database (2): mysql -uroot -p mydb1<c:\test.sql (window command)
create an employee table
use mydb1; enter the database
create table employee
(
id int,
name varchar(20),
gender varchar(4),
birthday date,
entry_date date ,
job varchar(40),
salary double,
resume text
)character set utf8 collate utf8_general_ci;
View all tables in the library
show tables;
view the creation details of the
table show create table employee;
view the structure of the table
desc employee;
basically add an image column to the above employee table.
alter table employee add image blob;
modify the job column to have a length of 60.
alter table employee modify job varchar(60);
delete the sex column.
alter table employee drop gender;
the table name is changed to user.
rename table employee to user;
modify the character set of the table to utf-8
alter table user character set gb2312;
show create table user;
modify the column name name to username
alter table user change column name username varchar(20);
use the insert statement to Insert an employee's information.
insert into employee(id,username,birthday,entry_date,job,salary,resume) values(1,'aaa','1980-09-09','1980-09-09','bbb',1000,'bbbbbbbb ');
View the inserted data
select * from employee;
Use the insert statement to insert an employee's information into the table.
insert into employee(id,username,birthday,entry_date,job,salary,resume) values(2,'Little plum','1980-09-09','1980-09-09','bbb',1000,' bbbbbbbb');
the solution after insert failure
show variables like 'chara%';
set character_set_client=gb2312;
the solution after display failure
set character_set_results=gb2312;
modify the salary of all employees to 5000 yuan.
update employee set salary=5000;
Modify the salary of the employee named 'aaa' to 3000 yuan.
update employee set salary=3000 where username='aaa';
update employee set salary=4000, job='ccc' where username='aaa';
increase aaa's salary by 1000 yuan on the original basis.
update employee set salary=salary+1000 where username='aaa';
delete the record named 'zs' in the table.
delete from employee where username='little plum';
delete all records in the table.
delete from employee;
use truncate to delete records in the table.
truncate table employee;
query the information of all students in the table.
select id,name,chinese,english,math from student;
select * from student;
query the names and corresponding English scores of all students in the table.
select name,english from student;
filter duplicate data in the table.
select distinct english from student;
add 10 special points to all students' English scores.
select name,english+10 from student;
count the total score of each student.
select name,(english+chinese+math) from student;
use an alias for student grades.
select name as name,(english+chinese+math) as total score from student;
select name name,(english+chinese+math) total score from student;
query the score of the student whose name is Wang
Wuselect * from student where name=' Wang Wu';
Query students whose English score is greater than 90
select * from student where english>90;
query all students whose total score is greater than 200
select * from student where (english+chinese+math)>200;
query English score is 80 - Classmates between 90.
select * from student where english>80 and english<90;
select * from student where english between 80 and 90;
query students whose math scores are 89,90,91.
select * from student where math=80 or math=90 or math=91;
select * from student where math in(80,90,91);
Check the grades of all students with the last name Li.
select * from student where name like 'Li%';
sort the math scores and output.
select name, math from student order by math;
sort the total score and output, and then output in descending order
select name from student order by (math+english+chinese) desc;
sort and output the scores of students with the last name Li
select name name,(math+english+chinese) total score from student where name like 'Li%' order by (math+english+chinese) desc;
How many students are there in a class?
select count(*) from student;
select count(name) from student;
How many students have a statistical math score greater than 90?
select count(*) from student where math>90;
How many people have a total score greater than 250?
select count(*) from student where (math+english+chinese)>250;
Count the total math scores of a class?
select sum(math) from student;
Count the total grades of Chinese, English, and mathematics in a class
select sum(math), sum(chinese), sum(english) from student;
count the sum of the grades of Chinese, English, and mathematics in a class
select sum(chinese+math+english ) from student; Calculate the
average score of a class in Chinese
select sum(chinese)/count(chinese) from student;
Find the average score of mathematics in a class?
select avg(math) from student;
find the average total score of a class
select avg(chinese+english+math) from student;
find the highest and lowest scores of the class
select max(chinese+english+math),min(chinese+english+ math) from student;
after classifying the products in the order table, display the total price of each type of product
select product from orders group by product;
select product, sum(price) from orders group by product;
query how many types of products have been purchased, And each category of products with a total price greater than 100
select product from orders group by product having sum(price)>100;
Define a table with primary key constraints
create table test1
(
id int primary key,
name varchar(20),
password varchar(20)
);
define a table with primary key auto-growth
create table test2
(
id int primary key auto_increment,
name varchar(20 ),
password varchar(20)
);
create table test3
(
id int primary key auto_increment,
name varchar(20) unique
);
create table test4
(
id int primary key auto_increment,
name varchar(20) unique not null
);
//what is a foreign key constraint
create table husband
(
id int primary key,
name varchar(20)
);
create table wife
(
id int primary key,
name varchar(20),
husband_id int,
constraint husband_id_FK foreign key(husband_id) references husband(id)
);
//一对多或多对一
create table department
(
)
create table employee
(
)
//多对多
create table teacher
(
id int primary key,
name varchar(20),
salary double
);
create table student
(
id int primary key,
name varchar(20) ); create table teacher_student ( teacher_id int, student_id int,
primary key(teacher_id,student_id),
constraint teacher_id_FK foreign key(teacher_id) references teacher(id),
constraint student_id_FK foreign key(student_id) references student(id) ); //一对一 create table person ( id int primary key, name varchar(20) ); create table idcard ( id int primary key, address varchar(40), constraint id_FK foreign key(id) references person(id) );