一、数据库的使用Mysql

2018-10-13  本文已影响0人  WenErone

一、数据库概念
- 数据库
- 数据库管理系统

二、数据库
- 安装

- 服务
    sudo systemctl start/stop/restart/status/enable/disable mysql.service 
    sudo service mysql start/stop/restart/status
    
- 数据库连接
    mysql -u用户名 -p密码
    mysql -u用户名 -p
    
    
    > 默认用户root
    > 数据库退出 quit / exit 
    
- 远程服务器的连接
    IP地址: 10.36.133.24
    数据库用户名: root
    数据库密码: 123456
    数据库默认端口号: 3306
    
    // 选择数据库
    use mysql;
    
    // 添加一个远程用户,用户名为root,密码123456
    GRANT ALL PRIVILEGES ON *.* TO 'root'@'%' IDENTIFIED BY '123456' WITH GRANT OPTION;    


    > 默认数据库只能是本地使用,没有远程用户即不能远程连接!
    > localhost 和 127.0.0.1 本地(本机)

三、SQL
- 数据库、表单关系
python班级学生信息
新建Execl文件(students.xls) -> 打开students.xls文件 -> 新建工作表(表单)python1807 -> 设置表结构 -> 录入数据 【一个文件多个表】

        数据库 <==>  Excel文件

- 分类
    DDL 数据库定义语言(表单操作)
    DML 数据操作语言(数据的插入、删除、更新)
    DQL 数据库查询语言(数据的查询)
    DCL 数据库控制语言(安全和访问限制)

四、数据库 DDL
- 显示所有数据库
show databases;
- 查看当前使用数据库
select database();

- 创建数据库
    语法:
        create database 数据库名;
    例如:
        create database pythonstu;
        create database pythonstu charset=utf8;

- 删除数据库
    语法:
        drop database 数据库名;
    例如:
        drop database pythonstu;
        

- 选择数据库
    语法:
        use 数据库名;
    例如:
        use pythonstu;
        
        
        
        
- 显示当前数据库中的表单
    show tables;


- 创建表
    语法:
        create table 表名(字段1 属性1, 字段2 属性2, 字段3 属性3...);
    例如:
        create table python1807(id int, name char(50), sex char(10));
        
- 显示表结构
    语法:
        desc 表名;
    例如:
        desc python1807;
        
    | Field | Type    | Null | Key | Default | Extra |
    Field 字段
    Type 数据类型
    Null 是否为空
    key 键(主键、外键)
    default 默认值
    extra 额外属性
    

- 删除表
    语法:
        drop table 表名;
    例如:
        drop table python1807;

- 修改表单 --- 修改表名
    语法:
        alter table 表名 rename 新表名;
    例如:
        alter table python1808 rename python1809;
    
- 修改表单 --- 添加字段
    语法:
        alter table 表名 add 字段 属性;
    例如:
        alter table python1807 add score int;
        
- 修改表单 --- 删除字段
    语法:
        alter table 表名 drop 字段;
    例如:
        alter table python1807 drop score;
        
- 修改表单 --- 修改属性
    语法:
        // 修改字段名
        alter table 表名 change 旧字段名 新字段名 旧的属性;
        // 修改属性
        alter table 表名 change 字段 字段 新属性;
    例如:
        alter table python1807 change id id int not null;
        alter table python1807 change sex p_sex char(10);
        
- 修改表单 ---- 添加主键
    主键,不能为空且唯一
    语法: 
        alter table 表名 add primary key(字段);
        alter table 表名 change 字段 字段 属性 primary key;
    例如:
        alter table python1807 add primary key(id);
        alter table python1807 change id id int(4) primary key;

- 修改表单 ---- 删除主键
    语法:
        alter table 表名 drop primary key;
    例如:
        alter table python1807 drop primary key;
        
    > 删除主键之前,必须先清除自增长属性!
        
- 修改表单 ---- 添加自增长属性
    自增长属性,前提该字段是主键。
    语法:
        alter table 表名 change 字段 字段 属性 auto_increment;
    例如:
        alter table python1807 change id id int auto_increment;

