• 数据库之增删改查


    1, 数据库

    • 数据:描述事物的符号记录
    • 数据库:就存放数据的仓库只不过这个仓库是存储在计算机存储设备上
    • 数据库中的数据按照一定的数据模型组织,描述和储存,具有较小的冗余度,较高的数据独立性和易扩展性,并为各种用户分享
    • 数据管理系统:科学的组织和存储数据,高效的获取和维护数据的一个系统软件
    • 数据库服务器:就是对外专门提供数据的一个机器
    • mysql:是一个关系型数据库管理系统,mysql最流行的关系型数据库管理系统,在web应用方面mysql是最好的RDBMS应用软件之一

    2,SQL语言

    • SQL:即结构化查询语言
    • SQL语言 主要用于存储数据,查询数据,跟新数据和管理关系型数据库,SQL语言由IBM开发
    • SQL语言的三种类型:
      • DDL语句:数据库定义语言,如create,drop,alter
      • DML语句:数据库操纵语言:插入数据insert into,删除数据delete,更新数据update,查询数据select
      • DCL语句:数据库控制语言:控制用户的访问权限grant,revoke

    3,数据库的增删改查

    • 增:create database db;
    • 删:drop database db;
    • 改:alter database db charset utf8
    • 查:show databases;
      • show create database db;
    • 用:use db;
    • select database() 查看在当前那个文件夹

    4,表的增删改查

    • 增:create table t1(id int ,name char);
    • 删:drop table t1;
    • 改:update db set name = ‘ss’ where id = 1;
      • insert into t1 values(2,‘ssss’);
    • 查:show tables;
      • show create table t1;
      • select * from t1;
      • select id ,name from t1;
      • desc t1;

    5,操作文件的一行行记录

    • 增:insert into db.t1 values(1,‘david’),(2,‘alang’);
    • 删:delete from t1 where id = 1;
      • 清空记录:
        • delete from t1;(行删除)
        • truncate t1(推荐使用,删除速度快,)
    • 改:update t1 set name= ‘xiaoyu’ where id = 2;
      • 修改id为主键并且自增
        • alter table t1 modify id int primary key auto_increment;
    • 查:select * form t1;

    6,拷贝表结构

    • 拷贝表结果,表结构+ 表数据一起拷贝

      mysql> select * from t1;
      +----+-------+--------+
      | id | name  | gender |
      +----+-------+--------+
      |  1 | david | NULL   |
      |  2 | alang | NULL   |
      |  3 | alex  | female |
      +----+-------+--------+
      3 rows in set (0.05 sec)
      
      
      mysql> create table t2 select * from t1;
      Query OK, 3 rows affected (0.04 sec)
      Records: 3  Duplicates: 0  Warnings: 0
      
      mysql> select * from t2;
      +----+-------+--------+
      | id | name  | gender |
      +----+-------+--------+
      |  1 | david | NULL   |
      |  2 | alang | NULL   |
      |  3 | alex  | female |
      +----+-------+--------+
      3 rows in set (0.00 sec)
      
      
    • 条件为假时,只拷贝表结构,不拷贝表数据,新表数据查不到任何记录

      mysql> select * from t1;
      +----+-------+--------+
      | id | name  | gender |
      +----+-------+--------+
      |  2 | alang | NULL   |
      |  3 | alex  | female |
      +----+-------+--------+
      2 rows in set (0.00 sec)
      
      mysql> create table t3 select * from t1 where 1=1;
      Query OK, 2 rows affected (0.04 sec)
      Records: 2  Duplicates: 0  Warnings: 0
      
      mysql> select * from t3;
      +----+-------+--------+
      | id | name  | gender |
      +----+-------+--------+
      |  2 | alang | NULL   |
      |  3 | alex  | female |
      +----+-------+--------+
      2 rows in set (0.00 sec)
      
      mysql> create table t4 select * from t1 where 1=2;
      Query OK, 0 rows affected (0.06 sec)
      Records: 0  Duplicates: 0  Warnings: 0
      
      mysql> select * from t4;
      Empty set (0.00 sec)
      
      mysql> desc t4;
      +--------+-------------+------+-----+---------+-------+
      | Field  | Type        | Null | Key | Default | Extra |
      +--------+-------------+------+-----+---------+-------+
      | id     | int(11)     | NO   |     | 0       |       |
      | name   | varchar(10) | YES  |     | NULL    |       |
      | gender | varchar(10) | YES  |     | NULL    |       |
      +--------+-------------+------+-----+---------+-------+
      3 rows in set (0.07 sec)
      
    希望你眼眸有星辰,心中有山海,从此以梦为马,不负韶华
  • 相关阅读:
    并发编程-操作系统简史,多道技术
    python下的excel表格处理 内含面试题
    epoll模型的探索与实践
    nginx搭建静态网站
    面向对象基础
    python+Django 下JWT的使用
    linux的history命令
    携程apollo配置中心Quick Start
    redis哨兵
    redis的主从复制
  • 原文地址:https://www.cnblogs.com/daviddd/p/12047358.html
Copyright © 2020-2023  润新知