Database操作
1、查看当前databases
mysql> show databases;
2、创建database
mysql> create database bigdata; //创建bigdata数据库
3、查看当前数据库用户信息
mysql> use mysql;
mysql> select * from user;
4、创建数据库用户同时授权
#创建一个密码为123456的zz用户,该用户可以通过所有Host访问bigdata数据库
mysql> grant all privileges on bigdata.* to zz@'%' identified by '123456';
Query OK, 0 rows affected (0.00 sec)
mysql> flush privileges;
Query OK, 0 rows affected (0.00 sec)
5、删除database
mysql> drop database bigdata;
Query OK, 0 rows affected (0.00 sec)
6、删除数据库用户
mysql> DROP USER 'username'@'host';
7、查询当前数据库连接访问数
mysql> show processlist;
若有mysql进程很卡, 通过这种方式找到,并kill
基本概念
常用数据类型:
数据类型 | 关键字 |
数值类型 | int (整数) 、 long (长整型) 、float (单精度)、 double (双精度)、 decimal ( 小数值 金额字段 ) |
字符串类型 | char ( 定长字符串 0-255字节) 、varchar (变长字符串 0-65535字节) |
日期和时间类型 | date (日期 YYYY-MM-DD) 、time (时间 HH:MM:SS)、datetime 、timestamp |
SQL概述
DML: (数据操纵语言) : INSERT / DELETE / UPDATE / SELECT
DDL: (数据定义语言): CREATE / ALTER / DROP / TRUNCATE / RENAME
DCL: (数据控制语言): COMMIT / ROLLBACK / GRANT / REVOKE / SAVEPOINT
约束
对表中列(或字段)的强制规定
常见的约束:
非空约束(not null)
唯一性约束(unique)
主键约束(primary key):非空约束 + 唯一性约束
可以通过有主键约束的列,唯一的确定表中的一条记录。
一个表中只能声明一个主键约束
通常创建主键约束的方式:添加AUTO_INCREMENT
外键约束(foreign key): 外键约束作用表A的列a,在赋值时,必须要求其赋的值是表A所关联的表B中相应列b中出现过的值。
添加外键的要求:要求表B的列b必须声明过主键约束或唯一性约束
默认值约束(default)
约束的分类:①根据约束数据列的限制:单列约束 vs 多列约束
②约束的作用范围:列级约束 vs 表级约束
通常,我们都是在create table的时候,给相应的列声明约束。
同时,我们还可以通过alter table 的方式,删除或添加列的约束
创建表示例:
create table stu_tb(
id int AUTO_INCREMENT primary key,
stu_num int,
stu_name varchar(100),
stu_age int,
#信息插入的时间
createtime timestamp default current_timestamp,
createuser varchar(100),
#信息更新的时间
updatetime timestamp default current_timestamp on update current_timestamp,
updateuser varchar(100)
);
CREATE TABLE `stu_tb` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`stu_num` int(11) DEFAULT NULL,
`stu_name` varchar(100) DEFAULT NULL,
`stu_age` int(11) DEFAULT NULL,
`createtime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`createuser` varchar(100) DEFAULT NULL,
`updatetime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`updateuser` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
注: MySQL创建表必须加上自增长ID主键
生产上最好加上 :createtime createuser updatetime updateuser
删除表:
drop table bigdata.stu_tb;
插入数据:
insert into bigdata.stu_tb(stu_num,stu_name,stu_age,createuser,updateuser)
values(111,'zz',23,'root','root');
更新数据
update bigdata.stu_tb set stu_age=22 where stu_num = 111;
查找数据
#条件查询 where and or
select * from bigdata.stu_tb;
select id,stu_name from bigdata.stu_tb where stu_age=18;
select * from bigdata.stu_tb where stu_age=18 and stu_name='zz';
select * from bigdata.stu_tb where stu_age=24 or stu_name='zz';
select * from bigdata.stu_tb where stu_age>18;
select * from bigdata.stu_tb where stu_age BETWEEN 14 AND 25;
select * from bigdata.stu_tb where stu_age IS NULL;
select * from bigdata.stu_tb where stu_age IS NOT NULL;
#模糊查询 like
select * from bigdata.stu_tb where stu_name like '%n%'; #包含n
select * from bigdata.stu_tb where stu_name like 'j%'; #以j开头
select * from bigdata.stu_tb where stu_name like '%u'; #以u结尾
select * from bigdata.stu_tb where stu_name like '__s%'; #占位符_ 第三个字母s
select * from bigdata.stu_tb where stu_name like '_\_s%'; #转义字符\ 第二个字母_s
#排序order by ,asc升序、desc降序
select * from bigdata.stu_tb ;
select * from bigdata.stu_tb order by stu_age;#默认是asc
select * from bigdata.stu_tb order by stu_age asc;
select * from bigdata.stu_tb order by stu_age desc;
按年龄升序 学号降序
select * from bigdata.stu_tb order by stu_age asc,stu_num desc;
#限制多少行 limit 行数
select * from rbigdata.stu_tb limit 2;
select * from bigdata.stu_tb order by stu_age asc limit 2;
select * from bigdata.stu_tb order by stu_age asc,id asc limit 2;
删除数据
delete from bigdata.stu_tb where id=11