广告
返回顶部
首页 > 资讯 > 数据库 >数据库常用基本命令——增删改查,排序
  • 340
分享到

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

2024-04-02 19:04:59 340人浏览 薄情痞子
摘要

数据库常用基本命令:show  databases; #查看数据库use + 数据库名称; #进入数据库show tables;     &nbs

数据库常用基本命令:

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;

您可能感兴趣的文档:

--结束END--

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

本文链接: https://www.lsjlt.com/news/45516.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作