MySql basic grammar

Access to the database system cd E:\mysql-8.0.18-winx64\bin;

Start mysql service net start mysql

log in mysql -u root -p

change Password ALTER USER 'root'@'localhost' IDENTIFIED BY 'xxxxxx';

All database queries mysql> show databases;

Create a database mysql> create database xxxx

View database mysql> show create database xxxx;

Delete Database mysql> drop database xxxx;

Modify the database mysql> alter database xxxx defalut character set utf8;

View all the tables mysql> show tables;

Enter the table mysql> use xxxx;into the specified database

创建表 mysql> create table student(
-> sid int,
-> sname varchar(20),
-> sage int
-> );

View table structure mysql> desc student;

Delete table mysql> drop table student;

Modify the table
to add a field mysql> alter table student add column sgender varchar(2);
remove fields mysql> alter table student drop column sgender;
to modify the field type mysql> alter table student modify column remark varchar(100);
modify the field names mysql> alter table student change column sgender gender varchar(2);
to modify the table namemysql> alter table student rename to teacher;

All of the fields inserted INSERT INTO student VALUES(1,'张三','男',20);
insertion portion FieldINSERT INTO student(id,NAME) VALUE(3,'王五');

All modified data update student set gender='女';// modified data values of all fields of the entire table
with a condition modification update student set gender='女' where id=1;
modified plurality of dataupdate student set gender='女',age=30 where id=1

Delete all the data delete from student;
another way of deleting truncate table student;// delete the entire table
delete from: ① ② can only be conditional delete delete data table can not be deleted constraint on a table
③ use delete from the deleted data can be rolled back (the transaction)
truncate table: ① ② deleting condition can either not be deleted with the data table, the table can also delete constraint
③ truncate table to delete the data can not be rolled back
deleted conditionaldelete from student where id=2;

All data query select*from student;
query specifies column select id,name from student;
alias (as) // often use aliases when querying a table in a multi-table queries (queries out of the field data is displayed as an alias)
select id as '编号’,name as '姓名' from student as s;
add // add a class constants are listed in the lookup table query column, the field is 'java class employment', alias 'grades'
select id,name,gender,age,'java就业班' as '年级' from student;
merger // query column values can only merge the type of field, generally used for the statistics two types of numeric fields and
select id,name,(math+english) from student;
queries such as queries gender remove duplicate // (not then be repeated by weight)
select distinct gender form student;
select distinct(gender)form student;

Query conditions (WHERE) conditions and logical (and) or (or)
comparison condition> <> = <= == <> (not equal) between and (> = and <=)
determination conditions empty (null empty string) IS null / is not null / == ' ' / <> ''
Fuzzy conditions like // replacement tags typically use the following:
-% represents any characters
- _ represents a replacement character

	  select * from student where id=2 and name='李四';
	  select * from student where id=2 or name='张三';
	  select * from student where math>70;
	  select * from student where math>=70 and math<=90;
	  select * from student where math betreen 70 and 90;//与上面一句相同
	  select * from student where address =='' or is null;
	  select * from student where address <>'' and is not null;	
	  select * from student where name like '张%';	
	  select * from student where name like '张_ _';(匹配几个就用几个)
	  select id,name,(math+english+chinese) as '总成绩' from student;

Common polymerization query functions:sum() avg() max() min() count()

	select sum(math) as 'math的总成绩' from student; //查询所有学生数学成绩和
    select avg(math) as 'math的平均分' from student; //查询数学的平均分
    select max(math) as 'math的最高分' from student;
    select min(math) as 'math的最低分' from student;
    select count(*)  from student;   //统计字段的最大数(包括空)	
    select count(id)  from student;  //统计id的数量(不包括空)

Paging query (limit start line, a few lines of the query) // starting from row zero
select * from student limit 0,2;// 1,2 query records

Sorting query (order by) order by field asc / desc default sort order in a case where recording is inserted

    select * from student order by id asc; //正序排序
    select * from student order by math asc,english desc;按数学正序,英语反序

Grouped query (group by) before the packet with the where keyword, keyword grouping followed by having
// grouped by gender, and the number of statistics for each group of
select gender,count(*) from student group by gender;
persons // filter out the record more than 2
select gender,count(*) from student group by gender having count(*)>2;

Published 25 original articles · won praise 0 · Views 310

Guess you like

Origin blog.csdn.net/weixin_45808666/article/details/104486325