day07_mysql——mysql基础
2016-03-21 21:34
267 查看
展示当前有哪些数据库?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;【按性别来统计出,每个性别有多少人】
来自为知笔记(Wiz)
创建数据库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;【按性别来统计出,每个性别有多少人】
来自为知笔记(Wiz)
相关文章推荐
- MySQL系列:单表&多表数据查询
- mysql 插入重复值 INSERT ... ON DUPLICATE KEY UPDATE
- MySQL在windows系统中修改datadir路径后无法启动问题,报错1067
- mysql 函数大全
- MySQL事务隔离级别(脏读,不可重复读,幻象读)
- MySQL 入门 之 联接关键字
- MySQL批量SQL插入各种性能优化
- MySQL简单使用
- MySQL 性能调优的10个方法
- mysql 好文收藏
- Ubuntu下忘记MySQL密码重设方法
- MySQL修改root密码的4种方法
- 解决初次登录MySQL出现ERROR 1045 (28000):Access denied for user 'root'@'localhost'问题
- ERROR 2003 (HY000): Can't connect to MySQL server on '192.168.10.210' (111) 解决方法
- Navicat for Mysql注冊码
- MySQL 支持远程登陆设置
- mysql必会必知
- 在Window下安装解压版的mysql 5.7.11
- mysql中utf8_general_cs和utf8_general_ci和utf8_bin的一点区
- mysql中的字符串的拼接