• 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!
  • 相关阅读:
    Ubuntu速配指南之热门设置
    最高境地的Linux桌面
    菜鸟在Linux零碎中安置Oracle 11G
    Ubuntu 7.10疾速设置指南
    excel的单元格怎么实现下拉菜单?
    Delphi 与 DirectX 之 DelphiX(16): DXImageList1.Items.Find();
    Delphi 与 DirectX 之 DelphiX(19): 绘图表面(TDirectDrawSurface)如何加载图片
    Delphi 与 DirectX 之 DelphiX(18): TDXDraw 中描绘图片的命令
    Delphi 与 DirectX 之 DelphiX(17): TPictureCollectionItem.PatternWidth、PatternHeight
    Delphi 与 DirectX 之 DelphiX(23): TDirectDrawSurface.Blur;
  • 原文地址:https://www.cnblogs.com/FanMLei/p/10501013.html
Copyright © 2020-2023  润新知