mysql 示例



綜述:兩張表,一張顧客信息表customers,一張訂單表orders

1、創建一張顧客信息表customers,字段要求如下:
c_id 類型爲整型,設置爲主鍵,並設置爲自增長屬性

c_name 字符類型,變長,寬度爲20

c_age 微小整型,取值範圍爲0~255(無符號)

c_sex 枚舉類型,要求只能在('M','F')中選擇一個值

c_city 字符類型,變長,寬度爲20
c_salary 浮點類型,要求整數部分最大爲10位,小數部分爲2位
在表中任意插入3條記錄,c_name爲"Zhangsan","Lisi","Wangwu", c_city儘量 寫"Beijing","Shanghai" ......
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 float(12,2)
);

insert into customers values
(1,"Zhangsan",25,"M","Beijing",8000),
(2,"Lisi",30,"F","Shanghai",10000),
(3,"Wangwu",27,"M","Shenzhen",3000);
2、創建一張訂單表orders,字段要求如下:
o_id 整型
        o_name 字符類型,變長,寬度爲30
        o_price 浮點類型,整數最大爲10位,小數部分爲2位
設置此表中的o_id字段爲customers表中c_id字段的外鍵,更新刪除同步
在表中任意插入5條記錄(注意外鍵限制)
o_name分別爲"iphone","ipad","iwatch","mate9","r11",其他信息自己定
create table orders(
o_id int,
o_name varchar(30),
o_price float(12,2),
foreign key(o_id) references customers(c_id)
on delete cascade
on update cascade);

insert into orders values
(1,"iphone",5288),
(1,"ipad",3299),
(3,"mate9",3688),
(2,"iwatch",2222),
(2,"r11",4400);
3、返回customers表中,工資大於4000元,或者年齡小於29歲,滿足這樣條件的前2條記錄
select * from customers where salary >4000 and age<29 limit2;
4、把customers表中,年齡大於等於25歲,並且地址是北京或者上海,這樣的人的工資上調15%
select * , salary*1.15 from cus where age >=25 and( city ='beijing' or city='shanghai') ;
update cus set salary *1.15 where age>=25 and(city='beijing' or city='shanghai');
and city in('beijing' or 'shanghai ')

5、把customers表中,城市爲北京的顧客,按照工資降序排列,並且只返回結果中的第一條記錄

select *from cus where city='beijing' order by salary desc limit 1;
6、選擇工資salary最少的顧客的信息
select*from cus order by salary limit 0;
select* from cus where salary=(select min(salary)from cus );
7、找到工資大於5000的顧客都買過哪些產品的記錄明細
 select * from ord where id in (select id from cus where salary>5000);
8、刪除外鍵限制
show create table ord ;
alter table ord drop foreign key 外鍵名;
9、刪除customers主鍵限制
1 刪除自增長屬性
alter table cus modify id int;
2 刪除主鍵
alter table cus drop primary key;
10 增加 cus 主鍵限制
alter table cus add primary key (id);



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章