MySQL database statement

   1064 means syntax error
create database dsay00; create database
create database day00 character set urf8 ; create database specified character set urf8
create database day00 character set utf8 collate utf8_bin; create database specified character set and check rules
show databases; show all databases
show database day00;Display database information
show character set;Display the character set supported by MySQL gbk urf8;
show create database day00 ;Display the character set for creating the database
show collation like 'utf8%';Display the validation rules under the specified character set

use day00 ; enter/switch the corresponding database

drop database day00; delete the database
alter database day00 character set 'gbk'; modify the character set of the
database alter database day00 collate gbk_bin; modify the validation rules
select database(); get the database currently in use

show tables ; show all tables
show create table stu; view the created table
desc stu; show the details of a table

Create table
create table data table name (
 column name column type [constraint],
 column name column type,
 column name column type
);
create table stu(
 name varchar(size),
 age int,
 num int,
 birthday date
 
);

MySQL data types

 Numeric type
 tinyint
 smallint
 MEDIUMINT
 int (most common)
 bigint


 time and date type
 column type
  'zero' value
 DATETIME
  '0000-00-00 00:00:00'
 DATE
  '0000-00-00'
 TIMESTAMP (timestamp)
  000000000000000
 TIME
  '00:00:00'
  time and timestamp Difference:
  date: only contains date, if no value is specified when incoming data, it will be null
  timestamp: contains date and time, if no value is specified when incoming data, it will be the current system time
 
 string type
  char and The length of a varchar type
  CHAR column is fixed to the length declared when the table is created. The length can be any value from 0 to 255. The value
  in a VARCHAR column is a variable-length string. The length can be specified as a value between 0 and 65,535
  char(character) fixed length, for example, the specified length is 100, the actual incoming value is 20, and the rest of the characters are filled with spaces.
  varchar(variable character) variable length, for example: the specified length is 100 , the actual incoming value is 20. The actual storage is 20 characters
 
  create table stu2(
  name char(20),
  age int,
  birthday date,
  login timestamp,
  time time
  
  );
 insert into stu2 values('tom',10,'1998-10-10',null,'12:30:50');//Insert data
 select * from stu;//Query data

 Constraint
 not null Non-null constraint
 unique Unique (but null value is not equal to null value)
 unique not null Unique non-null
 primary key Primary key constraint (non-null and unique) auto_increment Automatic growth
 
 create table employee(
 id int primary key auto_increment,
 name varchar(10) not null,
 phone_num varchar(15) unique
 ); delete table drop table temp
 for table operation ;  modify table add column alter table employee2 add hobby varchar(20); delete column alter table employee2 drop hobby; modify the length of column alter table employee2 modify name varchar(20); modify the data type of the column alter table employee2 modify name int;     modify the name of the column alter table employee2

















 change name score int;
 rename table name
  1. rename table employee2 to emp;
  2. alter table employee2 
     rename to tmp;  insert delect  update  select  insert  data   insert into table name (column name 1, column name 2... ) values(value1, value2...);   insert into employee2(id,name) values(1,'kk');   insert into employee2(id,name,phone_num) values(02,'kk','1516666666 ');   //Generally id column does not need to pass values   ​​insert into employee2 values(null,'xx','13945551544',1000);   insert into employee2 values(null,'Zhang San','132884884',5000);   insert into employee2 values(null,'Zhang Sansi','185445284',200);   insert into employee2 values(null,'Li Zhang Wangfei','1355444524',100);
 
 













  insert into employee2 values(null,'Zhang Changzhang','11528452874',220);
  insert into employee2 values(null,'Li Li','195554525278',1200);
 delete data
 delect from employee2 where id =2;
 delect from employee2;Empty the table, keep the table structure (delete one by one)
 truncate employee2;Empty the table, keep the table structure (delete the table at one time, equivalent to drop table, then create the same new empty table)
 modify the data
  update employee2 set name = ' ss' where id = 2;
  update employee2 set salary= salary+100 where id = 3;
 query data
  select column name from table name where condition;
  select * from employee2;
  select name from employee2 where id>2;
  select name,phone_num from employee2 where id>2;
  //Query to remove duplicate distinct
  select distinct salary from employee2;
  //The column after the query is operated
  select salary+100 from employee2;
  //After the query, take the alias as (as can be omitted)
  select salary+100 as new_salary from employee2;
 
 Chinese garbled problem
   Client server
   cmd --> client -->server
   1. Temporarily set cmd code
    mysql --dafault-character-set=gbk -uroot -proot
 will be invalid after the window is closed
   2. Permanently set
     modify the configuration file my.ini line 57 utf8 to GBK
  restart mysql net stop mysql
    net start mysql
 
 
 common symbols
 > < = <> !=
 in(10,20,30)  
 like 'Zhang%' // %One or more characters
 like '%Zhang'
 like '%Zhang%' //As long as it includes Zhang
 like 'Zhang_' // Only one wildcard can be matched
 is null //judging empty
 is not null //judging not empty
 not
 and
 or

