首页 > 数据库 >数据库基础操作 part1

数据库基础操作 part1

时间:2022-09-05 21:59:11浏览次数:42  
标签:-- name int 数据库 id part1 操作 table create

初识数据库

数据库相关概念

数据库管理软件: 本质就是一个C/S架构的套接字程序
    服务端套接字              客户端套接字
    操作系统: Linux           操作系统: 随意
    计算机(本地文件)           计算机硬件

    应用流程: 服务端发送请求语句给服务端. 服务端从本地文件中读取数据返回给客户端

    例如:
    关系型数据库管理软件: 优点是表之间有关系
        mysql Oracle
    非关系型数据库管理软件: 一般都是以key: value格式储存数据. 所以查询较快. 储存在内存中
        redis memcache mongodb

    sql语句: 套接字管理软件的作者为使用者规定的命名规范

数据库核心概念:
    数据: 事物的状态
    记录: 文件中的一条信息
    表: 一个文件
    库: 文件夹
    数据库管理软件: 套接字程序: 服务端-mysqld 客户端-mysql
    数据库服务器: 运行mysqld的计算器

初识sql语句

1. 库 -> 文件夹
    增
        create database day01 charset utf8mb4;
    改
        alter database  day01 charset gbk;
    查
        show databases;
        show create database day01;
    删
        drop database day01;
    creat database day01;

2. 表 -> 文件
    use day01;
    select database();

    增
        create table t1(id int, name varchar(16));
    改
        alter table t1 rename t2;
        alter table t1 modify name varchar(10);
    查
        show tables;
        desc t2;
    删
        drop table t2;

3. 记录 -> 文件中的每一行内容
    create database day02;
    use day02;
    create table t1(id int, name varchar(16));

    增
        insert t1 values
        (1, "Maxs_hu"),
        (2, "Mokeke"),
        (3, "xiaoergu")
    改
        update t1 set name = "lili" where id = 2;
    查
        select * from t1;
        select name from t1;
        select name from t1 where id >= 2;
    删
        delete from t1 where id = 2;


了解知识点. sql语句分三种:
    1. DDL语句 数据库定义语言: 数据库. 表. 视图. 索引. 储存过程. 例如: create  drop alter
    2. DML语句 数据库操纵语言: 插入数据insert. 删除数据delete. 更新数据update. 查询数据select
    3. DCL语句 数据库控制语言: 例如控制用户的访问权限grant. revoke

数据库表的详细操作

表的详细操作(增删改查)

表----------------文件
储存引擎engine -> show engines;

myisam  数据存在硬盘中
innodb  数据存在硬盘中(default)
blackhole  黑洞. 不存数据
memory 数据存在内存中. 关闭服务器数据就消失了


create table t1(id int)engine=innodb;
innodb储存引擎: 本质就是一段处理程序

1. 创建表
create table t1(
    id int,
    name varchar(8) not null
);

注意: 1. 在同一张表中. 字段名不能相同
    2. 宽度和约束条件可选
    3. 字段名和数据类型是必须的
    4. 表的最后一个字段不要加逗号

2. 修改表
    1. 修改表名
        alter table t1 rename t2;
    2. 增加字段
        alter table t1 add age int [完整性约束条件...],  -> 可以添加多条字段
                           sex varchar(10) [完成性约束条件...];

        alter table t1 add age int [完整性约束条件...] first;  -> 将字段添加至第一位

        alter table t1 add age int [完整性约束条件...] after name;  -> 将字段添加在某个字段后面

    3. 删除字段
        alter table t1 drop age;

    4. 修改字段
        alter table t1 modify name varchar(10) [完整性约束条件...]  -> modify不能修改字段名字

        alter table t1 change 旧字段名 新字段名 新数据类型 [完整性约束条件...]  -> 可以修改任何条件

3. 复制表
    create table t2 select id, name, age from test.t1;  -> 将t1中查看的数据复制给t2
    create table t2 select id, name, age from test.t1 where 1 < 0;  -> 只创建字段名. 没有数据




表字段之数据类型:
create table t3(id int);  -> 整型

create table t10(x float(255, 30));  -> 常用
create table t11(y double(255, 30));  -> 常用
create table t12(z decimal(65, 30)); -> 最精准


