常用的sql语句(通过命令提示符操作)

本文介绍了如何通过命令提示符操作数据库,包括创建表、操作字段及增删查改等基本操作。创建表示例中详细解释了各字段属性,如自增长、唯一、非空、默认值等。查询部分涵盖了基础查询、分页查询、条件查询及三表连接查询等。同时,还展示了插入、删除和更新记录的SQL语句。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.通过命令提示符操作数据库

 

 

 

 2.创建表

create table t_test(
    id int primary key auto_increment,
    name varchar(20) unique not null,
    sex varchar(20) check(sex in ('男','女')),
    salary decimal(10,2),
    birthday datetime,
    state int default 1
);

auto_increment代表的是自增长,插入数据时数据设为null即可

unique就是唯一,not null就是不能为空

check就是该字段的内容需要是后面规定的内容

 decimal中的10代表的是有效位,2代表的是小数位

default代表默认值为后面的数值

3.操作字段

删除字段:alter table t_test drop column state;

添加字段:alter table t_test add column state int;在后面加上default 1 时创建的该字段的数值为1

修改字段:alter table t_test change a a int;  修改字段的类型
                  alter table t_test change a b int;  修改字段名

4.增删查改

查:

select * from t_test;

select * from t_test where id = 1 and sex = "男";

select * from t_test where id = 1 and sex = "男" limit 0,3;  分页查询0代表页码,从0页开始,3代表每页的尺寸

select * from d_book order by salenum desc limit 3

降序查询前3条数据

select * from d_book order by salenum asc limit 2

升序查询前2条数据

select count(*) from d_book where cid = #{id}

三表连接查询:

select a.*,b.*,c.*

from a left join b 

on a.id = b.aid

join c

on b.id = c.bid

where c.id = #{id}

增:insert into t_test values(null,"ycc","男","15000","1999-11-11",1);

删:delete from t_test where id = 1;

改:update t_test set name = "ycc666" where id =1;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值