数据库1

数据库基础

  • DB:数据库,存储数据的仓库,保存一系列有组织的数据
  • DBMS:数据库管理系统,数据库是通过DBMS创建和操作的容器
  • SQL:结构化查询语言,专门用来和数据库通信的语言

DBMS分为两类:

  1. 基于共享系统的DBMS
  2. 基于客户端-服务器端

数据库基本操作原理

  1. 先将数据放在表中,再把表放在数据库中
  2. 一个数据库可以有多个表,每个表都有一个名字唯一标识
  3. 每个表由列或者多个多个列组成,称为字段

MySQL基本操作

  1. 登录数据库服务器mysql -u root -p1234qwer
  2. 显示数据库中的所有数据show databases
  3. 选中某一个数据库进行操作use test
  4. 查询表中的数据select * from pet;
  5. 退出数据库服务器exit;
  6. 创建数据库create database test;
  7. 创建一个表

    1
    2
    3
    4
    5
    6
    7
    mysql> CREATE TABLE pet (
    -> name VARCHAR(20),
    -> owner VARCHAR(20),
    -> species VARCHAR(20),
    -> sex CHAR(1),
    -> birth DATE,
    -> death DATE);
  8. 查看表show tables;

  9. 查看表的详情describe pet;
  10. 添加数据INSERT INTO pet VALUES('mimi','lulu','hamster','f','2017-08-01',NULL);
  11. 删除数据:delete from pet where species='hamster';'
  12. 更新数据:update pet set owner='lhy' where name='doudou';
  13. MySQL常用数据类型:
  • 数值
  • 日期
  • 字符串
  1. 数据库常见操作:
  • 增加 INSERT
  • 删除 DELETE
  • 修改 UPDATE
  • 查询 SELECT

MySQL建表约束

  1. 主键约束
  • 唯一确定一张表的中的唯一约束,通过给某个字段增加约束,就可以使得字段不重复且不为空
  • id添加主键约束,主键可以为空

    1
    2
    3
    4
    create table user(
    id int primary key,
    name varchar(20)
    );
  • id和name一起添加主键约束,称为联合主键,只要加起来不重复即可,但是不可为空

    1
    2
    3
    4
    5
    6
    create table user(
    id int,
    name varchar(20),
    password varchar(20),
    primary key(id, name)
    );
  • 后期增加主键约束alter table user4 add primary key(id);或者alter table user4 modify id int primary key;

  • 删除主键约束alter table user4 drop primary key;
  1. 自增约束,自动生成主键值

    1
    2
    3
    4
    5
    6
    create table user3(
    id int primary key auto_increment,
    name varchar(20)
    );

    insert into user3 (name) values('zhangsan');
  2. 唯一约束:约束修饰该字段的值不能重复

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    create table user5(
    id int,
    name varchar(20)
    );

    alter table user5 add unique(name);

    create table user5(
    id int,
    name varchar(20),
    unique(name)
    );

    create table user5(
    id int,
    name varchar(20) unique
    );

    //删除唯一性约束
    alter table user5 drop index name;

    //后期添加
    alter table user5 modify name varchar(20) unique;
  3. 外键约束

  • 涉及到两个表,父表子表,主表副表
  • 在主表中记录被副表引用,则不可以删除
  • 主表中没有的数据值,不可以在副表中使用
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    create table class(
    id int primary key,
    name varchar(20)
    );

    insert into class values(1, 'class1');
    insert into class values(2, 'class2');
    insert into class values(3, 'class3');
    insert into class values(4, 'class4');

    create table student(
    id int primary key,
    name varchar(20),
    class_id int,
    foreign key(class_id) references class(id)
    );

    insert into student values(1001, 'zhangsan', 1);
    insert into student values(1002, 'zhangsan', 2);
    insert into student values(1003, 'zhangsan', 3);
    insert into student values(1004, 'zhangsan', 4);
    insert into student values(1005, 'lisi', 1);
    insert into student values(1006, 'lisi', 2);
  1. 非空约束:修饰字段不能为空

    1
    2
    3
    4
    5
    6
    create table user6(
    id int,
    name varchar(20) not null
    );

    insert into user6 (name) values('zhangsan');
  2. 默认约束:插入字段时没有传值就会使用默认值

    1
    2
    3
    4
    5
    6
    7
    create table user7(
    id int,
    name varchar(20),
    age int default 10
    );

    insert into user7 (name) values('zhangsan');
WhitneyLu wechat
Contact me by scanning my public WeChat QR code
0%