MySQL数据库-对数据表的基本操作

本文详细介绍了MySQL数据库中数据表的操作,包括如何创建数据表、查看表结构、修改表(添加、修改、删除字段)、删除表以及重命名表。提供了具体的SQL语句示例,如创建students和class表,以及对字段的增删改查操作。

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

数据表的操作

创建数据表

查看当前数据库中所有表
  • show tables;
创建表

create table 数据表名字(字段 类型 约束[,字段 类型 约束])

  • create table xxxx(id int, name varchar(30));
  • create table yyyy(id int primary key not null auto_increment, name varchar(30));
create table zzzz(
        id int primary key not null auto_increment,
        name varchar(30)     
);

注:

  • auto_increment表示自动增长
  • not null 表示不能为空
  • primary key可以表示主键
  • default 默认值
创建students表(id、name、age、high、gender、cls_id)
create table students(
        id int unsigned not null auto_increment primary key,
        name varchar(30),
        age tinyint unsigned,
        high decimal(5,2),
        gender enum("男","女","中性","保密") default "保密",
        cls_id int unsigned
);

insert into students values (0,"张三",18,188.88,"男",0)
select * from students;
创建class表(id、name)
create table students(
        id int unsigned not null auto_increment primary key,
        name varchar(30),
);

insert into class values (0,"python12")
select * from students;
查看表结构

desc 数据表的名字;

  • desc xxxx;
查看表的创建语句

show create table 表名字;

  • show create table students;

表的增删改查

修改表-添加字段

alter table 表名 add 列名 类型;

  • alter table students add birthday datetime;
  • alter table students add birthday datetime [after 字段名]; 字段名后添加
  • alter table students add birthday datetime [first]; 在第一个位置添加
    注:[]表示可有可无
修改表-修改字段:不重命名版

alter table 表名 modify 列名 类型及约束;

  • alter table students modify birthday date;
修改表-修改字段:重命名版

alter table 表名 change 原名 新名 类型及约束;

  • alter table students change birthday birth date default “2000-01-01”;
修改表-修改字段-删除字段

alter table 表名 drop 列名;

  • alter table students drop high";
删除表

drop table 表名;
drop table xxxxx;
注:drop database 数据库;

  • drop table 数据表;
重命名表名

rename table 旧表名 to 新表明

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值