2024-03-14
MySQL
00
请注意,本文编写于 115 天前,最后修改于 100 天前,其中某些信息可能已经过时。

目录

行数据的增删改查

行数据的增删改查

  • insert
  • delete
  • update
  • select

创建库

SQL
create database linux;

切换库

SQL
use linux;

创建表,每个 web 项目其实都会创建很多个表来储存不同的数据

SQL
create table 表名( 字段名1 类型[(宽度)约束条件], 字段名2 类型[(宽度)约束条件], 字段名3 类型[(宽度)约束条件] );

示例:

SQL
mysql>create table Rrx( -> id int, -> name varchar(50), -> age int(3), -> int(3) -> );

查看一下 MySQL 帮我们创建表的时候的详细指令

SQL
show create table Rrx;

创建库和创建表的时候还可以指定字符集编码,默认字符集是Latin

SQL
create table Rrx(id int, name varchar(50)) ENGINE=MyISAM DEFAULT CHARSET=utf8;

ENGINE=MyISAM 这是指定存储引擎,这个后面说

往表里插入数据

SQL
insert into Rrx(id,name,age) value(1,'xx',18); # 插入单条数据 insert into Rrx(id,name,age) values(2,'xx2',15),(3,'xx3',19); # 插入多条数据

创建只有 name 列的表 t1

SQL
create table t1(name char(6));

查看表结构

SQL
desc t1;

往表t1插入数据

SQL
insert t1 value('zhang'); insert t1 value('li');

查询 t1 表中所有数据

SQL
select * from t1;

指定字符集的创表语句

SQL
create table t2(name char(6),age int(3)) default charset=utf8;

往表t2插入数据

SQL
insert t2 value('张三',20); insert t2 value('李四',60);

创建表t4

SQL
create table t4(name char(6),age int(3) default 0 ) default charset=utf8;

指定列插入数据

SQL
insert t4(name) values('张三'),('李四');

查询结果

SQL
mysql> select * from t4; +--------+------+ | name | age | +--------+------+ | 张三 | 0 | | 李四 | 0 | +--------+------+ 2 rows in set (0.00 sec)

修改字段的长度

SQL
alter table t2 modify name char(10);

查看创表语句

SQL
show create table t2;

增加字段

SQL
alter table s2 add age int(3);

删除字段

SQL
alter table s2 drop age;

语法

SQL
alter table 表名 add 字段名 数据类型 [完整性约束条件…] first; # 添加这个字段的时候,把它放到第一个字段位置去。 alter table 表名 add 字段名 数据类型 [完整性约束条件…] after 字段名; # after 是放到后的这个字段的后面去了,我们通过一个 first 和一个 after 就可以将新添加的字段放到表的任意字段位置了。

修改表的字符集

SQL
alter table 表名 charset=utf8mb4;

使用 where 条件删除单条数据

SQL
delete from t5 where name='zhangsan';

删除所有数据

SQL
delete from t5;

单条件修改

SQL
update t5 set password='123' where name='wangwu';

单条件修改多列

SQL
update t5 set password='123',name='xxx' where name='wangwu';

多条件修改

SQL
update t5 set password='123' where name='wangwu' and id=1; update t5 set password='123' where name='wangwu' or id=1;

修改所有数据

SQL
update t5 set password='123456';
如果对你有用的话,可以打赏哦
打赏
ali pay
wechat pay

本文作者:@Rrx

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!