-- 直接创建数据库db1

create database db1;

-- 判断是否存在,如果不存在则创建数据库db2

create database  if not exists db2;

-- 创建数据库并指定字符集为gbk

create database db3 character set gbk;

-- 显示创建数据库的SQL

show create database db3;

-- 查看所有的数据库

show databases;

-- 将db3数据库的字符集改成utf8

alter database db3 character set utf8;

-- 删除db2数据库

drop database db2;

-- 创建一个数据库day15

create database day15;

-- 使用day15

use day15;

-- 查看有哪些表

show tables;

-- 创建student表包含id整数,name变长字符串长20,sex性别定长型1,birthday字段日期类型

create table student (
   id int,    -- 编号
   `name` varchar(20),  -- 姓名    ` 重音符号 Esc下面 ~ 同一个键
   sex char(1),  -- 性别
   birthday date -- 生日
);

-- 查看mysql数据库中的所有表

use mysql;
show tables;

-- 查看student表的结构

use day15;
desc student;

-- 查看student的创建表SQL语句

show create table student;

-- 创建s1表,s1表结构和student表结构相同

create table s1 like student;
describe s1;

-- 直接删除表s1表

drop table s1;

-- 判断表是否存在并删除s1表

drop table if exists s1;

desc student;

-- 为学生表添加一个新的字段remark,类型为char(20)

alter table student add remark char(20);

-- 将student表中的remark字段的改成varchar(100)

alter table student modify remark varchar(100);

-- 将student表中的remark字段名改成intro,类型varchar(30)

alter table student change remark intro varchar(30);

-- 删除student表中的字段intro

alter table student drop intro;

-- 将学生表student改名成student2

rename table student to student2;

rename table student2 to student;

-- DML

-- 查询表中所有的行和列, * 表示所有的列

select * from student;

-- 向学生表中添加1条记录,在SQL中字符串都是使用单引号,但mysql中可以使用双引号,日期也使用单引号

insert into student values (1, '牛魔王', '男', '2000-11-11');

-- 插入部分数据,往学生表中添加 id, name数据

insert into student (id,name) values (2, '红孩儿');

-- 插入多条记录

insert into student values (3, '太上老君', '男', '2000-11-11'),(4, '孙悟空', '男', '2000-11-11'),(5, '孙悟天', '男', '2000-11-11');

-- 不带条件修改数据,将所有的性别改成女

update student set sex = '女';

select * from student;

-- 带条件修改数据,将id号为2的学生性别改成男

update student set sex='男' where id=2;

-- 一次修改多个列,把id为3的学生,修改性别为男,生日:1992-02-22

update student set sex='男', birthday='1992-02-22' where id=3;

-- 带条件删除数据,删除id为3的记录

delete from student where id=3;

-- 不带条件删除数据,删除表中的所有数据

delete from student;

-- 删除表结构,再重建表结构

truncate student;

-- * 表示所有的列(字段名)

select * from student;

-- 查询student表中的id 和name 列

select id,name from student;

select name,sex from student;

-- 查询student表中id 和name 列,id的别名为编号,name的别名为"姓名"

select id as 编号, name as 姓名 from student;   -- 注:别名没有引号

select id 编号, name 姓名 from student;

-- 查询学生来至于哪些地方
-- 修改表结构,添加address varchar(20)

alter table student add address varchar(20);

-- 只查询address这一列

select address from student;

-- 去掉重复的记录

select distinct address from student;

select distinct name, address from student;

-- 修改student表结构,添加数学和英语成绩列

alter table student add math int, add english int;

select * from student;

-- 查询姓名、数学,将数学每个减10分

select name as 姓名, math as 数学 from student;
select name as 姓名, math-10 as 数学 from student;
select name as 姓名, math*.5 as 数学 from student;

-- 查询所有列与math + english的和并使用别名”总成绩”

select *, math+english as 总成绩  from student;

-- 查询所有地址在广州的学生

select * from student where address='广州';

创建表

CREATE TABLE student3 (
  id int,  -- 编号
  name varchar(20), -- 姓名
  age int, -- 年龄
  sex varchar(5),  -- 性别
  address varchar(100),  -- 地址
  math int, -- 数学
  english int -- 英语
);

INSERT INTO student3(id,NAME,age,sex,address,math,english) 
VALUES (1,'马云',55,'男','杭州',66,78),(2,'马化腾',45,'女','深圳',98,87),
(3,'马景涛',55,'男','香港',56,77),(4,'柳岩',20,'女','湖南',76,65),(5,'柳青',20,'男','湖南',86,NULL),
(6,'刘德华',57,'男','香港',99,99),(7,'马德',22,'女','香港',99,99),(8,'德玛西亚',18,'男','南京',56,65);

select * from student3;

-- 查询math分数大于80分的学生

select * from student3 where math > 80;

-- 查询english分数小于或等于80分的学生

select * from student3 where english <=80;

-- 查询age等于20岁的学生

select * from student3 where age = 20;

-- 查询age不等于20岁的学生,注:不等于有两种写法

select * from student3 where age <> 20;
select * from student3 where age != 20;

-- 查询age大于35且性别为男的学生(两个条件同时满足)

select * from student3 where age>=35 and sex='男';

-- 查询age大于35或性别为男的学生(两个条件其中一个满足)

select * from student3 where age>=35 or sex='男';

-- 查询id是1或3或5的学生

select * from student3 where id=1 or id=3 or id=5;

-- 查询id是1或3或5的学生

select * from student3 where id in (2,4,5);

-- 查询id不是1或3或5的学生

select * from student3 where id not in (1,3,5);

-- 查询english成绩大于等于77,且小于等于87的学生

select * from student3 where english between 77 and 87;

-- 查询姓马的学生

select * from student3 where name like '马%';

-- 查询姓名中包含'德'字的学生

select * from student3 where name like '%德%';

-- 查询姓马,且姓名有2个字的学生

select * from student3 where name like '马_';
select * from student3 where name like '马__';

-- 查询英文成绩为NULL的学生

select * from student3 where english is null;   -- 正确的
select * from student3 where english = null;  -- 错误的

-- 查询英语成绩不为NULL的学生

select * from student3 where english is not null;

-- 查询姓名和英语成绩,如果英语为null,则显示为0分
-- ifnull(列名,默认值) 如果这一列有值,显示这一列的值,如果为null,就显示默认值。

select name, ifnull(english,0) 英语 from student3;

创建表

create database day16;
use day16;

CREATE TABLE student3 (
  id int,
  name varchar(20),
  age int,
  sex varchar(5),   
  address varchar(100),
  math int,
  english int
);
INSERT INTO student3(id,NAME,age,sex,address,math,english) VALUES (1,'马云',55,'男','杭州',66,78),(2,'马化腾',45,'女','深圳',98,87),
(3,'马景涛',55,'男','香港',56,77),(4,'柳岩',20,'女','湖南',76,65),(5,'柳青',20,'男','湖南',86,NULL),(6,'刘德华',57,'男','香港',99,99),
(7,'马德',22,'女','香港',99,99),(8,'德玛西亚',18,'男','南京',56,65);


select * from student3;

-- 查询所有数据,使用年龄降序排序

select * from student3 order by age ;
select * from student3 order by age  desc;

-- 按姓名排序

select * from student3 order by name;

-- 原因:因为字符集使用是utf-8,默认按汉字在码表中位置进行排序
-- 应该使用GBK编码对这一列进行排序

select * from student3 order by convert(name using gbk);

insert into student3 (id,name) values (99, '六六');

-- 查询所有数据大于20岁的学生,在年龄降序排序的基础上,如果年龄相同再以数学成绩升序排序

select * from student3 where age > 20 order by age desc, math asc;

-- 查询学生总数

select count(id) from student3;   -- 建议选择的列不能为空
select count(english) from student3;
select count(*) from student3;  -- 统计所有的列

-- 查询年龄大于40的总数

select count(id) from student3 where age > 40;  -- 先过滤行,再统计

-- 查询数学成绩总分

select sum(math) from student3;

-- 查询数学成绩平均分

select avg(math) from student3;

-- 查询数学成绩最高分

select max(math) 最高分 from student3;

-- 查询数学成绩最低分

select min(math) 最低分 from student3;

delete from student3 where id=99;

-- 按性别进行分组

select * from student3 group by sex;

-- 按性别进行分组,求男生和女生数学的总成绩

select sum(math) from student3 group by sex;

-- 通常要加上分组这一列来查询

select sex, sum(math) 数学总成绩 from student3 group by sex;

-- 查询男女各多少人

select sex,count(*) 人数 from student3 group by sex;

-- 查询年龄大于25岁的人,按性别分组,统计每组的人数
-- 1. 过滤一些行

select * from student3 where age > 25;

-- 2. 再结果中进行分组

select * from student3 where age > 25 group by sex;

-- 3. 统计每组人数

select sex, count(*) 人数 from student3 where age > 25 group by sex;
-- 错误的
SELECT sex, COUNT(*) FROM student3 WHERE age > 25 GROUP BY sex WHERE COUNT(*) >2;
-- 正确
SELECT sex, COUNT(*) FROM student3 WHERE age > 25 GROUP BY sex having COUNT(*) >2;

插入数据

INSERT INTO student3(id,NAME,age,sex,address,math,english) VALUES 
(9,'唐僧',25,'男','长安',87,78),
(10,'孙悟空',18,'男','花果山',100,66),
(11,'猪八戒',22,'男','高老庄',58,78),
(12,'沙僧',50,'男','流沙河',77,88),
(13,'白骨精',22,'女','白虎岭',66,66),
(14,'蜘蛛精',23,'女','盘丝洞',88,88);

select * from student3;

-- 查询学生表中数据,从第3条开始显示,显示6条。

select * from student3 limit 2, 6;  -- 跳过2行,返回6行

-- 每页显示5条
-- 第一页:跳过0条,显示5条

select * from student3 limit 0, 5;  
select * from student3 limit 5;  

-- 第二页:跳过5条,显示5条

select * from student3 limit 5, 5;  

-- 第三页:跳过10条,显示5条

select * from student3 limit 10, 5;   -- 最后一页,有多少条记录,返回多少条

-- 语句正确

delete from student3 where id=99;

select * from student3;

insert into student3 (id, name, age) values (100, '李四', 200);

-- 创建表学生表st1, 包含字段(id, name, age)将id做为主键

create table st1 (
   id int primary key,
   name varchar(20),
   age int
)

desc st1;

select * from st1;

-- 插入重复的主键值

insert into st1 values(1,'张三',20);
-- Duplicate(重复) entry记录 '1' for key 'PRIMARY'
insert into st1 values(1,'张三',20);

-- 插入NULL的主键值

-- Column 列 'id' cannot be null
insert into st1 values(null, '李四',20);

-- 删除st1表的主键

alter table st1 drop primary key;
desc st1;

-- 在现有的表中添加主键

alter table st1 add primary key (id);

-- 联合主键

alter table st1 add primary key (id,name);

select * from st1;

-- 主键自增长
-- 创建学生表st2, 包含字段(id, name, age)将id做为主键并自动增长

create table st2 (
   id int primary key auto_increment,
   name varchar(20),
   age int
)

desc st2;

-- 插入数据

insert into st2 (name,age) values ('张三',20);

select * from st2;

-- Column 列 count 个数 doesn't match 匹配 value 值 count 数 at row 行 1

insert into st2 values ('张三',20);

-- 占位

insert into st2 values (null, '张三',20);

-- 将主键的起始值设置为1000

alter table st2 auto_increment = 1000;

insert into st2 values (null, '张三',20);

-- 0零填充

create table st2a(
  id int(3) zerofill,
  name varchar(20)
);

desc st2a;

insert into st2a values(1,'张三'),(2,'李四'),(9999,'王五');

select * from st2a;

-- 创建学生表st3, 包含字段(id, name),name这一列设置唯一约束,不能出现同名的学生

create table st3(
   id int,
   name varchar(20) unique
);

desc st3;

-- 添加一个同名的学生

insert into st3 values(1, '张三');
select * from st3;
-- Duplicate重复 entry记录 '张三' for key 'name'
insert into st3 values(2, '张三');

-- 创建表学生表st4, 包含字段(id,name,gender)其中name不能为NULL

create table st4 (
   id int primary key auto_increment,
   name varchar(20) not null,
   gender char(1)
);

-- 添加一条记录其中姓名不赋值

insert into st4 (name,gender) values ('张三','男');

select * from st4;
-- Column 'name' cannot be null
insert into st4 (name,gender) values (null,'男');

-- 创建一个学生表 st5,包含字段(id,name,address), 地址默认值是广州

create table st5(
  id int primary key auto_increment,
  name varchar(20),
  address varchar(20) default '广州'
);

-- 添加一条记录,使用默认地址

insert into st5 (name) values ('张三');

-- 不符合要求

insert into st5 values (null, '李四',null);

insert into st5 values (null, '李四',default);
select * from st5;

-- 添加一条记录,不使用默认地址

insert into st5 values (null, '李四','深圳');
如果觉得我的文章对你有用,请随意赞赏