Navicat
SQL语句
连接数据库
mysql -h ip -p 端口 -u 用户名 -p 回车后输入密码
断开数据库
exit
数据库的管理
显示所有数据库:show databases;
选择数据库:use 数据库的名字;
新建数据库:create database 数据库名字 default charset utf8mb4;
修改数据库:alter database 数据库名字 default charset utf8mb4;
删除数据库:drop database 数据库名字;
表管理
1.显示所有表:show tables;
2.创建表: create table 表名 (字段属性);
3.字段属性:
字段名 字段类型(字段长度):id int(16)
非空:not null
默认值:default 值
注释:comment “注释内容”
主键:primary key
索引:index
附加方法: 自动递增/auto_increment
获取当前时间/now()
4.修改表:alter table 表名 操作 (字段属性):
添加字段: add
alter table 表名 add 字段名 字段类型(字段长度) 其他;
删除字段:drop
dr opa lter table 表名 drop 字段名;
修改字段:change,modify
changea lter table 表名 change 旧字段名 新字段名 字段类型(字段长度) 其他;
modifya lter table 表名 modify 字段名 字段类型(字段长度) 其他;
修改表名:rename
r enamea lter table 表名 rename 新表名;
添加索引:alter table t_class add index(cname);
alter table 表名 add index(字段名);
删除索引:a lter table 表名 drop index 字段名;
5.删除表:drop table 表名;
6.查看表属性:desc 表名;
数据的操作
1.增:insert into 表名 (字段1,字段2) values (值1,值2);
例子:insert into t_student (sname,sex,age) values (‘张东降’,‘男’,27);
2.删:delete from 表名 where 条件;
例子:delete from t_student where id = 6;
4.改:update 表名 set 字段1=值1,字段2=值2 where 条件;
例子:update t_student set sname = ‘张西生’ where id =5;
5.查:select * from 表名;
例子:select * from t_student;
select * from 表名 where 字段1=值1 and 字段2 = 值2;
select 字段1,字段2 from 表名;
6.多表联查:
内关联/join/inner joi:
select * from 表1 join 表2 on 表关系 joi n 表3 on 表关系;
列子:select * from t_student join t_grade on t_ student.id = t_grade.sid;
外关联:
左关联/left join
右关联/right join
嵌套查询:s elect * from (select * from 表) as a;
SQL的判断符号
SQL的逻辑关联符号
and,or,and not,or not
SQL语句的常用技巧
SQL语句的聚合函数
min() 最小值
max()最大值
sum()和
avg()平均值
count()所有的
数据库的表类型
1.虚拟表
2.实实在在的表
3.临时表:在断开数据库的连接后会自动消失
create temporary table 表名(字段属性);
复制表
创建表的时候直接复制数据:create temporary table 表名(select * from 表名);
先把表创建好,然后再复制数据:i nsert into 表名 (select * from 表名);
事务
事务只针对于数据的操作
开始事务:begin;
确认事务:commit;
回滚事务: rollback;
SQL分类
数据库的的优化