mysql-基础语法

课前预习

目录

0. sql(结构化查询语言)标准sql

       0.1  ddl(数据定义语言)       0.2 dml(数据操作语言)       0.3 dcl(数据权限语言)

1. Mysql的增删改查语句(查询,新建查询)(“dml”对数据进行操作)

1.1 如何插入一条数据

1.2 如何删除一条数据

1.3 如何修改(更新)数据

1.4 查询数据的相关操作

1.4.0 查询,条件查询:

1.4.1 查询数据---去重

1.4.2 查询指定的行数

1.4.3 对查询结果进行分组,排序 (分组查询)

1.4.4 细说where条件

1.4.5 模糊查询

1.4.6 统计查询

1.4.7 连表查询 left join    right join      inner join 

2. 常用数据类型


 

0. sql(结构化查询语言)标准sql

       0.1  ddl(数据定义语言)
       0.2 dml(数据操作语言)
       0.3 dcl(数据权限语言)

ddl(表进行操作)增删改查
   create table【tablename】(
      name varchar(40),
      age  int(6),
      sex  varchar(4),
      money double,
      time datetime
   )

   drop table 【tablename】

   alter table salary add  dept varchar(20)
   alter table salary modify  dept varchar(50)
   alter table salary drop  dept 

 

1. Mysql的增删改查语句(查询,新建查询)(“dml”对数据进行操作)

增---》向数据库插入数据:

(1) insert into  表名 (列名)value (值)

(2)insert into  表名(列名1,列名2)values (值1,值2)

删---》向数据库删除数据

(1)delete 表名 where 条件

(2)delete from 表名 ------> 清空表数据

(3)delete from 表名 where name = "三枝夕夏" 

改---》向数据库修改数据

(1) update zard set hobby = '流行音乐' where name = '周杰伦'

(2)update 表名 set 列名1=值1,列名2=值2 where name = '周杰伦'

查---》查询数据库中部分数据

(1)select 列名 from 表名 where 筛选条件 group by 分组名 having 分组后筛选条件 order by [排序] asc/desc

1.1 如何插入一条数据

INSERT INTO table_name (field1,field2....fieldn.) VALUES (VALUES1,VALUES2,.....VALUESn)

insert into student (name,age) VALUES('lkw',18) 增添一条数据

insert into student (name,age) VALUES('lkq',10)

insert into student (name,age) VALUES('lke',19)

insert into student (name,age) VALUES('lkr',17)

insert into student (name,age) VALUES('lkt',12)

insert into student (name,age) VALUES('lky',14)

 

1.2 如何删除一条数据

语法: DELETE FROM 表名 where

没有where会删除整个表,有where会删除指定的数据

DELETE FROM student where age=9

成功之后提示:

Affected rows: 1

时间: 0.058s

1.3 如何修改(更新)数据

UPDATE 表命 SET 字段1=值,字段2=值.........................

update student set age=100  所有age都变成100岁

update student set age=28 where id=4  (改一条数据)

1.4 查询数据的相关操作

-- select 【列名】 from 【tablename】 
-- where 【筛选条件】 
-- group by 【分组列】 having 【分组后筛选条件】 
-- order by 【排序列】 asc/desc

1.4.0 查询,条件查询:

select * from table(表名)    (*代表查询所有字段)

SELECT * from student where id>=4 #查询id大于等于4的所有数据

eg:  SELECT * from zard where name ='坂井泉水'

1.4.1 查询数据---去重

select distinct 字段名 from 表名

SELECT DISTINCT name from student #查询名字去重的值

1.4.2 查询指定的行数

select * from student limit 1 查询出整个表的第一条数据

1.4.3 对查询结果进行分组,排序 (分组查询)

1.0)分组:

select * from 表名 group by 分组列 having 分组后筛选条件

eg: SELECT * from zard group by `value` having value = 23

 (1.1) 分组:

select 分组列分组函数 from emp group by 分组列

eg:select job,count(1),max(sal),min(sal),avg(sal),sum(sal) from emp group by job

(2)排序:

select * from student order by '排序列' desc/asc (此处根据“排序列”进行排序)

asc正序  desc 倒序

eg:SELECT * FROM zard WHERE name = '周杰伦' order by id ASC 

1.4.4 细说where条件

between and

(1)select * from student where id  BETWEEN 3 AND 5 ORDER BY id desc

(2)select * from student where id  >3 AND age < 100

 

(3) select * from zard where id in (1,10)   查询1或10,都有时全查

 

强调一点

如果查询的数据字段比较多,不建议用*号,写上的字段叫索引

Select * from table         #查询速度慢

Select name,age from table  #大量数据查询速度快,可读性高,易于维护,查询特定列。

1.4.5 模糊查询

select * from student where name like 'a%'

select * from student where name like '%a%'

select * from student where name like 'a%' _通配符代表任意一个字符

1.4.6 统计查询

(1)查询符合某特征的数据数量   

select count(name) from student where name =’mzm’

1.4.7 连表查询 left join    right join      inner join 

  

 (A lelf join B on A.x = B.x)#A合并于B的左边

2. 常用数据类型

  1. int 无符号(0,4294967295);有符号(-2147483648,2147483647)用途:存大整数
  2. Float 浮 点型 单精度
  3. Double 浮点型 双精度
  4. 时间类型:

Date日期值  YYYY-MM-DD

TIME 时间值 或者持续时间 HH:MM:SS

YEAR 年份值 YYYY

DATETIME 混合日期时间值 YYYY-MM-DD HH:MM:SS

Timestamp 时间戳 python中的time.time()

(5)Decimal(M,D)  M代表长度为6; D表示小数点保留多少位 

 

 (6)字符串类型

Char: 定长字符 ,0~255字节 ,如果字符数没达到定义的位数 ,会在后面补全空格数据存入数据库 。如果超过指定长, 会被截断。

Varchar: 0~~65535字节,如果字符数没达到定义的位数不会补充空格 。如果超过长度页会被截断。

TEXT: 长文本 0~65535

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Top Secret

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值