MySQL语句

MySQL语句


MySQL本地安装

  • 进入官网,点击下载(使用dmg安装包)。
    然后傻瓜式安装即可。
    这里写图片描述

  • 进入系统偏好设置,点击MySQL
    这里写图片描述

点击start MySQL server
这里写图片描述

  • 修改环境变量
vim ~/.bash_profile
\\输入
PATH=$PATH:/usr/local/mysql/bin
\\结束
source ~\.bash_profile   
  • 进入MySQL
mysql -uroot -p \\输入初始密码(安装时弹出的窗口)
SET PASSWORD FOR 'root'@'localhost' = PASSWORD('新密码'); \\修改密码

如果忘记了,参考http://aiaileen.com/2018/01/18/Change-MySQL-password-for-root/

  • 创建索引
create unique index index_name on table_name(column)  //唯一索引
create index index_name on table_name(column)        //普通索引
  • 创建视图
create Siew name as SelectStatement  //创建视图不会返回任何数据
  • 向表里面增加新列
ALTER TABLE database_name.table_name RENAME TO new_table_name;
  • 启动与停止
启动MySQL服务
sudo /usr/local/MySQL/support-files/mysql.server start

停止MySQL服务
sudo /usr/local/mysql/support-files/mysql.server stop

重启MySQL服务
sudo /usr/local/mysql/support-files/mysql.server restart

MySQL语句

  1. MySQL支持:数值型字段、日期/时间型字段、字符串型字段。
  2. 创建MySQL表:

    CREATE TABLE table_name (column_name column_type)
    //例子 1.
    create table tb(
        `id` samllint unsigned auto_increment primary key,//auto_increment表示自增,primary key表示主键
        `username` varchar(20) not null;不允许为空
    )
    //例子 2.
    CREATE TABLE IF NOT EXISTS 'runoob_tbl'(
       `runoob_id` INT UNSIGNED AUTO_INCREMENT,
       `runoob_title` VARCHAR(100) NOT NULL,
       `runoob_author` VARCHAR(40) NOT NULL,
       `submission_date` DATE,
       PRIMARY KEY ( `runoob_id` )
    )ENGINE=InnoDB DEFAULT CHARSET=utf8 //ENGINE 设置存储引擎,CHARSET 设置编码
  3. 删除数据表

    DROP TABLE table_name  \\删除数据表
    delete from table_name where conditon \\删除表内数据
    truncate table  table_name \\清除表内数据,保存表结构
    
  4. 插入数据

    insert into  
    table_name ( col1, col2,...,colN )
    values( value1, value2,...,valueN ),
          ( value1, value2,...,valueN ),\\插入多行,列不必与表中顺序一致,但是值必须与列的顺序一致。
                                        \\values还可以用select子句代替
    
    \\例子
    insert into tasks(subject,start_date,end_date,description)
    values('Learn MySQL INSERT','2017-07-21','2017-07-22','Start learning..');
    
  5. 更新数据
update table
set Column1 = Exp1
Column2 = Exp2
where condition   //where必须,如果不指定,就会修改每一行。
\\例子
update Clients
set LastName = "joe"
State = "Hefei"
where ClientId = 1
\\根据子查询修改(根据表a的情况修改表b)
update Vendors
set Vendors.State = 
(select VendorsChange.State 
from VendorsChange 
where VendorsChange.VendorsID = Vendors.VendorsID)

where exists                         //EXISTS用于检查子查询是否至少会返回一行数据,该子查询实际上并不返回任何数据,而是返回值TrueFalseEXISTS 指定一个子查询,检测 行 的存在
(select *
from VendorsChange
where VendorsChange.VendorsID = Vendors.VendorsID)
  • 触发器:SQL触发器是存储在数据库目录中的一组SQL语句。每当与表相关联的事件发生时,即会执行或触发SQL触发器,例如插入,更新或删除。SQL触发器是一种特殊类型的存储过程。 这是特别的,因为它不像直接像存储过程那样调用。 触发器和存储过程之间的主要区别在于,当对表执行数据修改事件时,会自动调用触发器,而存储过程必须要明确地调用。
CREATE TRIGGER [trigger_name trigger_time trigger_event] //触发器名字
 ON table_name   //作用在的表
 before || after  update|insert|delete      //触发激活时间可以在之前或之后。必须指定定义触发器的激活时间
 BEGIN
 ...
 END;

//例子
create trigger audit_log
after insert
on employees_test 
begin 
insert into audit
values(new.ID,new.NAME);
end

NEW与OLD 关键字访问触发后或触发前的employees_test表单记录

  • 把object对象中出现的的search全部替换成replace。
update hellotable set 'helloCol' = replace('helloCol','helloSearch','helloReplace')

SQL查询

一般格式

\\最一般的单表查询
select columnlist as name               //"as"关键字可以用来指定列的别名,也可以指定表的;星号(*)表示要返回所有列
from tablelist                          //from指定要查询数据的表或视图
[inner | left |right] join table_2 
on conditions                           //根据某些连接条件从其他表中获取数据
where condition                         //过滤结果集中的行
group by columnlist                     //按照某个key值对数据表分组
having condition                        //基于GROUP BY子句定义的小分组取的条件
order by columnlist (desc)              //升序(降序)
limit offset, length                    //offset表示输出记录的初始位置,length表示输出几条记录
\\例子
select student as s,gradetype as type,avg(grade) as average
from grades
where type = 'quiz'
group by s,type
having average > 70
order by s 
  • where子句还可以加入复杂的逻辑
>,<,=,!=,>=,<=,and,or
//and 表示同时成立,or表示只要一个成立,一般用来表示复杂逻辑
select price,user_id
from Price_table
where price > 100
and (user_id = 1001
or user_id = 1002)   //圆括号指定计算顺序
  • Not、Between、In运算符
    • Not表示对后面的逻辑取反
    • Between表示在两个数之间
    • In表示满足在某个指定集合里
    • is NULL检查该值是否为NULL
select  id,lastname, firstname, officeCode
from employees
where officecode <= 4
and officecode >=1
and id is not NULL //可以用notis nullqu fan
//等价于
select  lastname, firstname, officeCode
from employees
where officecode bewteen 1 and 4
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值