insert into t10 values(1.11111111111111111111111111111111)
insert into t11 values(1.11111111111111111111111111111111)
insert into t12 values(1.11111111111111111111111111111111)


时间类型:
create table student(
    id int,
    name varchar(16),
    birth date,
    class_time time,
    reg_time datetime,
    born_year year
);

insert student values
(1, 'egon', '1993-01-23', '08:30:00', "2020-3-23 09:30:21", "1993")  -> 一一对应

create table t13(x datatime);  -> 范围广
create table t14(y timestamp);  -> 节省空间. 且不传值会自动更新current_timestamp
create table t15(
    id int,
    name varchar(16),
    commit_time timestamp
);


枚举和集合:
create table t18(
    id int,
    name varchar(16),
    gender enum("男", "女", "未知"),  -- 选一个插入. 如果插入值不在列表中. 则会显示报错
    hobbies set("play", "music", "read", "movie")  -- 可以多选插入. 如果插入在列表中不存在.则会报错
);

insert into t18 values (1, "Maxs_hu", "男", "play,music,movie,program")  -- set传入数据逗号后面不可加空格


补充:
    char和varchar的区别:
        1.char储存字符串会以固定长度储存(长度不足补空格). 而varchar则会以字节宽度+1(头)去储存.  -> varchar减小io
        相比俩说更加节省空间. 但是当字符串长度等于字符长度的时候. varchar会因为头占一个字节比char多一个字节
        2. select name.char_length from t18;  -- 查看字符数
        3. 使用where查询的时候. 不会在意字符后面跟的空格. 而使用like模糊查询的时候严格按照字符查询 _: 任意字符 %:任意个数字符
        

表的约束条件和表关系的建立

1. 表的约束条件:
# primary key: 不为空且唯一
create table t5(
    id int,
    name varchar(10),
    primary key(id, name)  -- 联合主键: 不能重复
);

insert into t5 values(1, "Maxs_hu");
insert into t5 values(1, "tom");
insert into t5 values(1, "Maxs_hu");  -- 会报错. 因为主键不能重复

# auto_increment: 自增
set global auto_increment_increment = 5;  -- 设置全局自增步长
set global auto_increment_offset = 3;  -- 设置开始第一个id为3

create table t6(
    id int primary key auto_increment,
    name varchar(16)
);

# not null 和 default组合: 将没有传值的默认为0
create table t7(
    id int not null default 0,
    name varchar(16)
);

# unique: 唯一的  uni
create table t8(
    id int unique,
    name varchar(16)
);

# not null 和 unique 组合的化学反应: 变成主键pri
create teble t9(
    id int not null unique,
    name varchar(16)
);

desc t9;



2. 表关系建立
---------------多对一: 强耦合
# foreign key: 外键

create table department(  -- 先创建父表
    id int primary key,
    name varchar(20) not null
);
create table employee(  -- 关联父表(department主键id), 同步更新, 同步删除
    id int primary key,
    name varchar(20) not null,
    dpt_id int,
    foreign key(dpt_id) references department(id)
    on delete cascade
    on update cascade
);
insert into department values  -- 先往父表department中插入记录
(1, "叙利亚战区"),
(2, "俄罗斯战场"),
(3, "东部战场");
insert into employee values  -- 再往字表employee中插入记录
(1, "Maxs_hu", 1),
(2, "Mokeke", 2),
(3, "longge", 2),
(4, "xiaoergu", 3),
(5, "alex", 3);

delete from department where id = 3;  -- 删除父表department. 字表employee中对应的记录会跟着删除
select * from employee;

update department set name = "东部战区" where id = 2;  -- 修改父表中department. 表employee中记录也会跟着改变
select * from employee;



--------------多对多: 常用底层架构

create table book(  -- 先创建book父表
    id int primary key,
    name varchar(20) not null
);
create table author(  -- 先创建author父表
    id int primary key,
    name varchar(16) not null
);

