MYSQL个人笔记

MySQL基本内容

常用的语句

  select  从数据库中提取数据
  update  更新数据库中的数据          (update table set "index=value" where index=value)
  delete  从数据库中删除数据           (delete from table where index=value)
  insert into  向数据库中插入新数据
  create database  创建新的数据库
  alter database  修改数据库
  create table  创建新表
  alter table  更新/改变数据表内容
  drop table  删除数据表
  create index  创建索引/搜索键
  drop index  删除索引
  distinct index  去除index中的重复值      (select distinct index from table)
  where  在查询中进行定位,可与and和or配合使用
  order by index 对查询结果进行排序 (order by index desc 降序)
  limit number  限制提取数据的条数

案例

  //使用数据库
  mysql> use practice;
  Database changed
  ​
  //创建数据表语句
  mysql> create table if not exists website(
      -> id int not null auto_increment,  
      -> name char(10) not null,
      -> url char(40) not null,
      -> alexa int not null,
      -> country enum("CN","USA"),
      -> primary key(`id`)
      -> )engine=innodb default charset=utf8mb4;
  Query OK, 0 rows affected (0.05 sec)
  ​
  //查看建表语句
  mysql> show create table website;
  +---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  | Table   | Create Table                                                                                                                                                                                                                                                                   |
  +---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  | website | CREATE TABLE `website` (
    `id` int NOT NULL AUTO_INCREMENT,
    `name` char(10) NOT NULL,
    `url` char(40) NOT NULL,
    `alexa` int NOT NULL,
    `country` enum('CN','USA') DEFAULT NULL,
    PRIMARY KEY (`id`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci |
  +---------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
  1 row in set (0.00 sec)
  ​
  //查询表中是数据
  mysql> select * from website;
  +----+--------------+---------------------------+-------+---------+
  | id | name         | url                       | alexa | country |
  +----+--------------+---------------------------+-------+---------+
  |  1 | Google       | https://www.google.cm/    |     1 | USA     |
  |  2 | 淘宝         | https://www.taobao.com/   |    13 | CN      |
  |  3 | 菜鸟教程     | http://www.runoob.com/    |  4689 | CN      |
  |  4 | 微博         | http://weibo.com/         |    20 | CN      |
  |  5 | Facebook     | https://www.facebook.com/ |     3 | USA     |
  +----+--------------+---------------------------+-------+---------+
  5 rows in set (0.00 sec)
  ​
  //distinct 删除重复值
  mysql> select distinct country from website;
  +---------+
  | country |
  +---------+
  | USA     |
  | CN      |
  +---------+
  2 rows in set (0.00 sec)
  ​
  //

like和regexp高级语法

  
  %  代替0个或多个字符
  _  代替一个字符
  [charlist]  只要含有一个便符合
  ^[charlist] 和 [!charlist]   一个都没有
  REGEXP  和上面的charlist一起使用

IN高级语法

  
  in (列表)  表示在列表中能够找到对应的值
  select * from table where index in (list)

between高级语法

  
  index 的值介于value两个值之间
  有两个方式分别是 not between 和 between
  select * from table where index between value and value

as别名

  
  在输出数据时index会被new_index代替
  select index as new_index from table
  ​

join连接

  
  用于连接两个或多个表中的数据
  mysql> select website.id,website.name,website.url,website.country,man.name as '创始人' from website inner join man on website.id=man.id;
  +----+--------------+---------------------------+---------+---------------------------------+
  | id | name         | url                       | country | 创始人                          |
  +----+--------------+---------------------------+---------+---------------------------------+
  |  1 | Google       | https://www.google.cm/    | USA     | 谢尔盖·布林                     |
  |  2 | 淘宝         | https://www.taobao.com/   | CN      | 马云                            |
  |  3 | 菜鸟教程     | http://www.runoob.com/    | CN      | 孙江涛                          |
  |  4 | 微博         | http://weibo.com/         | CN      | 曹国伟                          |
  |  5 | Facebook     | https://www.facebook.com/ | USA     | 马克·艾略特·扎克伯格            |
  +----+--------------+---------------------------+---------+---------------------------------+
  5 rows in set (0.00 sec) 
  ​
  left join语法:关键字从table1返回所有的行,即使table2中没有匹配。如果右表中没有匹配,则结果为 NULL。
  select 字段 from table1 left join table2 on 字段=字段;
  ​
  right join语法:关键字从table2返回所有的行,即使table1中没有匹配。如果左表中没有匹配,则结果为 NULL。
  select 字段 from table1 right join table2 on 字段=字段;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值