熊熊出没 发表于 2024-10-10 02:46:25

mysql底子汇总

命令行利用

   

[*]启动数据库服务:net start mysql;
[*]进入mysql:mysql -u root -p;
[*]退出mysql:shutdown;
底子利用

   库利用:


[*]查看所有库: show databases;
[*]创建库:create database 库名;
[*]使用库:use 数据库名;
[*]删除库:drop database 数据库名;
    表利用:


[*]查看所有表:show tables;
[*]查看表结构:desc 表名称;
[*]创建表:create table 表名(
字段名 字段范例 comment 解释,
字段名 字段范例 comment 解释
);


[*]删除表:drop table 表名;
[*]修改表名: alter table 表名 rename to 新的表名;
    字段利用:


[*]添加字段:alter table 表名 add 字段名 字段范例 ;
[*]修改字段范例: alter table 表名 modify 字段名 新的字段范例;
[*]修改字段: alter table 表名 change 旧字段名 新字段名 新的范例 ;
    数据利用:


[*]添加数据: insert into 表名 values (值1,值2…),(值1,值2,…);
[*]修改数据:update 表名 set 字段名1=值1,字段名2=值2 where 条件;
[*]删除数据: delete from 表名 where 条件;
查询利用

   底子查询:


[*]底子查询:select 字段名 from 表名 where 条件 ;
[*]分组查询:select 字段名 from 表名 where 条件 group by 字段名 having 条件;
[*]排序查询:select 字段名 from 表名 where 条件 order dy 字段名 desc/asc(降序/升序);
[*]分页查询:select 字段名 from 表名 limit 起始索引,查询记载数;
查询利用的顺序:
FROM -> WHERE -> GROUP BY -> SELECT -> ORDER BY -> LIMIT
    合并查询:


[*]内连接:select 字段名 from 表1 ,表二 where 条件;
[*]左外连接:select 字段名 from 表1 left join 表二 on 条件;
[*]右外连接:select 字段名 from 表1 right join 表二 on 条件;
[*]自连接查询:select 字段名 from 表 as 别名1,表名 as 别名2 on 条件;
[*]联合查询select 字段名 from 表1 where 条件 union select 字段名 from 表2 where 条件;
[*]子查询:select 字段名 from 表1 where 字段=(select 字段名 from 表2 where 条件);
约束

   常用约束;


[*]非空约束:not null;
[*]主键:primary key;
[*]自动增长:auto_increment;
[*]默认值:default;
[*]唯一:unique;
[*]逻辑条件:check;
    外键约束


[*]添加外键:alter table 表名 add constaint 外键名 foreign key(外键字段名) references 主表(主表字段名);
[*]删除外键:alter table 表名 drop foreign key 外键名;
事务

   事务常用语法:


[*]查看提交方式:select @@autocommit;
[*]修改提交方式:set @@autocommit=0/1(手动/自动);
[*]开启事务:start transaction;
[*]提交事务:commit;
[*]回滚:rollback;
视图

   

[*]创建视图:create view 视图名称 as (select 字段名 from 表名 where 条件);
[*]使用视图查询:select * from 视图名称;

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: mysql底子汇总