sql---数据库基本操作,增删改查(学习笔记)

命令行启动mysql

mysql -uroot -p123456  --连接数据库(用户名root 密码123456)

创建数据库

  -- mysql关键字不区分大小写,表名或者字段名可以用``,但是values内容必须是''
CREATE DATABASE IF NOT EXISTS westos;

删除数据库westos

DROP DATABASE IF EXISTS westos;

查看数据库

SHOW DATABASES;

使用数据库

-- 数据库名或者字段名是特殊字符,必须用``包起来
USE `school`; 
-- 我使用的数据库管理工具是navicat premium,执行该语句好像不生效,需要手动选择数据库,具体原因我也不清楚

创建表

create table `student`(
    `id` int(4) not null comment '学号',
    `name` varchar(20) not null default '匿名' comment '姓名',
    `pwd` varchar(20) not null default '123456' comment '密码',
    `sex` varchar(2) not null default '女' comment '性别',
    `birthady` datetime default null comment '生日',
    `address` varchar(20) default null comment '地址',
    primary key(`id`) 
)
-- `id`是tab上面那个`,comment '学号'是单引号

查看创建语句

show create database school  -- 查看创建数据库school语句
show create table student -- 查看创建表student语句
desc student  -- 显示表结构

修改和删除表

修改

alter table student rename as stu  -- 修改表名
alter table stu add age int(3)  -- 增加字段
alter table stu modify age varchar(12) -- 修改字段约束
alter table stu change age age1 int(1)  -- 重命名

删除

alter table stu drop age1  -- 删除字段
drop table if exists stu  -- 删除表

插入

insert into `stu` (`id`,`name`,`sex`,`pwd`) values ('3','张三','男','123456') -- 插入数据
insert into `stu` (`name`) values ('张三'),('李四')    -- 插入多条数据

修改数据

update `stu` set `name`='mike' where `name`='张三'  -- 修改表stu里面name='张三'的数据

删除数据

delete from `表名` where [条件]

truncate 完全清空一个数据库表,表的约束和索引不会变

truncate table `表名`
  • delete和truncate的区别

    • 相同点:都能删除表的数据

    • 不同点:truncate会重新自增列和计数器归零

    • truncate不会影响事物

查询

查询

查询带有一个或者多个条件时,返回的结果是布尔值

select `字段` from `表名`

别名,函数concat(a,b)

select `字段` as 学号 from `表名`
select CONCAT('姓名:',name) as 新名字 from `stu`

去重distinct

select distinct `id` from `stu`
运算符语法描述
and &&a and b a && b逻辑与
or ||a or b a || b逻辑或
not !not a ! a逻辑非
select * from `stu` where `id`=1 && `name`='mike'
-- 模糊查询
select * from `stu` where id between 1 and 10

模糊查询

运算符语法描述
is nulla is null如果操作符为null,结果为真
is not nulla is not null如果操作符为不为null,结果为真
betweena between a and b若在a和b之间结果为真
likea like bsql匹配,如果a匹配b,结果为真
ina in (a1,a2...)假设a在a1或者在a2...中的某个值,结果为真
-- like查询姓刘的同学
-- like结合,%(表示0到任意字符),_(表示一个字符)
select name from `stu` where `name` like '刘%'
​
-- like查询姓刘的同学,后面只带一个字
select name from `stu` where `name` like '刘_'
​
-- like查询姓刘的同学,后面只带两个字
select name from `stu` where `name` like '刘__'
​
-- like查询名字中间带'嘉'的同学,‘%嘉%’
select name from `stu` where `name` like '%嘉%'
​
-- in(具体的一个或者多个值)
-- 查询学号为1001,1002,1003的同学
select `name`,`id` from `stu` where `id` in (1001,1002,1003)
​
-- 查询在广州的同学
select `name`,`id` from `stu` where `address` in ('广州')
​
--查询地址为null的同学
select `name`,`id` from `stu` where `address` is null or `address`=''

联表查询

join on 连接查询,字段加了``,就必须要用join on

inner join两表的共同部分,left join左表,right join 右表

where 等值查询

-- inner join
select a.stunum,stuname 
from stu as a 
inner join sturesult as b
on a.stunum=b.stunum
-- left join 
select a.stunum,stuname 
from stu as a 
left join sturesult as b
on a.stunum=b.stunum
-- 联合多表查询
select a.stunum,stuname,stuid 
from stu as a 
left join sturesult as b
on a.stunum=b.stunum
inner join perstu as c
on b.stuid=c.stuid

自连接

sql里面有一个表

categoyardIdpidcategoyardName
31软件开发
51美术设计
43数据库
82办公信息
21信息技术
63web开发
75ps技术

父表

categoyardidcategoyardName
3软件开发
5美术设计
2信息技术

子表

pidcategoyardName
3数据库
2办公信息
3web开发
5ps技术

查询父类对应子类关系

父类子类
软件开发数据库、web开发
美术设计ps技术
信息技术办公信息
select a.categoyardName,b.categoyardName 
from categoyard as a,categoyard as b
where a.`categoyard`=b.`pid`
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值