8.3 mysql 表操作

库操作

一 系统数据库

    information_schema: 虚拟库,不占用磁盘空间,存储的是数据库启动后的一些参数,如用户表信息、列信息、权限信息、字符信息等
    performance_schema: MySQL 5.5开始新增一个数据库:主要用于收集数据库服务器性能参数,记录处理查询请求时发生的各种事件、锁等现象
    mysql: 授权库,主要存储系统用户的权限信息
    test: MySQL数据库系统自动创建的测试数据库

1 语法(help create database)
CREATE DATABASE 数据库名 charset utf8;

2 数据库命名规则:
可以由字母、数字、下划线、@、#、$
区分大小写
唯一性
不能使用关键字如 create select
不能单独使用数字
最长128位
二 创建数据库
查看数据库
show databases;
show create database db1;
select database();
选择数据库
USE 数据库名
删除数据库
DROP DATABASE 数据库名;
修改数据库
alter database db1 charset utf8;
三 数据库相关操作

表操作

02 初识SQL语句

SQL语句:

操作文件夹(库)
    增
        create database db1 charset utf8;
    查
        show create database db1;
        show databases;
    改
        alter database db1 charset gbk;
    删
        drop database db1;

操作文件(表)
    切换文件夹:use db1;
    查看当前所在文件夹:select database();

    增
        create table t1(id int,name char);
    查
        show create table t1;
        show tables;
        desc t1;
    改
        alter table t1 modify name char(6);
        alter table t1 change name NAME char(7);
    删
        drop table t1;

操作文件内容(记录)
    增
        insert t1(id,name) values(1,'egon1'),(2,'egon2'),(3,'egon3');
    查
        select id,name from db1.t1;
        select * from db1.t1;
    改
        update db1.t1 set name='SB';   # 改所有的name
        update db1.t1 set name='ALEX' where id=2;
    删
        delete from t1;
        delete from t1 where id=2;
view

03 存储引擎介绍

1、什么是存储引擎?
    存储引擎就是表的类型

2、查看MySQL支持的存储引擎
    show engines;

3、指定表类型/存储引擎
    create table t1(id int)engine=innodb;
    create table t2(id int)engine=memory;
    create table t3(id int)engine=blackhole;
    create table t4(id int)engine=myisam;

    insert into t1 values(1);
    insert into t2 values(1);
    insert into t3 values(1);
    insert into t4 values(1);
view

04 日期类型

create table student(
    id int,
    name char(6),
    born_year year,
    birth_date date,
    class_time time,
    reg_time datetime
);

insert into student values
(1,'egon',now(),now(),now(),now());

insert into student values
(2,'alex',"1997","1997-12-12","12:12:12","2017-12-12 12:12:12");
view

05 字符类型

char:定长            # 会去掉最右边空格,左边的不会去掉
varchar:变长        # 不会去掉右边的空格,右边超过长度的会去掉,

char 定长,char(5)    即使不够5个,也按5个去存,
          varchar(5)  变长,按实际去存更节省

#宽度指的是字符的个数
create table t4(name char(5));
create table t5(name varchar(5));

insert into t4 values('你好 '); #'你好   '
insert into t5 values('你好 '); #'你好 '

select char_length(name) from t4; #5
SELECT char_length(name) FROM t4;        # 空格给我都去了
+---------------------+
|   char_length(name) |
|---------------------|
|                   2 |
+---------------------+

select char_length(name) from t5; #3
SELECT char_length(name) FROM t5;
+---------------------+
|   char_length(name) |
|---------------------|
|                   3 |
+---------------------+

select name from t4 where name='你好';
select name from t4 where name like '你好';
+--------+
| name   |
|--------|
| 你好   |
+--------+

select name from t5 where name='你好';
+--------+
| name   |
|--------|
| 你好   |
+--------+
1 row in set
Time: 0.002s
select name from t5 where name like '你好';
+--------+
| name   |
|--------|
+--------+


INSERT INTO t4 VALUES('  你好      ');
Query OK, 1 row affected
Time: 0.073s
SELECT char_length(name) FROM t4;
+---------------------+
|   char_length(name) |
|---------------------|
|                   2 |
|                   4 |
+---------------------+

INSERT INTO t5 VALUES('  你好      ');
Query OK, 1 row affected
Time: 0.041s
SELECT char_length(name) FROM t5;
+---------------------+
|   char_length(name) |
|---------------------|
|                   3 |
|                   5 |
+---------------------+


name char(5)
egon |alex |wxx  |

name varchar(5)
1bytes+egon|1bytes+alex|1bytes+wxx|
4+egon|4+alex|3+wxx|
view

06 枚举类型与集合类型

create table consumer(
    id int,
    name char(16),
    sex enum('male','female','other'),
    level enum('vip1','vip2','vip3'),
    hobbies set('play','music','read','run')
);

