MySQL数据库学习小记

1.创建数据库

create database xxx
show table database xx --查看创建数据库的语句
show  create table xx --查看数据表的创建语句
desc xx --显示表结构

2. 创建数据表

#创建数据表student

create tables if not exists students(
`id` int(4) not null auto_increment '学号',
`name` varchar(20) not null defualt '匿名' comment '姓名',
`sex` varchar(20) not null comment '性别',
`address` varchar(30) default null comment '住址',
primary key(`id`)
)engine=innodb default charset=utf8;

3.修改表

alter table xx(旧表名) rename as xx1(新表名) --重命名表xx为xx1
--添加字段
alter table xx add age int(11)
--修改表的字段(重命名,修改约束)
alter table xx modify age varchar(11) 

alter table xx change age age1 int(11)  --字段重命名

alter table teacher1 drop age1 -- 删除表teacher1中的字段age1

alter table if exists xx drop age1 -- 删除表xx中的字段age1

4.数据库操作语言(DML)

  1. insert
insert
---插入语句
instert into 表名 (字段) values ('value值')
--同一个字段插入多个值
insert into 表名(字段) values('value1'), ('value2'), ('value3')
--插入多条语句
insert into 表名([字段名1, 字段名2,...]) values ('value1', 'value2', 'value3',...)

 2. update


update
update `表名` set `colnum_name` = value, [`colnum_name`=value, ...] where [条件]
#将表中列明为name,id-1的值改为张三
update `表名` set `name`=’张三' where id =1

#修改多个属性,逗号隔开
update `student` set `name`='John',  `email`='23423423@qq.com' where id=1

where条件语句
=, <>, <, > , <=, >=, between...and..., and, or

  3.delete

delete 
语法:
删除指定数据
delete from `表名`  where `id`= 1;
truncate ’表名'
delete与truncate比较
truncate重新设置,自增列,计数器会归零
truncate 不会影响事务

  4.查找select

   

//查询student表中所有的内容
select * from student
#查询指定字段
select ` `, ` ` from student
#查询student表中studentno,studentname中的值并使用别名学号和学生姓名显示
select `studentNo` as 学号, `studentname` as 学生姓名 from student  
#函数 concat(a, b)  
select concat('姓名:', studentname) as 新名字 from student

#去重查询
select distinct `studentno` from result

#查询MySQL版本
select version()
#计算算法结果
select 300*3-1 as 计算结果
#查询自增的步长
select @@auto_increment_increment
#考生成绩+1
select `studentno`, `studentresult`+1 as '提分后' from result

数据库中的表达式:文本值,列,null,函数,计算表达式,系统变量....
select 表达式 from 表

where条件
运算符(两种写法都可以,一般写英文)
and  &&
or     ||
not   !

  5.模糊查询

   

#模糊查询
运算符
is null   如果操作符为null返回真
is not null 
between and    a between b and c 若a在b和c之间返回真
like           a like b     sql匹配,如果a匹配b,则结果为真
in        a in (a1, a2, a3,...)  假设a在a1,a2,... 之间,返回真


#查询只有两个字的刘姓学生
select `studentno`, `studentname` from `student` where studentname like '刘%_'
#查询名字中间有“嘉”字的同学
select `studentno`, `studentname` from `student` where studentname like '%嘉%'
#查询三个字的刘姓同学
select `studentno`, `studentname` from `student` where studentname like '刘__'
#查询学号在1001,1002,1003之间的学生
select `studentno`,`studentname` from `student` where studentno in (1001, 1002, 1003)
#查询住址在北京的学生
select * from student where address in ('北京朝阳')
#查询地址为空的同学
select * from student where address is null;
#查询地址不为空的同学
select* from student where address is not null

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值