- 修改表单 ---- 删除自增长属性
    语法:
        alter table 表名 change 字段 字段 属性;
    例如:
        alter table python1807 change id id int;

五、数据库之 DML
- 插入数据
语法:
// 值和(表单)字段顺序要一一对应
insert into 表名 values(值1,值2...)
// 字段和值前后一一对应
insert into 表名(字段1,字段2...) values(值1,值..)
例如:
insert into python1807 values(1001, '张三', '男');
insert into python1807(name,sex) values('王五', '男');
insert into python1807(name) values('赵柳');

- 删除数据
    语法: 
        delete from 表名 where 条件;
    例如:
    
    
    
    
    
        // 没有条件,删除整个表
        delete from python1807;
        // 有条件,根据条件删除
        delete from python1807 where id=1004;

- 更新数据
    语法:
        update 表名 set 字段1=值1,字段2=值2... where 条件;
    例如:
        // 没有条件。更新整个表
        update python1807 set p_sex='男';
        // 有条件,根据条件更新
        update python1807 set name='测试',p_sex='0' where id=1003;

六、数据库之 DQL
- 基础查询
语法:
select * from 表名 where 条件;
例如:
// 查询所有显示数据
select * from python1807;
// 查询符合条件数据
select * from python1807 where id=1002;
// 指定显示字段
select name,p_sex from python1807;

- 条件查询
    1. 逻辑运算符 < <= > >=  =等于 !=不等于
        // 查询学号为1010的学生信息
        select * from students_info where id=1010;
        // 语文成绩小于80的学生信息
        select * from students_info where chinese<80;
        
    2. 算术运算符 + - * / %
        // 总分大于250的学生信息
        select * from students_info where (math+english+chinese)>250;
        select *,(math+english+chinese) from students_info where (math+english+chinese)>250;
        
        select name,class,math,english,chinese from students_info;
        select name,class,(math+english+chinese) from students_info;
        
    3. 模糊查询 like
        % 任意个字符
        _ 匹配任意字符
        // 查找王同学的学生信息
        select * from students_info where name like "王%";
        // 查找王某的学生信息
        select * from students_info where name like  "王_";
        // 查找王某某的学生信息
        select * from students_info where name like  "王__";
        // 以王结尾的学生信息
        select * from students_info where name like  "%王";
        // 带王字的学生信息
        select * from students_info where name like  "%王%";
    
    4. 并列 and
        // 语文,数学都大于90的学生信息
        # 条件: chinese>90      math>90
        select * from students_info where chinese>90 and math>90;
        // 广东男同学,语文成绩大于90的学生信息
        # 条件: address like "广东%"            sex='男'             chinese>90
        select * from students_info where address like "广东%" and sex='男' and chinese>90;
        
    5. 或者 or 
        // 语文、数学、英语只要一科小于60的学生信息
        # 条件: chinese<60        math<60       english<60
        select * from students_info where chinese<60 or math<60 or english<60;
        
    6. 在...之间 between x and y 
        // 语文成绩70~80之间的学生信息
        # 条件:  chinese>70       chinese<80
        select * from students_info where chinese>70 and chinese<80;
        select * from students_info where chinese between 70 and 80;
        
    7. 在..里面 in 
        // 班级为1707、1708、1709的学生信息
        # 条件 class=1707     class=1708          class=1709
        select * from students_info where class=1707 or class=1708 or class=1709;
        select * from students_info where class in(1707,1708,1709);

注意: MySQL不区分大小写,关键字会大写!
注意: 系统的数据库不要随意删减或其他操作!!!
注意: SQL语句以 ';' 分号结尾!
注意: 中英符号,只能用英文符号! 【报错检查】
注意: 检查语法是否正确!!! 【报错检查】

char和varchar
char(255) 固定255
varchar(255) 最大255

char 固定长度,效率高,用空间换时间;
varchar 可变长度,可以节省空间,影响到效率;
上一篇 下一篇

猜你喜欢

热点阅读