当前位置:   article > 正文

MySQL的基础汇总(包含60道练习题)~~_mysql练习题

mysql练习题

##数据库

  • 学习如何对数据进行增删改查操作

SQL

  • Structured Query Language 结构化查询语言, 此语言是用于程序员和数据库软件进行交流的语言

insert into user values("刘德华",50);

DBMS

  • DataBaseManagementSystem: 数据库管理系统(俗称数据库软件)
  • 常见的数据库管理系统(DBMS)
  • MySQL: Oracle公司产品, MySQL08年被Sun公司收购, Sun公司09年被Oracle收购. 开源产品 市占率第一
  • Oracle: Oracle公司产品, 闭源产品, 性能最强 价格最贵 市占率第二
  • SQLServer: 微软公司产品, 闭源产品, 市占率第三
  • DB2: IBM公司产品 ,闭源产品
  • SQLite: 轻量级数据, 安装包只有几十k ,只具备基础的增删改查功能

如何连接数据库执行SQL语句

  • 执行SQL语句之前需要先和数据库软件进行链接
  1. 从开始菜单中找到MariaDB或MySQL文件夹 展开后找到 MySQL Client 然后输入密码回车
  1. 如果使用的Linux或苹果系统 打开终端 然后输入 mysql -uroot -p 回车 然后输入密码回车
  1. 显示下图内容 代表成功
  1. 退出命令 exit;

数据库中如何保存数据

  • 数据库中以表为单位保存数据,表是存在于数据库之中, 保存数据则需要先建库再建表

SQL语句格式要求:

  1. 以;结尾
  1. 不区分大小写
  1. 可以包含空格和换行

数据库相关SQL

  1. 查询所有数据库
  • 格式: show databases;
  1. 创建数据库
  • 格式: create database 数据库名 charset=utf8/gbk;
  • 举例:
  • create database db1;
  • create database db2 charset=utf8;
  • create database db3 charset=gbk;
  • show databases;
  1. 查看数据库信息
  • 格式: show create database 数据库名;
  • 举例:
  • show create database db1;
  • show create database db2;
  • show create database db3;
  1. 删除数据库
  • 格式: drop database 数据库名;
  • 举例:
  • drop database db2;
  • drop database db3;
  1. 使用数据库
  • 执行表相关和数据相关的SQL语句之前必须使用了某一个数据库 否则会报错.
  • 格式: use 数据库名;
  • 举例:
  • use db1;

数据库相关练习题:

1. 创建 mydb1和mydb2 数据库 字符集分别为utf8和gbk

create database mydb1 charset=utf8;

create database mydb2 charset=gbk;

  1. 查询所有数据库检查是否创建成功

show databases;

  1. 检查两个数据库的字符集是否正确

show create database mydb1;

show create database mydb2;

  1. 先使用mydb2 再使用 mydb1

u

use mydb1;

  1. 删除两个数据库

drop database mydb1;

drop database mydb2;

表相关的SQL语句

  • 执行表相关的SQL语句必须提前使用了某个数据库,否则会报错

use db1;

  1. 查询所有表
  • show tables;
  1. 创建表
  • 格式: create table 表名(字段1名 类型,字段2名 类型)charset=utf8/gbk;
  • 举例:
  • create table person(name varchar(50), age int);
  • create table hero(name varchar(50),money int,type varchar(10))charset=utf8;
  • create table student(name varchar(50),chinese int,math int,english int)charset=gbk;
  • show tables;
  1. 查看表信息
  • 格式: show create table 表名;
  • show create table student;
  1. 删除表
  • 格式: drop table 表名;
  • drop table student;
  1. 修改表名
  • 格式: rename table 原名 to 新名;
  • 举例:
  • rename table person to p;
  1. 查看表字段
  • 格式: desc 表名; desc = description 描述
  • 举例:
  • desc p;

表相关练习题:

1. 创建数据库mydb3 字符集gbk 并使用

create database mydb3 charset=gbk;

use mydb3;

2. 创建t_hero英雄表, 有名字和年龄字段 默认字符集

create table t_hero(name varchar(20),age int);

3. 修改表名为hero

rename table t_hero to hero;

4. 查看表的字符集

show create table hero;

5. 查询表字段

desc hero;

6. 删除表

drop table hero;

7. 删除数据库

drop database mydb3;

表相关SQL(续)

use db1;

create table emp(name varchar(50));

  1. 添加表字段
  • 最后面添加格式: alter table 表名 add 字段名 类型;
  • 最前面添加格式: alter table 表名 add 字段名 类型 first;
  • 在xxx字段的后面添加格式: alter table 表名 add 字段名 类型 after xxx;
  • 举例:
  • alter table emp add age int;
  • alter table emp add id int first;
  • alter table emp add gender varchar(5) after name;
  • desc emp;
  1. 删除表字段
  • 格式: alter table 表名 drop 字段名;
  • 举例:
  • alter table emp drop gender;
  • alter table emp drop age;
  1. 修改表字段
  • 格式: alter table 表名 change 原名 新名 新类型;
  • 举例:
  • alter table emp change name age int;
  • alter table emp change id name varchar(50);

