zoukankan      html  css  js  c++  java
  • MySQL常用命令总结

    显示所有数据库:show databases;

    新建数据库:create database dbname;

    删除数据库:drop database dbname;

    切换数据库:use dbname;

    新建表:create table tbname(clo1 type ,col2 type,……);

    显示所有表:show tables;

    根据已有表结构创建新表:

    create table new_table_name like old_table_name;

    create table new_table as select col1,col2,…. from old_table only;

    显示表结构:describe table_name;

    删除表:drop table table_name;

    增加一列:alter table tbname add column col type;

    删除一列:alter table tbname drop column name;

    修改列属性:alter table tbname modify col typr;

    增加主键:alter table tbname add primary key (col);

    删除主键:alter table tbname drop primary key(col);

    修改表名:alter table tbname rename new;

    创建索引:create index idxname on tbname (col,….);

    删除索引:drop index idxname on tbname;

    查看索引:show index from tbname;

    创建视图:create view name as select (col,….) from tbname;

    删除视图:drop view name;

     

    增删改查:

    增:insert into tbname (col,col,…) values(value1,value2,…);

    删:delete from tbname where 条件;

    改:update tbname set field=value where条件;

    查:select filed from tbname where 条件;

     

    求和:select sum(col) as name from tbname;

    平均:select avg(col) as name from tbname;

    最大:select max(col) as name from tbname;

    最小:select min(col) as name from tbname;

     

    外键定义:外键是指引用另一个表中的一列或者多列,被引用的列应该具有主键约束或者唯一约束,外键用于建立和加强两个表数据之间的连接,被引用的表是主表,引用外键的表是从表。

    多表操作:

    建立外键:alter table tbname add constraint 外键名 froeign key(外键字段名) references 外表表名(主键字段名);

    示例:alter table student add constraint fk_id foreign key(gid) references grade(id);

    删除外键:alter table tbname drop foreign key 外键名;

    示例:alter table student drop foreign key fk_id;

    添加数据:当有外键的表添加数据的时候其字段值只能是被关联表中已有的数据,例如grade中id字段只有1和2,那么student中的gid值只能设为1和2.

    删除数据:因为grade表和student表具有关联关系,参照列中的被参照值是不能被删除的,所以想删除grade表中的数据必须先将student中关联数据都删除掉后再删除grade中的数据。

    连接查询:

    首先建立两个表:

    表1:create table department (did int not null primary key, dname varchar(32));

    表2:create table employee(id int not null primary key, name varchar(32), age int, did int not null);

    插入数据

    insert into department(did,dname)VALUES (1,'网络部'),(2,'媒体部'),(3,'研发部'),(5,'人事部');

    Insert into employee(id,name,age,did) VALUES (1,'王红',20,1),(2,'李强',22,1),(3,'赵四',20,2),(4,'郝娟',20,4);

    交叉连接:交叉连接返回的结果是两个连接表中所有数据的笛卡尔集,即返回第一个表中符合条件的数据乘以第二个表中符合条件的数据。

    语句:select 字段 from tbname1 cross join tbname2;

    例如:select * from department cross join employee;

    返回结果为:有16行(4*4)

    内连接:使用比较运算符对两个表中的数据进行比较,并列出与连接条件匹配的数据

    语句:select 字段 from tbname1 [inner] join tbname2 on tbname1.关系字段=tbname2.关系字段;

    例如 select name from employee join department on employee.did=department.did;

    返回结果为:3行(did为1,2,3的数据)

    自连接:如果在一个连接查询中涉及的两个表其实是同一个表,这种查询称为自连接查询

    语句:select p1.* from tbname as p1 join tbname as p2 on p1.字段 = p2.字段;

    示例:查询 name为王红的人所属部门员工

    select p1.* from employee as p1 join employee as p2 on p1.did = p2.did where p2.name = ‘王红’;

    返回结果为:2行(网络部的两个:王红和李强)

     

    外连接:外连接分为左连接和右连接,当返回的查询结果不仅需要包括符合条件的数据,还需要包含其中的一个表或者两个表的所有数据的时候,需要用到外连接查询

    语句:select 字段 from tbname1 left|right [outer] join tbname2;

    左连接:left join:返回包括左表中的所有记录和右表中符合条件的记录。

    例如:select department.dname,employee.name from department left join employee on department.did = employee.did;

    右连接:right join:返回包括右表的所有记录和左表符合条件的记录。

    例如:select department.dname,employee.name from department right join employee on department.did = employee.did;

    子查询:指一个查询语句嵌套在另一个查询语句内部的查询,在执行的时候会先执行子查询中的语句,然后将返回的结果作为外层查询的过滤条件。需要注意的是第一个条件的判断字段要包含在第二个查询语句的字段中,否则报错。

    IN/NOT IN语句:select 字段from tbname where 条件 in /not in (select 字段 where 条件)  

    EXISTS语句:EXISTS关键字后面的参数可以是任何一个子查询,但是不会产生任何数据,只返回TRUE或者FALSE,当返回TRUE的时候外层查询才会执行。

    语句:select 字段 from tbname where exists (select 字段 from tbname where 条件)

    ANY语句:ANY关键字表示只要满足内层子查询中的任意一个条件,就会返回一个结果作为外层查询条件。

    语句:select 字段from tbname where 字段 比较符 any(select字段 from tbnamewhere条件)

    ALL语句:类似于ANY只是他需要满足所有条件

    语句:select字段 from tbname where 字段 比较符 all(select 字段 from tbname where条件)

     

    努力成为一名GEEK!
  • 相关阅读:
    server.c:5170:31: error: ‘struct redisServer'
    SpringBoot配置文件笔记:yaml语法,yaml如何写、注入配置文件类(2种方式根据业务场景选择)、多环境切换、配置文件加载优先级、指定位置加载配置文件
    微服务(Microservices)—— Martin Flower
    利用原生子窗体解决悬浮窗口播放的问题及踩坑记录
    uniapp中nvue页面如何使用iconfont字体图标
    项目经验踩坑记录:跨平台业务影响时注意要考虑多个平台
    SQL基础知识笔记:概述(层状/网状/关系模型)、数据类型、操作数据库能力(DDL/DML/DQL)、关系模型(主键、联合主键、外键、外键约束-性能影响、一对一、一对多、多对多、索引、索引效率)、实用SQL语句、事务(四个特性、四种隔离级别)
    Java的Maven基础知识笔记:Maven是什么、maven目录、pom.xml唯一ID、maven解决依赖管理、maven中央仓库与镜像、构建流程(声明周期、阶段、目标)、使用插件、模块管理、mvnw指定版本、如何发布自己的开源库
    Java里的IO基础知识笔记:IO流、字节流/字符流、File对象读取、输入流/输出流(使用过后及时关闭、缓冲区)、Filter模式、ZIP操作、读取classpath资源的意义、序列化/反序列化、Reader/Writer、使用Files工具类及其局限性
    推荐了解一个用于JavaScript的快速SQL数据库
  • 原文地址:https://www.cnblogs.com/FanMLei/p/10501013.html
Copyright © 2011-2022 走看看