day07_mysql——mysql基础

展示当前有哪些数据库?
show databases;

创建数据库
create database 库的名称;

create database hehe;
create database `hihi`;

删除库
drop database 库名称;

使用指定数据库(说明以后的操作全在此库下)
use 库名;







展示当前库中有哪些表
show tables;

创建表

create table 表名(字段1名称 类型,字段2名称 类型.......字段n名称 类型)

create table renyuanxinxi(
name varchar(100),
age int,
phone varchar(1000),
sex   int
);

删除表
drop table 表名;









表中插入数据
insert into 表名(字段1名称,字段2名称.......字段n名称) values(值1,值2......值n);

insert into renyuanxinxi(name,age,phone,sex) values('nimei',1,'123456',1);【标准插入数据,全部字段】
=insert into renyuanxinxi values('nimei3',3,'123456',3);

insert into renyuanxinxi(name,age) values('nimei2',2);【标准插入数据,部分字段】


查看表中数据
select * from 表名;【查询所部字段】
select * from renyuanxinxi;

select 字段1名称,字段2名称.......字段n名称 from 表名;【查询表中部分字段】
select name,age from renyuanxinxi;

select * from 表名 where 条件1 and 条件2......条件n;
select * from renyuanxinxi where name='nimei';
select * from renyuanxinxi where name='nimei' and age=2;

select 字段1名称,字段2名称.......字段n名称 from 表名 where 条件1 and 条件2......条件n;


select * from 表名 where 条件1 or 条件2......条件n;
select * from renyuanxinxi where name='nimei' or age=2;

select 字段1名称,字段2名称.......字段n名称 from 表名 where 条件1 or 条件2......条件n;

select count(*) from 表名; 【查询表中的记录数】
select count(*) from chusheng2;

查看表结构
desc 表名;

克隆表
create table new as select * from old; 【两个表的数据和字段完全一样】
create table chusheng as select * from renyuanxinxi;
create table chusheng3 as select * from chusheng2;

create table new as select 字段1名称,字段2名称.......字段n名称 from old; 【从Old表中取部分字段的全部数据】
create table chusheng as select name,age from renyuanxinxi;

create table new as select 字段1名称,字段2名称.......字段n名称 from old where 条件1 or 条件2......条件n;【从Old表中取部分字段的部分数据】
create table new as select 字段1名称,字段2名称.......字段n名称 from old where 条件1 and 条件2......条件n;

create table new as select * from old where 10=200;【复制old表全部字段结构】
create table chusheng2 as select * from renyuanxinxi where 10=200;

create table new as select 字段1名称,字段2名称.......字段n名称 from old where 10=200;【复制old表部分字段结构】


高级插入
insert into new select * from old; 【把old表中全部数据insert到new表中】
insert into chusheng2 select * from renyuanxinxi;

insert into new select * from old where 条件1 or 条件2......条件n; 【把old表中部分数据insert到new表中】
insert into new select * from old where 条件1 amd 条件2......条件n; 【把old表中部分数据insert到new表中】

insert into new values(),(),(),()......();  【一次insert多行记录】
insert into renyuanxinxi values ('nimei',1,'123456',1),('nimei',1,'123456',1),('nimei',1,'123456',1);


insert into new select * from new; 【自己插入自己,数据翻倍增加】

删除表中数据
delete from 表名; 【删除表中全部数据】<11.29秒>
=truncate table 表名;  <0.13秒>

delete from chusheng;

delete from 表名 where 条件1 or 条件2......条件n; 【删除old表中部分数据】
delete from 表名 where 条件1 and 条件2......条件n; 【删除old表中部分数据】

delete from chusheng2 where name='nimei2' and age=20;


更新表中的数据
update 表名 set 字段1=值1,字段2=值2.....字段n=值n; 【更新表中全部记录】

update renyuanxinxi set name='xiaosan';
update renyuanxinxi set name='xiaosi',age=10;

update 表名 set 字段1=值1,字段2=值2.....字段n=值n where 条件1 or 条件2......条件n; 【更新表中部分记录】
update 表名 set 字段1=值1,字段2=值2.....字段n=值n where 条件1 and 条件2......条件n; 【更新表中部分记录】

update renyuanxinxi set name='xiaosan' where sex=3;
update renyuanxinxi set name='xiaosan' where sex=300;

排序
select * from renyuanxinxi order by age;【默认是升序 asc】
select * from renyuanxinxi order by age asc;

select * from renyuanxinxi order by age desc;【降序 desc】


select * from renyuanxinxi order by phone asc,sex desc;【按手机号排序,如果手机号相同再按性别降序】
select * from renyuanxinxi order by phone asc,sex asc;【按手机号排序,如果手机号相同再按性别升序】

聚集函数
max()  取最大值
min() 取最小值
sum()  取计算值的相加和
count()取记录有多少行
avg()  取平均值

mysql> select max(age) from renyuanxinxi;
mysql> select min(age) from renyuanxinxi;
mysql> select avg(age) from renyuanxinxi;
mysql> select sum(age) from renyuanxinxi;
mysql> select count(*) from renyuanxinxi;

分组【按什么什么来划分】

select sex,count(sex) from renyuanxinxi group by sex;【按性别来统计出,每个性别有多少人】




原文地址:https://www.cnblogs.com/xiaoxiao5ya/p/688435baa0d316afe1e49901cd628ef1.html