表相关SQL语句回顾:

  1. 查询所有表 show tables;
  1. 创建表 create table t1(name varchar(20),age int)charset=utf8/gbk;
  1. 查询表信息 show create table t1;
  1. 表字段 desc t1;
  1. 删除表 drop table t1;
  1. 修改表名 rename table t1 to t2;
  1. 添加表字段 alter table t1 add age int first/after xxx;
  1. 删除表字段 alter table t1 drop age;
  1. 修改表字段 alter table t1 change 原名 新名 新类型;

表相关练习题:

  1. 创建数据库mydb4 字符集utf8并使用

create database mydb4 charset=utf8;

use mydb4;

  1. 创建teacher表 有名字字段

create table teacher(name varchar(50));

  1. 添加表字段: 最后添加age 最前面添加id , age前面添加salary工资

alter table teacher add age int;

alter table teacher add id int first;

alter table teacher add salary int after name;

  1. 删除age字段

alter table teacher drop age;

  1. 修改表名为t

rename table teacher to t;

  1. 删除表

drop table t;

  1. 删除数据库

drop database mydb4;

数据相关SQL

  • 执行数据相关的SQL 必须已经使用了某一个数据库并且已经创建了保存数据的表
  • 建库建表

create database mydb5 charset=utf8;

use mydb5;

create table person(name varchar(50),age int)charset=utf8;

  1. 往表中插入数据(增)
  • 全表插入格式: insert into 表名 values(值1,值2,....);

insert into person values("Tom",18);

  • 指定字段插入格式: insert into 表名(字段1名,字段2名) values(值1,值2);

insert into person(name) values("Jerry");

  • 批量插入数据:

insert into person values("lilei",28),("hanmeimei",28);

insert into person(name) values("aa"),("bb"),("cc");

  • 插入中文数据:

insert into person values("刘德华",50);

如果执行上面SQL语句报如下错误

执行 set names gbk; 可以解决

  1. 查询数据
  • 格式: select 字段信息 from 表名 where 条件;
  • 举例:
  • select name from person;
  • select name,age from person;
  • select * from person;
  • select name from person where age=28;
  • select * from person where name='刘德华';
  1. 修改数据
  • 格式: update 表名 set 字段1名=值,字段2名=值 where 条件;
  • 举例:
  • update person set name="杰瑞" where name='Jerry';
  • update person set name="张学友",age=30 where name="刘德华";
  • update person set age=50 where name="aa";
  • update person set age=88 where age=28;
  • update person set age=10 where age is null;        
    • 删除数据
  • 格式: delete from 表名 where 条件;
  • 举例:
  • delete from person where age=88;
  • delete from person where name="张学友";
  • delete from person where age>10;
  • delete from person;

数据相关SQL语句回顾

  1. 增: insert into 表名(字段1名,字段2名) values(值1,值2),(值1,值2),(值1,值2);
  2. 查: select 字段信息 from 表名 where 条件
  3. 改: update 表名 set 字段名=值 where 条件
  4. 删: delete from 表名 where 条件;

综合练习题 :

1. 创建数据库day1db 字符集utf8并使用

create database day1db charset=utf8;

use day1db;

2. 创建t_hero表, 有name字段 字符集utf8

create table t_hero(name varchar(20));

3. 修改表名为hero

rename table t_hero to hero;

4. 最后面添加价格字段money, 最前面添加id字段, name后面添加age字段

alter table hero add money int;

alter table hero add id int first;

alter table hero add age int after name;

5. 表中添加以下数据: 1,李白,50,6888 2,赵云,30,13888 3,刘备,25,6888

insert into hero values(1,'李白',50,6888),(2,'赵云',30,13888),(3,'刘备',25,6888);

6. 查询价格为6888的英雄名

select name from hero where money=6888;

7. 修改刘备年龄为52岁

update hero set age=52 where name='刘备';

8. 修改年龄小于等于50岁的价格为5000

update hero set money=5000 where age<=50;

9. 删除价格为5000的信息

delete from hero where money=5000;

10. 删除表, 删除数据库

drop table hero;

drop database day1db;

数据类型

        整数: int(m) 和 bigint(m) , bigint等效java中的long, m代表显示长度,举例m=5,存18 查询到 00018 , 需要补零的话必须使用zerofill关键字

create table t1(age int(5) zerofill);

insert into t1 values(18);

select * from t1;

        浮点数: double(m,d) m代表总长度,d代表小数长度 54.432 m=5 d=3

create table t2(price double(5,3));

insert into t2 values(45.2312312123);

insert into t2 values(455.231); //报错

        字符串

  • char(m): 固定长度字符串 , m=5 存abc 占5个字符长度 , 应用场景: 当存储长度固定时,比如存储性别char(1) , 最大字符长度255
  • varchar(m):可变长度字符串, m=5 存abc 占3个字符长度, 最大值65535但是建议保存255以内长度的数据
  • text(m):可变长度字符串, 最大值65535 建议保存长度大于255的数据
  1. 日期
  • date: 只能保存年月日
  • time: 只能保存时分秒
  • datetime: 保存年月日时分秒, 最大值9999-12-31, 默认值为null
  • timestamp(时间戳:保存1970年1月1日到现在的毫秒数):保存年月日时分秒,
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/代码探险家/article/detail/869467
推荐阅读
相关标签
  

闽ICP备14008679号