create table author2book(  -- 在创建关联表
    id int primary key auto_increment,
    author_id int not null,
    book_id int not null,
    foreign key(book_id) references book(id)  -- 关联book表格
    on delete cascade
    on update cascade,
    foreign key(author_id) references author(id)  -- 关联author表格
    on delete cascade
    on update cascade
);
insert into book values  -- 先向父表中插入数据
(1, "葵花宝典"),
(2, "九阴白骨抓"),
(3, "轻功水上漂"),
(4, "辟邪剑法"),
(5, "九阴真经"),
(6, "掏奶龙抓手");
insert into author values  -- 先向父表中插入数据
(1, "Maxs_hu"),
(2, "mary"),
(3, "Mokeke"),
(4, "tom");
insert into author2book(author_id, book_id) values
(1, 6),
(3, 5),
(2, 4),
(1, 2),
(4, 3),
(3, 1),
(2, 6),
(3, 1),
(4, 5);

可以通过update来操作多对多表格去联动数据

建立表关系顺序: 多对多 > 多对一 > 一对一

表详细操作知识点总结

总结:
    1.约束条件
        not null
        default
        unique

        primary key
        auto_increment

        unsigned  -- 写在int后面. 确保数值大于0

        foreign key  -- 外键

    2. 表之间建立关系
       ---多对多
           emp          dep
                emp2dep  -- 建立中间表存父表id

           emp2dep: fk(dep_id) -> dep(id)  fk(emp_id) -> emp(id)
       ---多对一
        emp: fk(emp_id) -> dep(id)
        dep -> 父表

       ---一对一
       fk(dep_id)+uk

标签:--,name,int,数据库,id,part1,操作,table,create
From: https://www.cnblogs.com/Maxs-message/p/16659721.html

相关文章

  • 51 | JAVA_数据库JDBC_连接池
    JDBC连接池类似的,在执行JDBC的增删改查的操作时,如果每一次操作都来一次打开连接,操作,关闭连接,那么创建和销毁JDBC连接的开销就太大了。为了避免频繁地创建和销毁JDBC连接,我......
  • 47 | JAVA_数据库JDBC查询
    JDBC查询导入依赖因为我们选择了MySQL5.x作为数据库,所以我们首先得找一个MySQL的JDBC驱动。所谓JDBC驱动,其实就是一个第三方jar包,我们直接添加一个Maven依赖就可以了:<d......
  • 48 | JAVA_数据库JDBC更新
    JDBC更新数据库操作总结起来就四个字:增删改查,行话叫CRUD:Create,Retrieve,Update和Delete。查就是查询,我们已经讲过了,就是使用PreparedStatement进行各种SELECT,然后处理结果......
  • 49 | JAVA_数据库JDBC事务
    JDBC事务数据库事务(Transaction)是由若干个SQL语句构成的一个操作序列,有点类似于Java的synchronized同步。数据库系统保证在一个事务中的所有SQL要么全部执行成功,要么全部......
  • 50 | JAVA_数据库JDBC_批量操作Batch
    JDBCBatch使用JDBC操作数据库的时候,经常会执行一些批量操作。例如,一次性给会员增加可用优惠券若干,我们可以执行以下SQL代码:INSERTINTOcoupons(user_id,type,expir......
  • 虚拟机下卸载操作系统之CentOS7
    虚拟机下卸载操作系统之CentOS7https://www.jianshu.com/p/5f35d4a47854......
  • 在VM里安装一WINDOES 操作系统
    一、安装VM虚拟机软件1、打开虚拟机文件2、根据导向安装3、激活VM,点击许可证键入即可,没有可以根据当前版本自行去浏览器上搜索即可二、新建虚拟机1、新建新的虚拟机2......
  • oracle数据库sql常用语句02
    ---视图---视图的概念:视图就是提供一个查询的窗口,所有数据来自于原表。---查询语句创建表createtableempasselect*fromscott.emp;select*fromemp;---创建视图【......
  • oracle数据库sql常用语句01
    --创建表空间createtablespaceitheimadatafile'c:\itheima.dbf'size100mautoextendonnext10m;--删除表空间droptablespaceitheima;--创建用户createuseritheima......
  • 数据库治理的探索与实践
    简介: 本文是MSE即将推出的一个数据库治理能力的预告,我们从应用的视角出发整理抽象了我们在访问、使用数据库时场景的一些稳定性治理、性能优化、提效等方面的实战经验,对......