MYSQL基本命令

1、MySQL的登陆
mysql -u root - p ******
2、创建数据库
create database 数据库名称;
3、查看数据库
show databases;
4、删除数据库
drop database 数据库名称;
5、使用数据
use 数据库
6 显示当前数据库有多少张表
show tables
7、创建表
create table 表名( 字段名 类型 );
8查看表结构
desc 表名
9、修改表字段的类型
alter table 表名 modify 字段名 类型(新类型)
10、增加一列
alter table 表名 add 字段名 类型
11、删除一列
alter table 表名 drop 字段名
12、修改表名
rename table 表名 (原始表名) to 修改表名
13、查看创建表的完整语句
show create table 表名
14、修改字符集
alter table 表名 character set 修改后的字符集
15、修改表字段
alter table 表名 change 原始列名 新列名 数据字段
16 删除表
drop table 表名
17查询所有数据
select * from table\G;
18
插入数据
insert into 表名(列名)values (值);
19、更新数据
update 表名 set 列名 = 值 ,列名1 =值1 where id =?;
20 修改数据库密码
(1)、update user set authentication_string=password(‘123456)
where user =“root” and houst =“localhost”
(2)flush privileges; // 刷新数据库权限相关表
mysqladmin -u root -p 123456 //第二种修改密码
21 删除数据
(1)、delete form 表名 where ID=?; //这种数据库是可以找回的
(2)、truncat table 表名;//truncat 删除是把表直接drop 掉,然后再创建一个同样的新表,删除的数据是不能找回的,执行的效率是比delete快的
22 条件查询
select ** from 表名 where 条件(= != < > <> between … and… i,n(set) ,null is not null and ,or not)
23 、 模糊查询
根据给定的关键字使用“like” 进行查询 通配符:’_’ 表示任意一个字符 ‘%’:表示任意一个或者多个字符。
24sql 分组查询(使用group by + having + 聚合函数)
select gender, group_contact(name) from student group by genger
//注释:group_contact() 这个函数将分组之后归类,group by 单独查询没有什么意义,因为显示只是查询的第一条,这样可以使用distinct 来代替,在使用是select (name) group by(name)
例子:slect department ,group_contact(‘score’),avg(score),sum() from student group by department;
slect department ,group_contact(‘score’),avg(score) as sc from student group by department having sc >100;
wnere 和having 的区别 ,having只能跟在group by 之后,而且having是在分组之后进行过滤,where 是在分组之前进行过滤
select department group_contact(salary),sum(salary) where salary>2000 groupby department having (sum(slalry)>8000;
25 数据完整性
1、数据完整性是什么?
:保证用户输入的数据是正确的
2、如何添加数据完整性?
:在创建表的时候中添加约束。
3、完整性的分类
(1)、实体完整性:表中的一行就是一个实体。
作用:标识每一行不重复
1.1 约束类型:
主键约束, 唯一约束 unicode(制定列的数据不能重复,但是可以为空),自动增长约束
(2)、域完整性
(3)、引用完整性
26、为什么要拆分表
避免数据表的冗余
27、合并结果集
1、什么是合并结果集?
:合并结果集就是吧两个select语句的查询结果合并到一起
合并结果集的两种方式
union:合并时去除重复记录
union all: 合并时不去除重复记录
样式:select * from table1 union select * from table2
select * from table1 union all select * from table2
注意事项:被合并的结果集:列数,列类型必须相同
28、笛卡尔积现象
:假设集合A={a,b} 集合B={1,2}
则两个的笛卡尔积为: {a,1}{a,2}{1,a}.{b,2},可以扩展到多个表
2隐式去除笛卡尔:使用where 条件
29内连接
使用 inner join
样例:select * from table1 inner join table2 on 条件 inner join table3 where condition
30、左连接
select * from table1 left join table2 on 条件 where 条件
31 右连接
select * from table1 right joun table2 on 条件 where condition
32 非等值连接
// 查询所有员工的姓名。工资、。所在部门的名称以及工资的等级

33 子查询
:一个select 语句中包含另外一个select语句查询,或者两个以上的select,如果select在where后是作为条件值,在from后面则是当作一个结果。
例子:select* from table where = {select id form table2 where name =""};
select * from { select * from table 2 }where name =’’} where id = ‘’
34 自连接
:就是把自己起个别名当作另外一张表进行查询
35 字符串函数
1、contact(s1,s2,s3) //将传入的字符串拼接成一个字符串 ,与null 拼接之后还是null select contact(s1,s2,s3);
2、 insert(str,x,y,insert); // str 字符串 x 从第几个开始,y 为替换多少个,要替换的数字
3、lower ,upper //转换大小写
4、right(str,x) left(str,y)//从左边获取几个,从右边获取几个
5、lpad(str,n,pad) rpad (str,n,pad) //用字符串pad 对str 或最右边进行填充,直接到的长度n个字符长度
6、trim(str) //去掉两个的空格
7、repeat(str,x) //将字符串重复多少次
8、replace(str ,‘c’,‘xxx’)
9/substr
36 数值函数
abs 绝对值 ceil 向上取整 floor乡下取整 rand
37 日期时间
查菜鸟
38 流程函数
if 函数 select if ( select name form table where id =1=‘XXX’,‘xxx’),'xxx;
ifnull

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值