系统学习-----数据库的表操作

表操作 - DDL

创建数据库:create database test;
创建表:create table

常用格式:
create table table_name(
字段1 数据类型1,
字段2 数据类型2,
字段3 数字类型3,

) [table_options] [partition_options]

查看所有表:show tables;
查看表结构:desc table_name;
查看创建表的DDL语句:show create table table_name;
在这里插入图片描述


向表中插入记录:insert student_info values(11,‘zhangsan’,80);
在这里插入图片描述
在这里插入图片描述
修改表
– SELECT * FROM student_info
#1.修改表名:alter table table_name rename new_table_name;

alter table student_info rename new_stu_info;

#2.增加字段:alter table table_name add field_name datatype [约束条件…]

alter table new_stu_info add age int(2);

#3.修改字段:alter table table_name change|MODIFY old_field new_field new_datatype;

alter table new_stu_info change age garden char(4);

#4.指定字段位置: first/after field_name;

alter table new_stu_info add age int(2) first;
alter table new_stu_info change age age2 int(2) after stu_name;

#5.删除字段:alter table table_name drop field_name;

alter table new_stu_info drop garden;

#添加约束条件:主键(唯一性 + not null:不能为空)+自增ID
这里建立一张书表:

create table book(bid int(4),bname char(8),bpublish char(8));
alter table book change bid bid int(4) primary key auto_increment;

效果就是为书的bookID添加主键+自增,这样向表中插入书:
1 水浒传 出版社
之后插入书籍bid就会自增:
2 西游记 出版社

#删除约束条件:先去删除自增约束再去删除主键

alter table book change bid bid int(4);
alter table book drop PRIMARY KEY;

发布了49 篇原创文章 · 获赞 6 · 访问量 3686

猜你喜欢

转载自blog.csdn.net/weixin_46097280/article/details/105016945