mysql基础小结
基础操作
1 # 选择数据库 2 show databases; 3 4 # 创建temp库,utf8 5 create database temp character set utf8; 6 7 # 选择temp库 8 use temp; 9 10 # 创建students表 11 CREATE TABLE students ( 12 id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY, # 学生id,int整数型列, 13 # unsigned无符号从零开始修饰符有符号是负数到正数 14 # notnull不许为空,因为id后边要作为primary key,主键不可为空,如果不指定该属性,默认可为空 15 # auto_increment自增长 16 name CHAR(8) NOT NULL,# char 17 sex CHAR(4) NOT NULL, 18 age TINYINT UNSIGNED NOT NULL, 19 tel CHAR(13) NULL DEFAULT '-' #默认值为空,不想为空指定为—— 20 ); 21 22 # 增加address列students 23 alter table students add address char(60); 24 25 # age 列后插入 26 alter table students add birthday date after age; 27 28 # 删除birthday 29 alter table students drop birthday; 30 31 # tel列 重命名为 telphone 32 alter table students change tel telphone char(13) default "-"; 33 34 # name列 数据类型修改为 char(16) 35 alter table students change `name` `name` char(16) not null; 36 37 # 删除 birthday 列 38 alter table students drop birthday; 39 40 # 重命名 students 表为 workmates 41 alter table students rename workmates; 42 43 # 查看表 44 show tables; 45 46 # 查看表信息 47 describe workmates; 48 describe students; 49 # 删除 students 表 50 drop table workmates; 51 52 # 删除 temp 库 53 drop database temp;
查询语句
1 # 选择数据库 2 use credit; 3 4 # 查询语句 5 SELECT * FROM user_info limit 20; 6 7 SELECT user_id,occupation FROM user_info limit 20; 8 9 SELECT * FROM user_info 10 where sex=1 and (education=1 or marriage=1) 11 limit 20; 12 13 # 插入语句 14 insert into user_info values ('A10','1','1','1','1','1'); 15 insert into user_info(user_id,sex) values ('A10','1'); 16 17 # 记录更新 18 set sql_safe_updates = 0; 19 update user_info set occupation='2',education = '2' 20 where user_id = 'A10'; 21 22 # 删除记录 23 delete from user_info where user_id = 'A10'; 24 # delete from user_info; # 删除user_info表所有记录 25 26 # 排序 27 select * from user_info order by user_id; 28 select * from user_info order by user_id asc; # 升序排列 29 select * from user_info order by user_id desc; # 降序排列 30 select * from user_info order by sex desc,education desc; 31 select * from user_info where sex = 1 order by user_id; 32 33 # 分组 34 SELECT 35 user_id, AVG(trans_amount) 36 FROM 37 bank_detail 38 GROUP BY user_id; 39 40 SELECT 41 user_id,trans_type, AVG(trans_amount) trans_amount_avg 42 FROM 43 bank_detail 44 GROUP BY user_id,trans_type; 45 46 # 空值 47 select * from user_info where sex is null; 48 select * from user_info where sex is not null; 49 50 # 模糊匹配 51 select * from user_info where user_id like '278%'; # user_id以278开头 52 select * from user_info where user_id like '%278%'; # user_id包含278 53 select * from user_info where user_id like '%278'; # user_id以278结尾 54 55 # 正则匹配 56 SELECT * FROM user_info WHERE user_id REGEXP '^278'; # user_id以278开头 57 select * from user_info where user_id like '278%'; 58 59 # 内连接 60 SELECT user_info.user_id,# 查询用户表里的 61 sex, 62 trans_amount 63 FROM user_info,bank_detail #两个表中都有要查询的字段 64 WHERE user_info.user_id = bank_detail.user_id;# 连接条件两张表上的用户ID保持一致 65 66 select count(*) from ( 67 SELECT user_info.user_id, 68 sex, 69 trans_amount 70 FROM user_info,bank_detail 71 WHERE user_info.user_id = bank_detail.user_id) a; 72 73 # 左连接 74 select count(*) from ( 75 SELECT 76 user_info.user_id, sex, trans_amount 77 FROM 78 user_info 79 LEFT JOIN 80 bank_detail ON user_info.user_id = bank_detail.user_id) a; 81 82 # 右连接 83 SELECT 84 user_info.user_id, sex, trans_amount 85 FROM 86 user_info 87 RIGHT JOIN 88 bank_detail ON user_info.user_id = bank_detail.user_id; 89 90 91 # 自定义函数 92 DROP FUNCTION if exists standardize; 93 94 DELIMITER $ 95 CREATE FUNCTION standardize(x float,y int) 96 RETURNS float 97 BEGIN 98 DECLARE a float; 99 SET a = x/5; 100 RETURN a+y; 101 END $ 102 DELIMITER ; 103 104 select standardize(3.2,3); 105 106 SELECT 107 STANDARDIZE(trans_amount,1) 108 FROM 109 bank_detail; 110 111 # 存储过程 112 drop procedure if exists info; 113 114 delimiter $ 115 create procedure info(uid int) 116 begin 117 select * from user_info where user_id = uid; 118 end $ 119 delimiter ; 120 121 call info(10); 122 123 # 重复数据 124 SELECT 125 user_id, COUNT(*) AS repetitions 126 FROM 127 bank_detail 128 GROUP BY user_id 129 HAVING repetitions > 1; 130 131 # 过滤重复 132 select count(*) from 133 (select * from bank_detail 134 group by user_id,trans_amount) a;