4-12总结

sql基础语法

sql(结构化查询语言) — 所有关系型数据库通用语言

– DDL(数据定义语言) - create、drop、alter
– DML(数据操作语言) - insert、delete、update
– DQL(数据库查询语言) - select
– DCL(数据控制语言) - grant、revoke

– 注意:sql中不区分大小写,一条sql语言结束必须加分号

– 1.DDL
– 1)创建数据库
create database if not exists school default charset utf8mb4;

– 2)删除数据库(灵魂拷问) drop database 数据库名称
drop database if exists school;

– 3)使用数据库 use 数据库
– 在做表的操作前先需要通过use来确定数据库
use school;

– 4)创建表 create table 表名(字段1 字段类型1 约束1 comment 说明1,字段2 字段类型2 约束2 comment 说明2,……);
– 注意:sql中字符串数据用单引号
– 创建表的时候必须添加主键约束,主键是能够表示表中唯一一条记录的字段

create table if not exists school.t_student(

stu_id int unsigned not null comment ‘学生学号’,

stu_name varchar(4) not null comment ‘姓名’,

stu_gender char(1) not null comment ‘性别’,

primary key(stu_id)

)engine=innodb;

– 5)删除表(灵魂拷问)

drop table if exists t_student;

– 6)修改表 添加字段、删除字段、修改字段名称和类型

a、删除字段

alter table t_student drop stu_birth;

b、增加字段

alter table t_student add str_addr varchar(50);

b、修改字段

alter table t_student change stu_gender stu_sex char(1);

– Mysql常用数据类型
– 数字:int (整型) decimal(小数)
– 字符串类型:char(N)定长、varchar(N)不定长
– 布尔:Boolean/tinyint
– 时间:date、datetime、timestamp

– 字段常用的约束
– 不为空:not null
– 设置默认值:default默认值
– 唯一约束:unique
– 自动增长:auto_increment

– 2、DML
– 1)插入数据

insert into t_student(stu_id,name,sti_sex,stu_addr) values
(102,‘张飞’,‘女’,‘四川成都’),
(105,‘关羽’,‘男’,‘四川凉山’),
(107,‘刘备’,‘男’,‘四川南充’);

– 2)删除数据
– sql的条件语句
– where 字段=值
– where 字段>值/字段<值/字段>=值/字段<=值
– where 字段 in(值1,值2,……)
– where 字段 between 值1 and 值2

delete from t_student where stu_id=101;

delete from t_student where stu_id >104;

delete from t_student where stu_id in (103,105); 删除学号是103,105的记录

– 3)修改数据

update t_student set name=‘小花’ where stu_id=107;

update t_student set name=‘小米’,stu_sex=‘女’ where stu_id=108;

– 3、DQL
– 查询整个表中所有的数据:select * from 表名;
– 查询指定列所有的数据:select 字段1,字段2…… from 表名;
– 查询满足条件的所有列:select 字段1,字段2,……from 表名 where 条件

select * from t_student;

select stu_id,stu_sex from t_student;

select stu_name,stu_sex from t_student where stu_id=108;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值