SQL语句

重启mysql服务器  service mysql restart

停止mysql服务器  service mysql stop

开启mysql服务器  service mysql restart

创建数据库  CREATE DATABASE 数据库名;

删除数据库  drop database <数据库名>;

创建数据表  CREATE TABLE 表名(column_name column_type); 
          create table 表名 -- 表字段名 -- 表字段类型 create table students(id int unsigned primary key auto_increment not null);

查看表结构    desc 表名;

查看表的创建语句 show create table 表名;

查看所有表 show tables;


修改数据表名&修改数据表字段

 删除表字段           ALTER TABLE  表名 DROP  字段名;    alter table students drop birthda;

 增加表字段(定义字段类型)    ALTER  TABLE  表名  ADD 字段名  类型;  alter table students add birthday datetim;

 修改字段类型          alter table 表名 modify 列名 类型及约束    alter table students modify birth date not null;

 修改字段名&字段类型       alter table 表名 change 原名 新名 类型及约束;  alter table students change birthday birth datetime not null;

 修改字段默认值          ALTER TABLE  表名  ALTER 字段名 SET DEFAULT 1000;

 修改表名        ALTER TABLE  原表名 RENAME TO 新表名;

 删除外键约束      alter table tableName drop foreign key keyName;

 修改存储引擎      alter table tableName engine=myisam;

删除数据表  DROP TABLE 表名;  drop table students;

插入数据  INSERT INTO 表名( field1, field2,...fieldN ) VALUES ( value1, value2,...valueN );   
      insert into students(name,hometown,birthday) values('⻩蓉','桃花岛','2016-3-2'value若为字符串类型,需添加引号(单/双)
     
 
      insert into 表名 values (..);       insert into students values(0,’郭靖‘,1,'蒙古','2016-1-2')
      insert into 表名 values (..),(...);   insert into classes values(0,'python1'),(0,'python2')
      insert into 表名(列1,...) values(值1,...),(值1,...)..;  insert into students(name) values('杨康'),('杨过'),('⼩⻰⼥')

修改数据  update 表名 set 列1=值1,列2=值2... where  条件;  update students set gender=0,hometown='北京' where id=5;

删除数据  DELETE FROM 表名 [WHERE 条件]    delete from students where id=2;

删除表中所有数据 :   
delete from 表名;  # delete相当于将mysql表中所有记录一条一条删除到删完,而truncate相当于保留mysql表的结构,重新创建了这个表,所有的状态都相当于新表

truncate table 表名;    # 效率上truncate比delete快,但truncate删除后不记录mysql日志,不可以恢复数据。

 

  删除有外键约束的MySQL表中的数据 :     SET FOREIGN_KEY_CHECKS = 0 ;


  操作结束后  :   SET FOREIGN_KEY_CHECKS = 1;

 

查询数据  SELECT column_name, column_name  FROM table_name  [WHERE Clause]  [LIMIT N][ OFFSET M]

as关键字 select id as 序号, name as 名字, gender as 性别 from student(字段起别名)
       select s.id,s.name,s.gender from students as (表起别名)

消除重复行 select distinct 列1,... from 表名

where条件查询   SELECT field1, field2,...fieldN FROM table_name1, table_name2... [WHERE condition1 [AND [OR]] condition2.....
              比较运算符、逻辑运算符(and or not)、模糊查询(like %多个 _一个)、范围查询(bettween..and(区间内) in(指定值))、空判断(is null is not null)

like条件查询     SELECT field1, field2,...fieldN FROM table_name WHERE field1 LIKE condition1 [AND [OR]] filed2 = 'somevalue'

排序查询      select 显示字段名 from 表名 order by 排序字段名 asc|desc [,列2 asc|desc,..]   select * from students order by age desc,height des

分组查询      SELECT column_name, function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name;
          select gender from students group by gender;  单字段分组
          select gender,group_concat(id) from students group by gender;  分组并显示每组成员
          select gender,avg(age) from students group by gender;     分组+聚合,平均值
          select gender,count(*) from students group by gender;     分组+聚合,计总人数
          select gender,count(*) from students group by gender having count(*)>2;  分组+过滤,having过滤分组之后的结果
          select gender,count(*) from students group by gender with rollup;  分组后新增一行作为汇总

聚合查询      select count(字段名) from student   计总
          select max(id) from students where gender=2;  最大值
          select min(id) from students where is_delete=2;  最小值
          select sum(age) from students where gender=2;   求和
          select sum(age)/count(*) from students where gender=2; 或 select avg(id) from students where is_delete=0 and gender=2; 平均值

连接查询      INNER JOIN(内连接,或等值连接):查询的结果为两个表匹配到的数据。  
          select * from students inner join classes on students.cls_id = classes.id;


          LEFT JOIN(左/外连接)查询的结果为两个表匹配到的数据和左表特有的,右表不存在数据null填充
     
     select * from students as s left join classes as c on s.cls_id = c.id;


          
RIGHT JOIN(右/外连接)查询的结果为两个表匹配到的数据和右表特有的数据,左表不存在的数据null填充
          select * from students as s right join classes as c on s.cls_id = c.id;


自连接查询    select city.* from areas as cityinner join areas as province on city.pid=province.aidwhere province.atitle='山西省';
         select dis.* from areas as disinner join areas as city on city.aid=dis.pidwhere city.atitle='广州市';


子查询      标量子查询: 子查询返回的结果是⼀个数据(一行一列) select * from students where age > (select avg(age) from students);   
         列子查询: 返回的结果是⼀列(⼀列多行) 主查询 where 条件 in (列子查询)   select name from classes where id in (select cls_id from students);
         行子查询: 返回的结果是⼀行(⼀行多列) 主查询 where (字段1,2,...) = (行子查询)  select * from students where (height,age) = (select max(height),max(age) from students);
         子查询是⼀个完整的SQL


limit分页查询    limit 起始记录,记录数    
          select * from students limit 0,3; 意思是:从第下标为0的记录开始取,取3条

          select * from 表名 limit start=0,count  select * from students where is_delete=0 limit (n-1)*m,m

posted @ 2019-06-09 18:46  cool小伙  阅读(199)  评论(0编辑  收藏  举报