MySQL初步学习和语句

数据库操作

  • 查看所有数据库

show databases;
  • 查看当前使用的数据库

select database();
  • 使用数据库

use 数据库名;
  • 创建数据库

create database 数据库名 charset=utf8;
例:
create database python charset=utf8;
  • 删除数据库-慎重

drop database 数据库名;
例:
drop database python;

表操作

添加表

CREATE TABLE table_name(
  字段名称 数据类型  可选的约束条件,
  column1 datatype contrai,
  column2 datatype,
  column3 datatype,
  .....
  columnN datatype,
  -- 主键说明可以放在字段中单独说明 也可以放在最后统一说明
  PRIMARY KEY(one or more columns)
);
​
例如:
create table students(
    id int unsigned primary key auto_increment not null,
    name varchar(20) default '',
    age tinyint unsigned default 0,
    height decimal(5,2),
    gender enum('男','女','人妖','保密'),
    cls_id int unsigned default 0
)

 

修改表

  • 修改表-添加字段

alter table 表名 add 列名 类型;
​
例:
alter table students add birthday datetime;
  • 修改表-修改字段:重命名版

在表中已有字段 但是字段名不满足要求 类型或约束满足或者不满足均可。

alter table 表名 change 原名 新名 类型及约束;
例:
alter table students change birthday birth datetime not null;
  • 修改表-修改字段:不重命名版

在表中已有字段 并且字段名也满足要求 但是类型或约束不满足要求

alter table 表名 modify 列名 类型及约束;
例:
alter table students modify birth date not null;
  • 修改表-删除字段

当表中多出一个字段 已经不再需要的时候

alter table 表名 drop 列名;
例:
alter table students drop birthday;

删除表

drop table 表名;
例:
drop table students;

查看表

  • 查看创表语句

show create table 表名;
例:
show create table classes;
  • 查看表结构

desc 表名;
例:
desc classes;

数据操作

数据添加

-- 全插入
insert into 表名 values (...)
-- 例:
insert into students values(0,’郭靖‘,1,'蒙古','2016-1-2'); 
​
​
-- 指定字段插入
insert into 表名 (列1,...) values(值1,...)
-- 例:
insert into students(name,hometown,birthday) values('黄蓉','桃花岛','2016-3-2');
​
​
-- 插入多条数据
insert into 表名(列1,...) values(值1,...),(值1,...)...;
-- 例:
insert into students(name) values('杨康'),('杨过'),('小龙女');

 

数据修改

update 表名 set 列1=值1,列2=值2... where 条件
-- 例:
update students set gender=0,hometown='北京' where id=5;

 

数据删除

delete from 表名 where 条件
-- 例:
delete from students where id=5;

 

数据查询

/*
as                                              起别名
distinct                                    去重
where                                           条件查询
=,>,>=,<,<=,<>                      比较运算符
and,or,not                              逻辑运算符
like                                            模糊查询
in                                              范围查询
is null                                     空判断
order by                                    排序
limit                                           分页
group by                                    分组
count,max,min,sum,avg           聚合操作
*/
​
select name as 姓名, age as 年龄 where id in(2,4,6,8,10,12,14,16,18,20) order by age desc limte 2,2;
​
select gender,count(*) from students group by gender having count(*)>2;
​

一对多建表

create table goods(
    id int primary key auto_increment not null,
    name varchar(40) default '',
    price decimal(5,2),
    cate_id int unsigned,
    brand_id int unsigned,
    is_show bit default 1,
    is_saleoff bit default 0,
    foreign key(cate_id) references goods_cates(id),
    foreign key(brand_id) references goods_brands(id)
);
​

 

多对多建表

create table hero(
    id int primary key auto_increment not null,
    name varchar(40) default '',
    price decimal(5,2),
);
​
create table arms(
    id int primary key auto_increment not null,
    name varchar(40) default '',
    price decimal(5,2),
    number int 
);
​
create table hero_arms(
    id int primary key auto_increment not null,
    h_id int,
    a_id int,
    foreign key(h_id) references hero(id),
    foreign key(a_id) references arms(id)
  
);
​
​

 

自关联建表

create table areas(
    aid int primary key,
    atitle varchar(20),
    pid int
);

 

 

关联查询

-- 一对多关联查询
select * from students inner join classes on students.cls_id = classes.id;
​
-- 多对多关联查询
select * from hero h inner join hero_arms ha on h.id=ha.h_id inner join arms a on a.id=ha.a_id;
​
-- 自关联查询
select city.* from areas as city inner join areas as province on city.pid=province.aid
where province.atitle='山西省';

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值