zoukankan      html  css  js  c++  java
  • mysql示例及练习2

    #创建数据库并应用
    create database shopdb;
    use shopdb;

    #创建表customers
    create table customers(
    c_id int primary key auto_increment,
    c_name varchar(20),
    c_age tinyint unsigned,
    c_sex enum("M","F"),
    c_city varchar(20),
    c_salary decimal(12,2));

    #创建表orders
    create table orders(
    o_id int,
    o_name varchar(30),
    o_price decimal(12,2),
    foreign key(o_id) references customers(c_id)
    on delete cascade
    on update cascade
    );

    #在customers中添加数据
    insert into customers values
    (1,'Tom',25,'M','上海',10000),
    (2,'Lucy',23,'F','广州',12000),
    (3,'Jim',22,'M','北京',11000);

    #在orders中添加数据
    insert into orders values
    (1,"iphone",5288),
    (1,"ipad",3299),
    (2,"iwatch",2222),
    (2,"r11",4400);

    1.customers表中,工资大于4000元,或者年龄小于29岁,满足这样条件的前2条记录
    select * from customers where
    c_salary > 4000 or c_age<29
    limit 2;

    2.customers表中,年龄大于等于25岁,并且地址是北京或者上海,这样的人的工资上调15%
    select *,c_salary * 1.15 from customers where
    c_age>=25 and c_city in ("北京","上海");
    3、把customers表中,城市为北京的顾客,按照工资降序排列,并且只返回结果中的第一条记录
    select * from customers where c_city="北京" order by c_salary limit 1;

    4、选择工资c_salary最少的顾客的信息
    select * from customers order by c_salary limit 1;

    5、找到工资大于5000的顾客都买过哪些产品的记录明细
    select * from orders inner join customers on c_id = o_id;
    (select c_id from customers where c_salary > 5000)
    inner join orders on c_id = o_id;
    6、删除外键限制

    7、删除customers主键限制

    8、增加customers主键限制c_id
  • 相关阅读:
    python dict
    用Python requests beautifulSoup 获取并显示中文信息
    Python information to learn
    Python 中的异常
    Python 中的函数
    安装Linux系统Fedora 23
    (转)开源协议的比较
    WizNote for linux installation
    linux下编译bib、tex生成pdf文件
    NLP学术组织、会与论文
  • 原文地址:https://www.cnblogs.com/huohu121/p/12300466.html
Copyright © 2011-2022 走看看