MySQL

1、基本概念

主键(primary key):int类型、无符号、自动递增、唯一的标识一条记录

2、SQL语句

(1)创建表

  CREATE TABLE table_name (column_name column_type)

(2)删除表

  DROP TABLE table_name 

(3)插入数据

  INSERT INTO table_name ( field1, field2,...fieldN ) VALUES ( value1, value2,...valueN )

  注:插入多条数据的两种方式

  • 写多条插入语句

  INSERT into student3 VALUES(0,'亚瑟1',20,145);

  INSERT into student3 VALUES(0,'亚瑟2',20,145);

  INSERT into student3 VALUES(0,'亚瑟3',20,145);

  • 一条语句插入多条记录,value使用逗号分隔

  INSERT into student3 VALUES(0,'亚瑟1',20,145),(0,'亚瑟2',20,145),(0,'亚瑟3',20,145); 

(4)修改数据

  UPDATE table_name SET field1=new-value1, field2=new-value2 [WHERE Clause]

(5)删除语句

  DELETE FROM table_name [WHERE Clause]

DROP TABLE IF EXISTS students;
CREATE TABLE students(
    studentNo varchar(10) PRIMARY KEY,
    name VARCHAR(10),
    sex varchar(1),
    hometown varchar(20),
    age tinyint(4),
    class varchar(10),
    card varchar(20)
);
INSERT into students values
('001','王昭君','','北京','20','1班','1234567891'),
('002','诸葛亮','','上海','18','2班','1234567892'),
('003','张飞','','南京','24','3班','1234567893'),
('004','白起','','安徽','22','4班','1234567894'),
('005','大乔','','天津','19','3班','1234567895'),
('006','孙尚香','','河北','18','2班','1234567896'),
('007','百里玄策','','山西','20','1班','1234567897'),
('008','小乔','','河南','15','3班','null'),
('009','百里守约','','湖南','21','1班',''),
('010','妲己','','广东','26','2班','1234567898'),
('011','李白','','北京','30','4班','1234567899'),
('012','孙膑','','新疆','26','3班','12345678910')

3、查询语句

(1)查询所有字段

  select * from 表名

(2)查询指定字段

  select 字段1,字段2 from 表名

(3)使用as给列名或表名起别名

  例:select name as 姓名,sex as 性别 from students as s

(4)使用distinct消除重复行

  select distinct 列1,... from 表名

(5)条件查询

  select 字段1,字段2,...from 表名 where 条件

(6)模糊查询

  • like
  • %表示任意多个任意字符
  • _表示一个任意字符

  例:select * from students where name like '孙%'

(7)范围查询

  • in
  • between...and

  例:select * from students where hometown in ('北京','上海') 

         select * from students where age between 18 and 20

(8)空判断——is null

  例:select * from students where card is (not)null

(9)排序

  order by ... asc/desc    默认正序

  例:select * from students order by age,studentno desc

    select * from students order by convert(name using gbk)——对中文数据进行排序

(10)聚合函数

  例:select count(*) from students——count(*)代表一行记录任意字段有值,就会统计在内

  select max(age) from students where sex='女'
  select min(age) from students where class='1班'
  select sum(age) from students where hometown='北京'
  select avg(age) from students where sex='女'

(11)分组——通常和聚合运算结合使用

  例:select sex,count(*) from students group by sex

    select sex,count(*) from students group by sex having sex='男'——having必须跟在group by 后

(12)分页

  limit start,count

  例:select * from students limit 0,3

4、连接查询

(1)等值连接——获取两个表中字段匹配关系的记录

  方式1:select * from 表1,表2 where 表1.列=表2.列

    注:此方式会产生笛卡尔积,产生临时表

  方式2(内连接):select * from 表1 inner join 表2 on 表1.列=表2.列

(2)左连接——获取左表所有记录,即使右表没有对应匹配的记录

   select * from 表1 left join 表2 on 表1.列=表2.列

(3)右连接——获取右表所有记录,即使左表没有对应匹配的记录

   select * from 表1 right join 表2 on 表1.列=表2.列

5、子查询

(1)关键字

  • in 范围

    格式:主查询 where 条件 in (列子查询)

  • any|some 任意一个

    格式:主查询 where 列=any(列子查询)

    在条件查询的结果中匹配任意一个即可,等价于in

  • all

    格式:主查询 where 列=all(列子查询)   等于里面所有

    格式:主查询where 列<>all(列子查询)   不等于其中所有

原文地址:https://www.cnblogs.com/wangzicong/p/15692666.html