insert into consumer values
(1,'egon','male','vip2','music,read');

insert into consumer values
(1,'egon','xxxxx','vip2','music,read');
view

 

约束条件

07 约束条件not null与default

create table t15(
    id int(11) unsigned zerofill
);

create table t16(
    id int,
    name char(6),
    sex enum('male','female') not null default 'male'
);

insert into t16(id,name) values(1,'egon');
view

08 约束条件unique key

unique key

单列唯一
    #方式一
    create table department(
        id int unique,
        name char(10) unique
    );
    #方式二:
    create table department(
        id int,
        name char(10),
        unique(id),
        unique(name)
    );

    insert into department values
    (1,'IT'),
    (2,'Sale');


联合唯一
create table services(
    id int,
    ip char(15),
    port int,
    unique(id),
    unique(ip,port)            # ip 和 port 合起来唯一
);


insert into services values
(1,'192.168.11.10',80),
(2,'192.168.11.10',81),
(3,'192.168.11.13',80);


insert into services values
(4,'192.168.11.10',80);
view

09 约束条件primary key

primary key
约束:not null unique
存储引擎(innodb):对于innodb存储引擎来说,一张表内必须有一个主键

# 单列主键
create table t17(
    id int primary key,
    name char(16)
);


insert into t17 values
(1,'egon'),
(2,'alex');

insert into t17 values
(2,'wxx');

insert into t17(name) values
('wxx');

create table t18(
    id int not null unique,
    name char(16)
);



# 复合主键
create table t19(
    ip char(15),
    port int,
    primary key(ip,port)
);


insert into t19 values
('1.1.1.1',80),
('1.1.1.1',81);
view

10 约束条件 auto increment

auto_increment

create table t20(
    id int primary key auto_increment,
    name char(16)
);

insert into t20(name) values
('egon'),
('alex'),
('wxx');



insert into t20(id,name) values
(7,'yuanhao');

insert into t20(name) values
('egon1'),
('egon2'),
('egon3');


#了解
    show variables like 'auto_inc%';

    #步长:
    auto_increment_increment默认为1
    #起始偏移量
    auto_increment_offset默认1

    #设置步长
    set session auto_increment_increment=5;
    set global auto_increment_increment=5;

    #设置起始偏移量
    set global auto_increment_offset=3;
    # 强调:起始偏移量<=步长



    create table t21(
        id int primary key auto_increment,
        name char(16)
    );

    insert into t21(name) values
    ('egon'),
    ('alex'),
    ('wxx'),
    ('yxx');

    mysql root@localhost:lala> SELECT * FROM t21;
    +------+--------+
    |   id | name   |
    |------+--------|
    |    1 | egon   |
    |    6 | alex   |
    |   11 | wxx    |
    |   16 | yxx    |
    +------+--------+


清空表:
    delete from t20;
    delete from t20 where id = 3;
    insert into t20(name) values
    ('xxx');

    truncate t20; #应该用它来清空表



view

11 约束条件之foreign key

foreign key:建立表之间的关系

#1、建立表关系:
    #先建被关联的表,并且保证被关联的字段唯一
    create table dep(
        id int primary key,
        name char(16),
        comment char(50)
    );


    #再建立关联的表
    create table emp(
        id int primary key,
        name char(10),
        sex enum('male','female'),
        dep_id int,
        foreign key(dep_id) references dep(id)
        on delete cascade
        on update cascade
    );

#2、插入数据
#先往被关联表插入记录
insert into dep values
(1,"IT","技术能力有限部门"),
(2,"销售","销售能力不足部门"),
(3,"财务","花钱特别多部门");

#再往关联表插入记录
insert into emp values
(1,'egon','male',1);

insert into emp values
(2,'alex','male',1),
(3,'wupeiqi','female',2),
(4,'yuanhao','male',3),
(5,'jinximn','male',2);

delete from emp where dep_id=1;
delete from dep where id=1;                    # emp 中dep_id 为1 被删

delete from dep where id=3;
view

12 表之间的关系

两张表之间的关系:
    1、多对一
        出版社    书(foreign key(press_id) references press(id))

    2、多对多    # 第三张表存对应关系
        作者       书
        egon:
            九阳神功
            九阴真经
        alex:
            九阳神功
            葵花宝典
        yuanhao:
            独孤九剑
            降龙十巴掌
            葵花宝典
        wpq:
            九阳神功

        insert into author2book(author_id,book_id) values
        (1,1),
        (1,2),
        (2,1),
        (2,6);


    3、一对一
        customer表       student表
view

 

后续,----》数据操作

posted @ 2018-11-29 21:39  前海渔文乐  阅读(180)  评论(0编辑  收藏  举报