• ADADADADAD

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

    mysql数据库 时间:2024-12-24 19:11:56

    作者:文/会员上传

    简介:

    数据库常用基本命令:show databases; #查看数据库use + 数据库名称; #进入数据库show tables; #查看对应数据库中的表select * from info; #查看info表中的数据, * 代表所有数据se

    以下为本文的正文内容,内容仅供参考!本站为公益性网站,复制本文以及下载DOC文档全部免费。

    数据库常用基本命令:

    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;

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

    将本文的Word文档下载到电脑

    推荐度:

    下载
    热门标签: mysqlinsertselect