zoukankan      html  css  js  c++  java
  • -- 数据库的操作命令 笔记


    -- 数据库的操作
    -- 连接数据库之前需要保证数据库已经启动
    -- 验证数据库是否已经启动
    -- ps aux | grep mysqld
    -- sudo service mysql status

    -- 启动mysql
    -- sudo service mysql start/restart


    -- 链接数据库
    -- mysql -u用户名 -p密码
    -- mysql -uroot -pmysql
    -- mysql -uroot -p 后续再输入密码 密码不显示(回显echo)

    -- 退出数据库
    -- quit / exit / ctrl+d

    -- sql语句最后需要有分号;结尾
    -- 显示数据库版本
    select version();

    -- 显示时间
    select now();

    -- 查看所有数据库
    show databases;

    -- 创建数据库
    -- create database 数据库名 charset=utf8;
    create database python26 charset=utf8;

    -- 查看创建数据库的语句
    show create database python26;

    -- 查看当前使用的数据库
    select database();

    -- 使用数据库
    -- use 数据库的名字
    user python26;

    -- 删除数据库
    -- drop database 数据库名;
    drop database py27;


    -- 数据表的操作

    -- 查看当前数据库中所有表
    show tables;

    -- 创建表的基本用法
    -- auto_increment表示自动增长
    -- not null 表示不能为空
    -- primary key 表示主键
    -- default 默认值
    -- create table 数据表名字 (字段 类型 约束[, 字段 类型 约束]);
    -- create table students(字段的名字 类型 约束, 字段2的名字 类型 约束);

    create table class26(
    -> id int unsigned primary key auto_increment not null,
    -> name varchar(10));


    -- 查看表结构 describe -- 查看表中 字段名 类型 约束
    -- desc 数据表的名字;
    desc py26;

    -- 创建students表(id、name、age、high、gender、cls_id)
    create table students(
    id int unsigned primary key auto_increment not null,
    name varchar(30) default '',
    age tinyint unsigned default 0,
    high decimal(5,2),
    gender enum('男','女','人妖','保密'),
    cls_id int unsigned default 0
    );

    -- 插入一条数据到classes表中

    -- 查询classes表中所有的数据
    -- 插入一个数据到students表中

    -- 查询students表中的所有数据
    select * from students;

    -- 查看表的创建语句
    -- show create table 表名字;
    show create table students;

    -- 修改表-添加字段
    -- alter table 表名 add 列名 类型;
    alter students add birthday datetime;

    -- 修改表-修改字段:不重命名版 ---- 》 只是修改字段对应的类型和约束
    -- alter table 表名 modify 列名 类型及约束;
    alter table students modify birth date not null; # (-) sql 语句不识别,字符串可以

    -- 修改表-修改字段:重命名版
    -- alter table 表名 change 原名 新名 类型及约束;
    alter table students change birthday birth date not null;

    -- 修改表-删除字段
    -- alter table 表名 drop 列名;
    alter table students drop birth;

    -- 删除表
    -- drop table 数据表;
    drop table students;


    -- 增删改查(curd)

    -- 增加
    -- 全列插入
    -- insert [into] 表名 values(...)
    -- 主键字段 可以用 0 null default 来占位
    -- 向classes表中插入 一个班级

    -- 向students表插入 一个学生信息
    insert into students values (0,'郭靖',23,1.78,'男',1);

    -- 部分插入
    -- insert into 表名(列1,...) values(值1,...)
    insert into students (name,high,gender) values ('周伯通',165,'男');
    insert into students (name,high,gender) value ('周伯通',165,'男');

    -- 插入数据的时候可以改变顺序
    insert into students (high,name,gender) value (165,'周伯通','男');

    -- 多行插入
    insert into students (name,high,gender) values ('黄蓉',165,'女'),('张三丰',175,'男');


    -- 查询基本使用
    -- 查询所有列
    -- select * from 表名;
    select * from students;

    -- 查询指定列
    -- select 列1,列2,... from 表名;
    select name ,age,high,gender from students;

    -- 可以使用as为列或表指定别名 as可以省略
    -- select 字段[as 别名] , 字段[as 别名] from 数据表 where ....;
    select name as 姓名,age as 芳龄 ,high 海拔,gender from students;

    -- 修改
    -- update 表名 set 列1=值1,列2=值2... where 条件;
    update students set age = 158 where name='郭靖';
    -- 同时修改一条记录中的多个字段的值
    update students set age=220,high=180 where name='张三丰';

    -- 删除
    -- 物理删除
    -- delete from 表名 where 条件
    delete from students where id = 4;

    -- 逻辑删除
    -- 用一个字段来表示 这条信息是否已经不能再使用了
    alter table students add is_delete bit
    -- 给students表添加一个is_delete字段 bit 类型

    目标 奋斗,不一定能够成功,可有一定的几率是可以成功的. 但是,不奋斗是不会成功的.
  • 相关阅读:
    IOS开发通过代码方式使用AutoLayout (NSLayoutConstraint + Masonry) 转载
    iOS8.0 使用Photos.framework对相册的常用操作
    iOS 通用button 上图下字
    navigationItem的设置和titleView的设置
    iOS 设置导航栏 返回按钮文字隐藏
    iOS uitableivewCell 下划线顶格
    gitlab创建项目代码:
    iOS block用作属性封装代码
    iOS实录:GCD使用小结(一)
    代码处理 iOS 的横竖屏旋转
  • 原文地址:https://www.cnblogs.com/yuanzhengchi/p/8478683.html
Copyright © 2011-2022 走看看