select* from stu where name = 'Li Si'; 
select * from employee2 where salary in(1000,200,1200);
select * from employee2 where name like 'Zhang%';
select * from employee2 where name like '__'; //Two underscores represent two words

select * from employee2 where name is null;
select * from employee2 where salary between 81 and 90;
select * from employee2 where salary 1000 or 2000;
 
sort
order by
asc ascending order (default)
desc descending order
select name,math,english from stu order by math asc,english desc;
select name,math+english as sum from stu order by sum desc;alias+sort sort
the English grades of all students surnamed Zhang in descending order
select * from stu where name like 'Zhang%' order by english desc ;
 
 //Create student table
 create table stu(
 id int primary key auto_increment,
 name varchar(10) not null,
 age varchar(15) unique ,
 math int,
 english int
 );
 
 insert into stu values(null,'Li Hua', '15',60,30);
 insert into stu values(null,'Li Ming','14',80,60);
 insert into stu values(null,'Zhang Shi','22',40,96);
 insert into stu values(null,' Wangwu','54',66,50);
 insert into stu values(null,'angel','25',25,70);
 insert into stu values(null,'devil','12',90, 52);
 insert into stu values(null,'Li Si','31',98,90);
 
 Aggregate function, usually used with grouping
 count() count
 count(*)
 select count(name) from stu;
 select count (*) as count from stu;//The query record is aliased
 select count(*) as count from stu where math>80;
 select count(*) as count as sum from stu where sum>120;
 sum
 sum()
 select sum(math) as sum from stu;
 select sum(math) ,sum(english) from stu;
  if the aggregate function sum() is used, null is treated as 0;
    select sum(math)+sum(english) from stu;
  when there is a null value in a column in the record, if you use + to add, it will be null;
    select sum(math+english) from stu;
    solution: use ifnull(column name , specify the default value) function to solve the problem of adding to 0
    select sum(ifnull(math,0)+ifnull(english,0)) from stu;
average

avg()
 select avg(math) from stu;
求max()   min()
 select max(math) from  stu;
 select min(english) from stu;

//Create sales record table
create table orders(
 id int primary key auto_increment,
 product varchar(12) not null,
 date timestamp,
 price int
); 
insert into orders values(null,'washing machine',null,1000);
insert into orders values(null,'TV',null,2200);
insert into orders values(null,'TV',null,2200);
insert into orders values(null,'washing machine',null,1000);
insert into orders values( null,'air conditioner',null,5000);
insert into orders values(null,'washing machine',null,1000);
insert into orders values(null,'air conditioner',null,5000);
insert into orders values(null, 'TV', null, 2200);
grouping operation group by
         group by having followed by conditions // filter the results after group by again, cannot use where with having
  
select *  from  表名 where 条件 group by 列名 having分组后的条件 order by 列名desc/asc;
 select product, sum(price) from orders group by product; 
 select product, sum(price) from orders where price>1000 group by product having sum(price) >10000;
 //出错,应该加别名 select product, sum(price) from orders where price>1000 group by product having sum >10000;
 select product, sum(price) as sum from orders where price>1000 group by product having sum >10000;
 select product, sum(price) as sum from orders where price>1000 group by product having sum >10000 order by sum desc;

 
 

 
 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325893779&siteId=291194637