zoukankan      html  css  js  c++  java
  • 数据库的基本操作(一)

    mysqld install # 安装sql服务

    net start mysql # 启动服务

    net stop mysql # 停止服务

    select user(); # 查看当前用户

    mysql -uroot -p # 用户名 密码
    mysql -uroot -h192.168.12.61    # 连接其他人的mysql

    set password = password('123');      # 设置密码(注意分号,忘记了可换行直接补上一个‘;’)

    c # 表示放弃当前要执行的sql语句

    create user '用户名'@'允许的网段' identified by '密码' # 创建账号
    # 创建账号
    mysql> create user 'eva'@'192.168.10.%' identified by '123'; # 指示网段
    mysql> create user 'eva'@'192.168.10.5' # 指示某机器可以连接
    mysql> create user 'eva'@'%' # 指示所有机器都可以连接
    mysql> show grants for 'eva'@'192.168.10.5'; # 查看某个用户的权限
    # 远程登陆
    mysql -uroot -p123 -h192.168.10.3
    # 给账号授权
    mysql> grant all on . to 'eva'@'%';
    mysql> flush privileges; # 刷新使授权立即生效
    # 创建账号并授权
    mysql> grant all on . to 'eva'@'%' identified by '123'

    基础操作:

    数据库的基本操作:

    create database 数据库名;

    show databases; 查看数据库

    use 数据库名  切换到库中

    数据表的基本操作:

    create table damo(num int, user_name char(12), password char(32));   创建一个表

    show tables; 查看当前库中的所有表

    desc 表名    查看表结构

    数据的基本操作:

    insert into demo values(1,'xuhuo','xuhuo');

    select * from 表名;  查看表的所有内容

    select 字段1,字段2... from 表名     查看表的指定字段

    update demo set password = 'xuhuo1' where num = 1;     # 修改数据

    delete from demo where num = 1;     删除数据,where后边跟删除的数据

    数据类型

    • 数字类型

      • 整数 tinyint(0,255) int (0-四十多亿)

      • 默认创建的所有的数据都是有符号的

      • create table t1(i1 tinyint unsigned,i2 int unsigned) # unsigned 是约束,表示无符号

      • 小数 float double

      • create table t3(f1 float(7,2),f2 double)

    • 字符串

      • 定长:节省时间 浪费空间 char (255个字符)

      • 变长:节省空间 浪费时间 varchar(65535个)

      • create table t5(c1 char(5),c2 varchar(5))

    • 时间类型

      • datetime # 年月日时分秒 0000-9999年

      • date # 年月日

      • time # 时分秒

      • year # 年

      • timestamp # 年月日时分秒 1970-2038年

    • enum 和 set

      • enum 单选

      • create table t6(username char(12),sex char() enum(''))

      • set 多选

    单表操作:

    insert

    # 增 insert
    # insert into emp values (3,'alex',84,'female',2.2,'linux,python'),
    # (4,'alex',84,'female',2.2,'linux,python');
    # insert into emp(id,name) values (5,'wusir'), (6,'wusir');
    # insert into emp2 select * from emp;
    # insert into emp2(id,name) select id,name from emp;

    # 删
    delete

    # 改update
    update 表 set 字段1= 值1, 字段1=值2 where 条件;

    select:

    # 建表准备:
    create table employee(
    id int not null unique auto_increment,
    emp_name varchar(20) not null,
    sex enum('male','female') not null default 'male', #大部分是男的
    age int(3) unsigned not null default 28,
    hire_date date not null,
    post varchar(50),
    post_comment varchar(100),
    salary double(15,2),
    office int, #一个部门一个屋子
    depart_id int
    );

    https://www.cnblogs.com/Eva-J/articles/9688313.html

    select * from 表名;

    # 指定列查询:
    select name from 表名;
    # 在列中使用四则运算:
    select emp_name,salary * 12 from employee;
    # 重命名查询结果的字段:
    select emp_name,salary * 12 as annul_salary from employee;
    # 去重 distinct
    select distinct post from employee;

    # concat()拼接
    select concat('姓名:',emp_name),concat('年薪:',salary*12) from employee;

    case when语句
    SELECT
    (
    CASE
    WHEN emp_name = 'jingliyang' THEN
    emp_name
    WHEN emp_name = 'alex' THEN
    CONCAT(emp_name,'_BIGSB')
    ELSE
    concat(emp_name, 'SB')
    END
    ) as new_name
    FROM
    employee;

    where:

    where语句 根据条件筛选行
    比较运算 = > < >= <= !=/<>
    between a and b
    select * from employee where salary between 10000 and 20000;
    in
    select * from employee where salary in (17000,19000);
    like模糊查询
    _通配符,匹配一个字符长度的内容
    select * from employee where emp_name like 'jin___';
    %通配符,匹配任意长度的内容
    select * from employee where emp_name like 'jin%';
    regexp正则匹配
    select * from employee where emp_name regexp '^jin';

    逻辑运算
    # and
    select * from employee where age > 18 and salary > 10000;
    # or
    select * from employee where age > 18 or salary > 10000;
    # not
    select * from employee where salary not in (10000,17000);

    关于null
    查看岗位描述为null的员工信息
    select * from employee where post is null;
    查看岗位描述不为null的员工信息
    select * from employee where post is not null;

    五个聚合函数
    count()
    max()
    min()
    avg()

    group by分组聚合
    查询岗位名和岗位包含的所有员工姓名
    select post,emp_name from employee group by post;
    查询各部门内年龄在20岁以上的人的平均薪资
    select post,avg(salary) from employee where age > 20 group by post;

    过滤having(group by + 聚合函数)
    查询平均薪资大于一万的部门:
    select post from employee group by post having avg(salary) > 10000;

    order by 排序
    升序
    select * from employee order by salary;
    select * from employee order by salary asc;
    降序
    select * from employee order by salary desc;
    select * from employee order by age,salary desc;
    select * from employee order by age desc,salary asc;

    limit
    select * from 表 order by 列 limit n; # 取前n条
    select * from 表 order by 列 limit m,n;从m+1开始,取n条
    select * from 表 order by 列 limit n offset m;从m+1开始,取n条

    # 使用关键字必须按照顺序写
    select * from 表 where 条件 group by 分组 having 过滤 order by 排序 limit n;

    select post,group_concat(emp_name) from employee group by post;
    # 查询一个组中的所有成员

    # 练习

    https://www.cnblogs.com/Eva-J/articles/11074845.html

    https://www.cnblogs.com/Eva-J/articles/9772614.html

  • 相关阅读:
    LeetCode 623. Add One Row to Tree
    LeetCode 894. All Possible Full Binary Trees
    LeetCode 988. Smallest String Starting From Leaf
    LeetCode 979. Distribute Coins in Binary Tree
    LeetCode 814. Binary Tree Pruning
    LeetCode 951. Flip Equivalent Binary Trees
    LeetCode 426. Convert Binary Search Tree to Sorted Doubly Linked List
    LeetCode 889. Construct Binary Tree from Preorder and Postorder Traversal
    LeetCode 687. Longest Univalue Path
    LeetCode 428. Serialize and Deserialize N-ary Tree
  • 原文地址:https://www.cnblogs.com/xuyuwei/p/11587174.html
Copyright © 2011-2022 走看看