mysql笔记(1)

数据库

MySQL

单机程序(自己DB)

单机程序(公用DB)

MySQL:是用于管理文件的一个软件
    - 服务端软件
        - socket服务端
        - 本地文件操作
        - 解析指令【SQL语句】
    - 客户端软件(各种各样)
        - socket客户端
        - 发送指令
        - 解析指令【SQL语句】

    PS:
        - DBMS数据库管理系统
        - SQL语句

技能:
    - 安装 服务端和客户端
    - 连接
    - 学习SQL语句规则;指示服务端做任意操作

其他类似软件:
关系型数据库:sqllite,db2,oracle,access,sql server MySQL
非关系型数据库:MongoDB,redis

  1. MySQL安装

Windows:
可执行文件

    压缩包
        放置任意目录
        初始化
        > http://blog.csdn.net/mchdba/article/details/53618276
    针对服务端
    `mysqld --initialize-insecure`
    查看data目录,.err文件中有初始化创建的账号密码信息
 
 
            服务端:E:wupeiqimysql-5.7.16-winx64mysql-5.7.16-winx64inmysqld --initialize-insecure
                   # 用户名 root 密码:空
            启动服务端:
                E:wupeiqimysql-5.7.16-winx64mysql-5.7.16-winx64inmysqldmysqld
                
            客户端连接:
                E:wupeiqimysql-5.7.16-winx64mysql-5.7.16-winx64inmysqldmysql -u root -p 
                
                发送指令:
                    show databases;
                    create database db1;
            环境变量的配置:
                E:wupeiqimysql-5.7.16-winx64mysql-5.7.16-winx64in
                mysqld
                
            windows服务:
                E:wupeiqimysql-5.7.16-winx64mysql-5.7.16-winx64inmysqld --install
                net start MySQL
                
                E:wupeiqimysql-5.7.16-winx64mysql-5.7.16-winx64inmysqld --remove
                
                net start MySQL
                net stop MySQL
  1. 关于连接

文件夹【数据库】

  • 文件【表】

    • 数据行【行】
    • 数据行
    • 数据行
  • 连接:
    默认:用户root

        show databases;
        //查看数据库表
        use 数据库名称;
        //使用数据库【文件夹】
        show tables;
        //查看文件夹中的【表格】
        select * from 表名;
        //获取表格的数据
        select name,age,id from 表名;
        //从表中获取某些特定的【字段】
        mysql数据库user表
        use mysql;
        select user,host from user;

通信交流

  • 创建用户:
    create user 'alex'@'192.168.1.1' identified by '123123';
    create user 'alex'@'192.168.1.%' identified by '123123';
    create user 'alex'@'%' identified by '123123';
  • 授权:
    权限 人
grant select,insert,update  on db1.t1 to 'alex'@'%';
grant all privileges  on db1.t1 to 'alex'@'%';
//百分号内填写对方的ip,这样就绑定了对方的用户名和登陆ip 
revoke all privileges on db1.t1 from 'alex'@'%';
//移除权限
DBA: 用户名密码

学习SQL语句规则

操作文件夹

create database db2;
create database db2 default charset utf8; *****
show databases;
drop database db2;

操作文件

show tables;
create table t1(id int,name char(10)) default charset=utf8;
create table t1(id int,name char(10))engine=innodb default charset=utf8;
create table t3(id int auto_increment,name char(10))engine=innodb default charset=utf8;  *****

create table t1(
列名 类型 null,
列名 类型 not null,
列名 类型 not null auto_increment primary key,
id int,
name char(10)
)engine=innodb default charset=utf8;
# innodb 支持事务,原子性操作
# myisam myisam

desc customers_info;查看某张表信息/结构

auto_increment 表示:自增

自增补充:
修改自增起始值:`alter table t1             auto_increment=20;`
自增步长-----
——>会话级别:
show session variables like "auto_inc%";
//查看会话级别变量
set session auto_increment_increment=2
//设置会话步长为2
set session auto_increment_offset=10;
//设置会话起始值_

-->全局级别:
show global variables like "auto_inc%";
//查看全局变量
set global auto_increment_invrement=2;
//设置全局步长
set global auto_increment_offset=10;

primary key: 表示 约束(不能重复且不能为空); 加速查找
not null: 是否为空

增删改查:
*

标记查看表

mysql> desc department
    -> ;
+-------+-------------+------+-----+---------+----------------+
| Field | Type        | Null | Key | Default | Extra          |
+-------+-------------+------+-----+---------+----------------+
| id    | int(11)     | NO   | PRI | NULL    | auto_increment |
| dname | varchar(32) | YES  |     | NULL    |                |
+-------+-------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

mysql> show create table stuff G
*************************** 1. row ***************************
       Table: stuff
