zoukankan      html  css  js  c++  java
  • mysql安装与使用

    dpkg -l | grep mysql         //查看是否安装mysql
    apt install mysql-server        //安装mysql-server
    mysql -u root -p           //-u表示后接用户名,-p表示用密码登录。初次安装后,直接敲回车跳过输入密码
    mysql_secure_installation   //初始化数据库,初次安装后建议操作,在里面可以设置root密码。
    show databases;
    show tables;
    flush privileges;
    exit;
    quit;

    /var/lib/mysql/             //创建的数据库文件
    /etc/mysql/mysql.conf.d/mysqld.cnf     //配置文件

    create database 数据库名;        //创建数据库
    create table 表名(属性n 类型 [完整性约束]); //创建表
    show databases|tables;           //显示所有数据库或所有表
    drop 数据库名|表名;            //删除数据库或表,有外键关联的表,要先删除外键
    use/close 数据库名;                 //切换数据库的使用。use使用一个数据库,close关闭一个数据库
    desc (describe) 表名;            //查看表结构;
    show create table 表名;           //查看详细表结构
    alter table 旧表名 rename 新表名;      //修改表名
    alter table 表名 modify 属性名 属性类型 [完整性约束] [first|after 字段];     //修改属性
    alter table 表名 add 新属性名 类型 [完整性约束] [first|after 字段];        //在after字段后添加属性
    alter table 表名 drop 完整性约束(属性);                     //撤销属性的完整性约束
    alter table 表名 change 旧属性 新属性名 新属性类型;              //用新属性替换旧属性

    完整性约束:
    primary key          //主键
    unique            //唯一性
    auto_increment           //自增长,类型必须为int类
    default : default '默认值'      //设置一个默认值
    foreign key : constraint 外键别名 foreign key(字段1,字段2等) references 主表名(字段1,字段2等)

    创建索引:(某些数据引擎不支持一些特殊索引,因此需要更换引擎)
    create table t1(id int,name char(10),index 索引名(id));        //用id字段给t1新表创建一个普通索引
    create table t2(id int,name char(10),unique index 索引名(id));      //用id字段给t2新表创建一个unique索引
    create [unique|spatial] index 索引名 on 表名(属性 [asc|desc]);     //对已经存在的表创建索引。
    alter table 表名 add [unique|spatial] index 索引名(属性 [asc|desc]);  //alter关键字创建索引。
    drop index 索引名 on 表名;                       //删除索引

    增加/删除/更新数据:
    //插入元组到表中。
    insert into 表名 [snum,sname,ssex,sage,dnum,sclass] values('01','tian','男','23','001','02012');

    //创建个新表,将select出来的数据插入
    insert into 新表名(属性1,属性2,属性n)
    select 属性1,属性2,属性n
    from 表名 where 条件

    //删除表中满足条件的数据
    delete from 表名 where 条件;

    //将所有老师的工资上调5%
    update teacher set salary=salary*1.05;
    update 表名 set 属性=子查询/新值 where 条件; //将满足条件的元组的属性更新为新值或子查询的值

    查找:
    select [distinct] 属性1,属性名2....属性名n from 表名
    where 条件(>= <= = > <)           //对字符类型或数值类型做比较。
    where like 张%                //表示以张开头的任意字符串,%表示0个或多个任意字符
    where like 张_                 //表示张某的字符串,_表示一个任意字符
    where not like 张%            //所以不以张开头的字符串
    [order by 属性 [asc|desc]]
    多表查询:
    select 属性 from 表1,表2
    where 表1.s# = 表2.s# and c# = '001'           //表1和表2通过s#连接,并且满足c#='001'

    //表的自身连接
    select t1.tname [as teacher1],t2.tname [as teacher2]   //可以去掉as teacher1
    from teacher t1,teacher t2                //类似于c语言用teacher声明两个变量表示teacher类型
    where t1.salary > t2.salary ;

  • 相关阅读:
    javascript中数组去重的4种方法
    dede使用方法----实现英文版的搜索功能
    dede去掉当前位置position后面的箭头
    dede使用方法----如何转换时间戳
    Python字符串、元组、列表、字典互相转换的方法
    Python 列表的操作
    Python 元祖的操作
    Python 操作文件、文件夹、目录大全
    python文件目录操作大全
    python用time函数计算程序运行时间
  • 原文地址:https://www.cnblogs.com/Ccluck-tian/p/12040748.html
Copyright © 2011-2022 走看看