# 查看当前有哪些数据库
show databases;
# 使用名为test的数据库
use test;
# 创建一张学生表
create table stu(
id int(10), name varchar(20),age int(10),
primary key(id));
# 每一张表都需要包含一个主键,主键唯一标识一条记录,唯一的字段,不可重复不能为空,通过`primary key`关键字来定义。
# 查看创建好的表
show create table stu;
# 新加一个字段
alter table stu add column gender varchar(20);
# 修改一个字段
alter table stu modify column gender varchar(40);
# 删除一个字段
alter table stu drop column gender;
# 删除表
drop table stu;
# 查看当前数据库中的表
show tables;
# 向表中插入数据
insert into stu(id,name,age) values(1,'pw',28);
# 插入全部字段时可以只写表名
insert into stu values(2,'nss',29);
# 查看刚才添加的数据,"*"代表查询全部字段
select * from stu;
# 如果只想查询两个字段,则只写要查询的字段名
select name, age from stu;
# 也可以根据某个条件进行查询,比如只查询id为1的记录
select name age from stu where id=1;
# 更新语句
update stu set age=29 where id=1;
# 删除表中的数据
delete from stu where id=1;
创建表,添加修改删除字段,插入修改删除数据
最新推荐文章于 2024-03-31 13:18:06 发布