Create Table: CREATE TABLE `stuff` (
  `sid` int(11) NOT NULL AUTO_INCREMENT,
  `sname` varchar(32) DEFAULT NULL,
  `did` int(11) DEFAULT NULL,
  PRIMARY KEY (`sid`),
  KEY `fk_stuff_did` (`did`),
  CONSTRAINT `fk_stuff_did` FOREIGN KEY (`did`) REFERENCES `department` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

数据类型:

  • 数字:
    整数
(范围不同):int tinyint bigint(unsigned/signed)

浮点数

float
0.00000100000123000123001230123
DOUBLE
0.00000000000000000000100000123000123001230123
0.00000100000123000000000000000
decimal(精确小数,可以指定精度)
0.1
  • 字符串:
    字符串类型:char varchar(可变长)
    最高只能有255个字符
    如果高于255,就用text类型
char(10)      速度快()

varchar(10)   节省空间

PS: 创建数据表定长列往前放

text类型

上传文件: 
文件存硬盘
db存路径
  • 时间类型
    DATETIME
  • 枚举类型
    enum
  • 集合类型
    set
create table t1(
id int signed not null auto_increment primary key,// 主键 自增
num decimal(10,5),
name char(10)
)engine=innodb default charset=utf8;

清空表:

delete from t1;
truncate table t1;

删除表:

drop table t1;

用法:DROP TABLE IF EXISTS "dt_class";
如果表中存在dt_class,就删除这个表,一般用在表头可以确保创建表的策略。

  • 操作文件中内容

插入数据:
insert into t1(id,name) values(1,'alex');
删除:
delete from t1 where id<6
修改:

update t1 set age=18;
update t1 set age=18 where age=17;

查看数据:
select * from t1;

主键和外键

  • 主键:
    一个表只能有一个主键,但是逐渐可以由多列组成

  • 外键:

create table userinfo(
    uid bigint auto_increment primary key,
    name varchar(32),
    department_id int,
    xx_id int,
    constraint fk_user_depar foreign key ("department_id",) references       department('id'),
    constraint fk_xx_ff foreign key (xx_id) references XX(id)
)engine=innodb default charset=utf8;
//注意,有些版本的mysql并不支持这种,括号内在加 引号的方式。推荐去掉引号和末尾的逗号。
create table department(
id bigint auto_increment primary key,
title char(15)
)engine=innodb default charset=utf8;

为已经添加好的数据表添加外键:
语法:
alter table 表名 add constraint FK_ID foreign key(你的外键字段名) REFERENCES 外表表名(对应的表的主键字段名);

作业:
http://images2015.cnblogs.com/blog/425762/201608/425762-20160803224643778-2071849037.png
http://www.cnblogs.com/wupeiqi/articles/5729934.html


作业:

mysql> create database db_school;
Query OK, 1 row affected (0.00 sec)
 
mysql> use db_school;
Database changed
 
 
mysql> create table t_teacher(id tinyint unsigned not null auto_increment,name varchar(60) not null,primary key(id))engine=innodb
    -> ;
Query OK, 0 rows affected (0.02 sec)
 
mysql> insert into t_teacher(name)value('scott'),('jerry'),('peter'),('jack');
Query OK, 4 rows affected (0.01 sec)
Records: 4  Duplicates: 0  Warnings: 0
 
mysql> select * from t_teacher
    -> ;
+----+-------+
| id | name  |
+----+-------+
|  1 | scott |
|  2 | jerry |
|  3 | peter |
|  4 | jack  |
+----+-------+
4 rows in set (0.00 sec)
 
 
mysql>  create table t_course(cid tinyint unsigned not null auto_increment,cname varchar(30) not null,primary key(cid))engine=innodb
    -> ;
Query OK, 0 rows affected (0.02 sec)
 
 
mysql> alter table t_course add column tid tinyint not null;
Query OK, 0 rows affected (0.04 sec)
 
mysql> insert into t_course(cname,tid)value('oppo','3'),('vivo','2'),('sansang','3');
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0
 
mysql> select * from t_course;
+-----+---------+-----+
| cid | cname   | tid |
+-----+---------+-----+
|   1 | oppo    |   3 |
|   2 | vivo    |   2 |
|   3 | sansang |   3 |
+-----+---------+-----+
3 rows in set (0.00 sec)
 
mysql> create table t_class(
    -> id tinyint unsigned not null auto_increment primary key,
    -> class char(12) not null)engine=innodb charset=utf8;
Query OK, 0 rows affected (0.01 sec)
 
mysql> insert into t_class(class)values('三年二班'),('三年三班'),('三年一班');
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0
 
mysql> select * from t_class;
+----+--------------+
| id | class        |
+----+--------------+
|  1 | 三年二班     |
|  2 | 三年三班     |
|  3 | 三年一班     |
+----+--------------+
3 rows in set (0.00 sec)

原文地址:https://www.cnblogs.com/scott-lv/p/7587060.html