1.链接mysql: mysql -uroot -p123456 ; 注释: -u后跟是用户名,-p密码
2. 创建数据库: createdatabase 库名字;3. 查询所有数据库: showdatabases;4. 进入某个数据库: use 库名字;5. 查看某个数据库中的表: showtables;
创建数据表
1创建数据库
createdatabase 库名字;2.创建数据表
createtableifnotexists stu(id intnotnullprimarykeyauto_increment, name varchar(20), age int)
数据表插入数据
1.第一中插入方式
insertinto stu values('zhangsan',20)2.第二种插入方式
insertinto stu (name, age)values('zhangsan',20)
数据表数据更新
1.修改某一条数据
update stu set age=30where name='zhangsan';2.修改全部数据
update stu set age=10;
数据删除操作
1.删除数据表中的一条数据
deletefrom stu where name='xxy';2. 删除数据表中所有数据
deletefrom stu;3.删除数据表
droptable 数据表名
4.删除数据库
dropdatabase 数据库名
5.清空数据表的数据
truncatetable stu;
alter 相关操作
1.修改数据表名
altertable stu rename student
2.添加一列
aletr table stu add phone int;(字段和数据类型)3. 数据表中添加多个列
altertable stu add email varchar(20), class varchar(30);4.指定在某列后面或前面添加一列
altertable stu add habbit varchar(20)first/after age;5.删除数据表中某列
altertable stu drop phone;(phone: 列名字)6.删除数据表的主键
altertable stu dropprimarykey7.修改某一列为主键
altertable stu addprimarykey(name);8.修改某个字段的数据类型
altertable stu modify age varchar(20);9. 设置某个字段为唯一
altertable stu addunique(name);10: 设置某个字段为自增的主键
altertable stu modify id intnotnullprimarykeyauto_increment;
数据表查询
1.查询所有数据
select*from stu;2.根据某个条件查询数据
select*from stu where name='zhangsan'3. 去重查询的数据
selectdistinct name from stu;4.条件查询区间查询
select*from stu where age >20and age<50;select*from stu where age between20and50;5.数据查询排序: orderby 默认是asc: 升 序,desc: 降序
select*from stu orderby age desc;6.模糊查询:%是占位符
select*from stu where name like'%三%';4. 子查询: 执行顺序是先获取age的数据, 然后在根据age所在区间进行查询
select*from stu where age in(select age from stu where age >20and age <30);8.多表查询
select stu_name,avg(degree)from student , score where student.stu_num=score.stu_num groupby student.stu_name;9.联表查询,join/leftjoin/rightjoinselect stu_name,avg(degree)from student join score on student.stu_num = score.stu_num;5.groupbyorderby 同时运用时,groupby 是在orderby 前面
利用正则进行搜索
正则表达式可以直接利用
# 匹配 sname 字段中包含 4 的数据select*from student where sname regexp'4';#正则表达式匹配: sname 字段中含有数据 [0-9], 匹配数据一次、多次select*from student where sname regexp'[0-9]+';# 查询sno 是4, 或者6 的数据select*from student where sno regexp'4|6';