数据库常用基本命令——增删改查,排序
admin
2023-04-23 10:03:33
0

数据库常用基本命令:

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;

相关内容

热门资讯

“一次愉快的会晤”,泽连斯基披... 在俄乌互相加大对对方纵深目标打击之际,乌克兰总统泽连斯基7月28日造访了白宫,与美国总统特朗普举行会...
总统连遭“逼宫”,少帅临危受命 躲避着燃烧瓶和火箭弹袭击,四辆苏制BMP步兵战车快速穿行在马里乌波尔市区。即将出城的路口,迎面出现1...
独居女子在卧室玩偶内发现摄像头... 租房独居女子意外发现家中玩偶内藏着偷拍设备,报警后偷拍设备竟“不翼而飞”,门锁记录显示有人在她出门的...
伊学者:乌克兰袭击行动适得其反... 乌克兰袭击与伊朗有关的船只后,伊朗和俄罗斯接连作出强硬回应。伊朗外交学院教授法拉吉扎德在接受凤凰卫视...
20万人怒火不会停!蓝议员挺蒋... 海峡导报综合报道 在蓝白号召台湾民众上凯道呐喊“反毒台”后,台北市长蒋万安日前透露,近日不少民代私下...
柯文哲妻子陈佩琪畅游大陆,民进... 台湾民众党前主席柯文哲妻子陈佩琪27日在脸书发文证实,自己早前由儿子陪同前往上海、杭州与北京旅游一周...
高市早苗被问如何改善中日关系 据日本东京新闻报道,当地时间7月27日,日本首相高市早苗在首相官邸举行记者会。当被问及如何改善中日关...
扎哈罗娃批小泉进次郎涉核言论:... 【环球网报道 记者 张江平】综合俄新社等媒体报道,对于日本防卫大臣小泉进次郎涉核言论,俄罗斯外交部发...
“特高课”再现?日本强推“国家... 无视反对战争正义呼声 复刻战前军国主义体制日本强推“国家情报局”引发广泛担忧和批评(国际视点)本报记...
6G相比5G不只是速度更快 将...   6G相比5G不只是速度更快  【6G相比5G不只是速度更快】7月28日,相比5G,6G不仅是速度...