关于MySql

一、Mysql概述

1.MySql是一种关系型数据库

2.SQL:结构化的查询语句

3.SQL分类:

  • DDL:数据定义语言   eg : create,alter,drop...
  • DML:数据操纵语言   eg : update,insert,delete
  • DCL:数据控制语言   eg : grant,if..
  • DQL:数据查询语言   eg : select

二、数据库增删查改

1.创建数据库:create database  数据库 [character 字符集 collate 校对规则]

2.查看数据库:

  • 查看所有数据库:show databases;
  • 查看某个数据库:show create database 数据库名;

3.修改数据库:alter database 数据库名 character 字符集 collate 校对规则

4.删除数据库:drop database 数据库名;

5.切换数据库:use 数据库名;

6.查看当前使用数据库:select database();

三、数据库表操作

1.创建表:create table 表名 (

              字段名 类型(长度) 约束,

              字段名 类型(长度) 约束,

              字段名 类型(长度) 约束    );

2.查询当前数据库下有哪些数据表:show tables

3.like 模糊查询:  通配符:_可以代表任意的单个字符,%可以代表任意的字符   show tables like '模糊查询表名%';

4.查看表的创建语句 :show create table表名;

5.查看表的结构:desc 表名;

6.drop 删除数据表:drop table [if exists] 表名  eg : drop table if exists test;

7.alter 修改表名 : alter table 旧表名 rename to 新表名;

8.add 增加一列 : alter table 表名 add 新列名 字段类型 [字段选项];

四、数据操作

1.insert 插入数据(增)insert into 表名(字段列表) values(值列表);

2.select 查询数据(查) :select *[字段列表] from 表名[查询条件];  eg : select * from user;--查全部字段用*代替

3.delete 删除数据(删) :delete from 表名[删除条件];  eg : delete from user where age<1;--删除age小于1数据

4.update 修改数据(改) :update 表名 set 字段1=新值1,字段n=新值n [修改条件];  eg: update user set age=100 where name='admin_a';

注意事项:

  • 列名的个数与值的个数对应.
  • 列的类型与值的类型对应.位置也要对应.
  • 列的类型如果是字符串或者日期,写值的时候使用单引号将值引起来.
  • 插入的值的最大长度不能超过列的最大长度.

 五、数据类型

MySQL三大数据类型:数值型、字符串型和日期时间型

六、约束

单表约束:

  • 主键约束:primary key (默认就是唯一非空的)
  • 唯一约束:unique
  •  非空约束:not null

七、连接

1.交叉连接:select * from A,B;   --- 获得的是两个表的笛卡尔积.

2.内连接: inner join -- inner 可以省略

  • 显式内连接:select * from A inner join B on 条件;
  • 隐式内连接:select * from A,B where 条件;

3.外连接:outer join -- outer 可以省略

  • 左外连接:left outer join  -- select * from A left outer join B on 条件;
  • 右外连接:right outer join -- select * from A right outer join B on 条件;

4.多表查询的子查询:一个SQL语句查询的过程中需要依赖另一个查询语句.

 

 

posted @ 2020-04-14 23:08  是阿芸啊  阅读(166)  评论(0编辑  收藏  举报