mysql之数据类型与表操作

关系数据库的注释
-- 单行注释
/* 多行注释 */

数据类型
数值
整数 只能保存整数,如果是小数,则自动四舍五入取整。
mediumint
smallint
tinyint(6) zerofill
int(8)
bigint

decimal(8,0)


小数实型
float(p,s)
numeric(p,s)
decimal(p,s) decimal
double(p,s)

字符串
char() 固定长度字符串 手机号 学号 身份证号
sno char(12)
insert into 1 6
varchar() 可变长度字符串 家庭地址
varchar(12)


text
longtext

日期 时间类型
日期 date 2020-06-28
时间 time 16:48:50

date
time
datetime
timestamp

日期时间 datetime 2020-06-28 16:48:50

复合数据类型
enum 枚举 enum('计算机科学','英语','会计')
enum('Y','N')
set 集合
set('java','php','python','javascript','mysql')

'java' true
'php,java,mysql' true
'abc,java,ok' false

扫描二维码关注公众号,回复: 11392531 查看本文章

mysql 5.7新的json 类型 NoSQL类型

二进制数据类型
如果将一张图片文件,保存到数据库,必须使用二进制类型。
longblob


表操作 建立表
create table if not exists 表名(
字段名 数据类型 unsigned zerofill auto_increment 相关的约束,

)引擎 字符集 自增;

删除表
drop if exists table 表名,表2,表3;

表的约事,字段类型的修饰
unsigned zerofill auto_increment 自增一般和主键
not null 不能为空
默认值
default 18
default '郑州'
default '男'

主要约束
PK FK UK 默认值 default 非null not null
PK primary key 主键 唯一的,必须填写,不能为null 空 一表只能有一个主键
FK foreign key 外键一般两个表以表
uk unique 唯一约束

sno char(5) primary key

id int unsigned auto_increment,
primary key(id)

primary key(id,name) 复合主键,一个主键控制两列或多列


foreign key 外键操作
select host,user,plugin from mysql.user;
-- 建立老师信息表
create table teacher(
tid int unsigned auto_increment,
tname varchar(15) not null,
primary key(tid)
)engine=innodb charset=utf8 auto_increment=1;

-- 插入数据
insert into teacher(tname) values('李老师'),('张老师'),('周老师');
insert teacher(tname) values('李老师');


select * from teacher;

/* 建立学生信息表 */
create table student(
sid int unsigned auto_increment,
sname varchar(15) not null,
stid int unsigned,
primary key(sid)
/*foreign key(stid) references teacher(tid)*/
/* constraint fk foreign key(stid) references teacher(tid) on delete cascade*/
)engine=innodb charset=utf8 auto_increment=1;

-- 给student表增加建立外键
alter table student add foreign key(stid) references teacher(tid);

-- alter table student add foreign key(stid) references teacher(tid) on delete set null on update cascade;

drop table student;

select * from teacher;

select * from student;

insert into student values(null,'李四',null),(null,'jack',2),(null,'张三',5),(null,'李强',5);

select * from student;
select * from teacher;

delete from teacher where tid = 3;

select 语句查询
select * from student;
select * from xxx;


select 条件
= > < >= <= != <>

-- 随机查询两条记录 (此语句对于大型数据不合理)
select name,score,age from stu order by rand() limit 2;

--
select * from mysql.user;

select 10*10,10/5,10+5;
-- = > >= < <= != <>
select * from stu where name = '李四';
select * from stu where age != 20;

update stu set address = null where id = 5;

-- is null is not null
select * from stu where address is null;
select * from stu where address is not null;

-- in not in
select * from stu where dept in ('英语');
select id,name from stu where id not in (1,2,4,6);

-- between and not between and
select id,name,age from stu where age not between 18 and 20;
select id,name,age from stu where age <18 or age >20;


select id,name,age from stu where age between 18 and 20;
select id,name,age from stu where age >=18 and age <=20;

-- and &&
-- or ||
-- not 相当于java ! 非

select * from stu where sex = '女';

-- 模糊查询 like % 代表0个或多个任意字符 _ 一个任意字符

select * from stu where name like '张%';

select * from stu where name like '_三_';

select * from stu where name like '%丰%';

select * from stu where name like '李%';

-- 正则表达式条件
select * from stu where name regexp '^..$';

查询排序 order asc desc;
select * from stu;

查询聚合 聚集 集合函数
-- 查询班级的总人数

select count(*) from stu;

-- 查询多少个专业
-- 查询统计专业的个数 count(
select count(distinct dept) from stu;
select distinct * from stu;

-- 查询分组统计 group by order by where limit
group by 要集合函数配置

select max(score) from stu where sex = '男';

select max(score) from stu where sex = '女';


UML

E-R

猜你喜欢

转载自www.cnblogs.com/jinjinqiao/p/13209827.html