1-数据库学习-MySQL 安装和卸载

1-数据库学习-MySQL 安装和卸载

登录 在控制台输入 mysql -uroot -p123456
                如果密文登录  mysql -uroot -p 回车再输入密码
登录MySQL 确保mysql服务开启 
使用DOS命令
打开服务面板  services.msc
停止服务 net stop mysql
开启服务 net start mysql


修改数据库密码:


操作数据库:
SQL:结构化查询语言,是一种规范,所有的关系型数据库都遵循这个规范,允许各家数据库,有所差异,这些差异我们称之为方言
我们把查询数据库的SQL语句 会细分以下几种
DDL:建库,建表,删库 删表
DML::对表中的表头的增删改
DQL:主要是对表中的数据进行查询
DCL:他是对数据库的一些权限的设置

DDL

查询所有的数据库: show databases;
创建数据库: create database mytestdb;
删除一个数据库:drop database mytestdb;



建表: create table 表名(列名 类型(长度), 列名2 类型2(长度)...)
建表之前先切换库
use mydb;  -- 使用这个库
查看该库下的所有表  show tables;
-- 在这个mydb 库下去建表
 create table teacher(
    name varchar(10),
    age int,
    sex char(1),
    sal double(5,2)
 );
 
 -- 删除表  drop table 表名  例如: drop table student;
 
 -- 查询表结构 desc 表名;
 
 -- 修改表 alter(对表头进行增删改)
 
 -- 往表中增加一个字段 add 字段名 数据类型()
   alter table teacher add sex char(1);
 -- 删除一个字段 drop 字段名
   alter table teacher drop sex;
 -- 修改字段的数据类型 modify 旧字段名 新类型
   alter table teacher modify tname char(10);
 -- 修改字段名称  change 旧列名 新列名 char(10);
   alter table teacher change tname username char(10);
 -- 使用 change 也能修改列的数据类型
  alter table teacher change username username varchar(10);
  
  
--修改表名 alter table 旧表名 rename to 新表名;
alter table teacher rename to dog;
 

 
 

DML:对表中的行 进行增删改


增: insert 
	-- varchr char 类型的值需要用单引号 引起来
    insert into teacher(username,tage,tsal,birthday) values('zhangsan',18,999.99,null);
    
    -- 日期的值也需要用单引号 引起来
       insert into teacher(username,tage,tsal,birthday) values('lisi',18,999.99,'1985-12-12 14:20:20');

-- 给表中个别字段插入值

 insert into teacher(username,tage) values('wangwu',23);

-- 你如果要给所有字段插入值,可以简写
insert into teacher values('zhaoliu',18,999.99,null);


-- 删除一行或多行 delete 
delete from teacher;  -- 没有加条件的删除,表中的所有行就会全部删除

-- 条件删除 where 条件 =、!=、<>(不等于)、<、<=、>、>=; and &&  or ||

delete from teacher where username='zhaoliu';


delete from teacher where username='zhaoliu' and birthday='2019-12-06 15:10:54';

-- 修改 update
update teacher set username='hehe';  -- 没有条件的修改
   
-- 带有条件的修改 where
update teacher set username='zhansan' where tage=23; 

-- 修改多个字段
update teacher set username='wangwu',tage=30,birthday='2018-12-06 15:10:54' where tage=23; 


DQL:对表中的行 查询

查询表中的数据 select
-- 查询表中所有的数据
select * from 表名;

-- 查询个别字段的值
select username,tage from 表名;

-- 条件查询 where 
=、!=、<>(不等于)、<、<=、>、>=;
			BETWEEN…AND;  在什么范围之间
			IN(set);
			IS NULL;为空
			IS NOT NULL 不为空
			AND; 并且
			OR;   或者
			NOT;非



CREATE TABLE emp(
	empno INT,
	ename VARCHAR(50),
	job VARCHAR(50),
	mgr INT,
	hiredate DATE,
	sal DECIMAL(7,2),
	comm decimal(7,2),
	deptno INT
) ;

-- 查询工资 大于 500 的员工信息
select * from emp where sal>1000;

-- 查询工资 大于 等于1000 小于等于2000 的员工信息

select * from emp where sal>=1000 and sal<=2000; 

-- 查询工资大于1000 或奖金 大于100的

select * from emp where sal>=1000 or comm>=100;


-- 查询工资 大于 等于1000 小于等于2000 的员工信息
BETWEEN…AND;  在什么范围之间

select * from emp where sal between 1000 and 2000;


-- 查询奖金是 300 500 的人
select * from emp where comm=300 or comm=500;

select * from emp where comm in(300,500);

-- 查询奖金为null
-- null 值 不能用 = != 去判别
select * from emp where comm is null;
-- 查询奖金不为 null的
select * from emp where comm is not null;

-- 字段可以进行数学运算
select ename,sal,comm from emp;

select ename,sal,comm,sal*12 from emp;

-- 可以给字段起个别 as  注意 as 可以省略不写

select ename as '姓名',sal as '工资',comm as '奖金',sal*12 as '年薪' from emp;


-- 模糊查询 like
-- 模糊查询使用过的通配符  _ 匹配单个任意字符  % 匹配多个任意字符
-- 我要查询姓名包含A的人
  select * from emp where ename like '%A%';
  
--查询姓名是A开头的人
 select * from emp where ename like 'A%';
-- 查询姓名是K结尾的人
 select * from emp where ename like '%K';
 
 -- 查询第二个字符是M的人
  select * from emp where ename like '_M%';
  --查询这个名字是5个字符组成的人呢
   select * from emp where ename like '_____';
      select * from emp where ename like 'SMITH';
      
      
      
--查询年薪 并加上奖金  null  值参与运算结果 null

select ename,sal,comm,sal*12+comm from emp;
 
-- 如果奖金是null值 我们想要当0来处理 ifnull()
	select ename,sal,comm,sal*12+ifnull(comm,0) from emp;
	
-- 对结果去重distinct
select distinct sal from emp where sal=3000;


排序:查询所有员工信息安装工资升序排列  order by 工资
-- ASC 升序  DESC 降序
 select * from emp order by sal asc;
 
  select * from emp order by  ename desc;
 
 -- 可以定义第二排序条件
 
 select * from emp order by sal asc,comm desc;


-- 聚合函数  null值不参与  count(comm)
select count(comm) as zongshu from emp;

select count(*) as zongshu from emp;

-- MAX()

select max(sal) as zdz from emp;

select min(sal) as zdz from emp;

-- avg() 平均值
select avg(sal) as zdz from emp;

select sum(sal)/count(*) from emp;

-- sum 求和
select sum(sal) from emp;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值