数据库常用基本命令——增删改查,排序

发布时间:2020-08-05 22:33:10 作者:若恒
来源:网络 阅读:2252

数据库常用基本命令:

show  databases; #查看数据库

use + 数据库名称; #进入数据库

show tables;        #查看对应数据库中的表

select  * from info; #查看info表中的数据, * 代表所有数据

select 字段 from 表; #查看指定的数据,从表中查看


创建数据库

create database school ;    #创建school数据库


创建表:一定要进入到数据库中

举例:

create table info (id int not null primary key auto_increment,name char (10) not null,score decimal(5,2),hobby int (2)) ;

#创建一个名为info的数据表,表的属性(id 类型为int 不能为空null 为主键 自增列,name char(字符串长度为10)不为null空值,成绩 最大5位数字其中两位小数,hobby 类型为int(2字节));


PS详解

创建表(表的属性)

not null:不为空值

primary key:主键

auto_increment: 自增列


char:定长字符串类型

说明:M为最大可存储字节数 汉子占两个字节,通过指定m,来限制存储的最大字符数长度,char(20)和varchar(20)将最多只能存储20个字符,超过的字符将会被截掉。m必须小于该类型允许的最大字符数。


decimal(5,2):定点精度和小数位数。【最大5位数字,其中两位小数】

5是(有效位数:可储存的最大十进位数总数,小数点左右两侧都包括在内。有效位数必须是 1 至最大有效位数 38 之间的值。)

2是 (小数位数:小数点右侧所能储存的最大十进位数。小数位数必须是从 0 到 4 的值。只有在指定了有效位数时,才能指定小数位数。预设小数位数是 0;因此,0 <= 1 <= 4。最大储存体大小会随著有效位数而不同。)



常用基本命令

insert into info(id,name,score,hobby) values(1,'zhangsan',95,1);     #表后面插入数据

语法 :insert into 表名(列,列,列) values(填入的数据,‘填入的数据’, 填入的数据);


select * from info where id=2;                          #条件筛选,where代表的是条件

update info set score=75 where id=6;           #修改表中的信息 ,set代表的是列,where代表的是条件

delete from info where name=’test’;   #删除条件为name=‘test’的行

alter table test01 rename info01;             #修改表名

alter table info change name username varchar(10) unique key;     #修改信息名,并设置约束

insert into info(id,name,score) values(6,’test’,null);                #插入数据

insert into hob (sid,hobname) values (2,’聊天’),(3,’运动’),(4,’游戏’);      #连续添加

Select * from info where id=6;             #条件筛选

delete from info where name=’test’;          #删除条件为name=’test’的那一行的全部信息。

select * from info where 1=1 order by score(asc升序)(desc降序);                    #排序(升序/降序)

desc info;                   #查看表结构

select info.name,info.score,hob.hobname from info inner join hob where info.hobby=hob.id;

select username from info where hobby=(select sid from hob where hobname='游泳');         #多表查询

select a.name,a.score,b.hobname from info a inner join hob b where a.hobby=b.id;         #别名查询

create table infos select a.name,a.score,b.hobname from info a inner join hob b where a.hobby=b.id;           #新建表infos内容为多表查询结果

drop database infos;                  #删除infos表


排序:

升序 order by asc

select * from info where 1=1 order by score asc;  # where 1=1 order by  score 默认是asc升序。

降序order by desc

select * from info where 1=1 order by score desc;


===========查询============

desc info ;  查看info表结构

select  #查询


多表相连查询

select * from info inner join hob where info.hobby=hob.id;

#表示查询info表和hob表,inner join是固定语法。


#多表查询,查看info的name列和红包列

select info.name,info.score,hob.hobname from info inner join hob where info.hobby


#多表查询结果创建为新的表格 名为infos

create tables infos select info.name,info.score,hob.hobname from info inner join hob where info.hobby


#创建别名查询

select a.name,a.score,b.hobname from info a inner join hob b where a.hobby=b.id;

推荐阅读:
  1. mysql创建数据库的基本命令
  2. 常用增删改查sql示例

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mysql insert select

上一篇:mysql数据库改名的三种方法

下一篇:list列表数据结构清空

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》