菜鸡学习日记-大一年度项目-数据库学习-1.25-1.26-对表进行操作吧!

今天首先解决了昨天(1.24)的问题,发现是我的资料书上有错误,可能是版本太老(?)。资料书为5.6版本,我使用的为MySQL 8.0。

(最近的内容写博客很费时间,还有假期的人情事,时间不够啊)

上图是我昨天写的创建表,发现格式错误

1、create table 表名(!不要带单引号)

2、对属性如 sno创建时 应该为: sno varchar(10) not null comment '学号',(sno不要带单引号!)

今天学了对表进行的操作

7.4 表的操作 (注以下代码中‘['']’表示可写可不写的部分)

7.4.1 创建表

create [temporary] table [if not exists] table_name

[([column_definition],|[index_definition])]

[table_option][select_statement] ;

 说明:

1)temporary:表示使用该关键字创建临时表。

2)if not exists:如果数据库中已经存在某个表,再来创建一个同名的表,这时会出现错误,为了避免错误信息,可以在创建表的前面加上这个判断,只有该表目前不存在时才执行create table操作。

3)table_name:  要创建的表名。

4)column_definition:字段的定义。包括指定字段名、数据类型、是否允许空值,指定默认值、主键约束、唯一性约束、注释字段名、是否为外键、以及字段类型的属性等

col_name type [not null | null ] [default default_value]

[auto_increament][unique[key]]|[primary]key]

[comment 'string'] [reference_definition]

其中:

col_name:字段名。

type: 声明字段的数据类型。

not null或者 null:表示字段是否可以为空值。

default:指定的字段的默认值。

auto_increment:设置自增属性,只有整型类型才能设置此属性。

rimary key:对字段指定主键约束。

unique key:对字段指定唯一性约束。

reference_definition:指定字段外键约束。

5)index_definition:为表的相关字段指定索引。

(具体的接下来的学习会逐步了解)

例:

(要先use database_name,如上图我在studentinfo数据库下创建student2表)

engine= 表示选择的引擎,default charset=选择的字符集类型.

如果希望压缩索引中的关键字,使索引关键字占用更少的存储空间,可以通过设置pack_keys选项实现(仅对MyISAM存储引擎有效)(这部分书没有具体讲,可能以后会了解吧)。

pack_key=压缩类型。

7.4.2 查看表

1.显示表达名称:

显示指定数据库中存放的所有表的名称

show tables;

2.查询表的结构:

describe 表名;  /des 表名; / show create table 表名;

7.4.3 修改表

alter table 用于更改原有结构,可以增加或删除列,重命名列或表,修改字符集。

alter [ignore] table table_name

     alter_specification [, alter_specification]

add [column] column_definition[first|after col_name]                    //添加字段

| alter [column] col_name  [set default literal | drop default ]         //修改字段

| change [column] old_colunm_name colunm_definition [first | after col_name]  //重命名字段

| modify [colunm] column_definition [first | after col_name]           //修改字段

| drop [column] col_name                                                              //删除列

| rename [to] new_table_name                                                      //对表重命名

|  order by col_name                                                                     // 对字段排序

|  convert to character set character_name [collate collation_name]   //  将字符集转换为二进制

| [default] character set charset_name [collate collation_name]          //修改字符集

例:在student2表出生日期后面添加一个数据类型为char 长度为20的字符段,允许为空,表示学生所在学院

删除字段

7.4.4 复制表

create [temporary] table [if not exists] table_name

[( ) like old_table_name [ ] ]

| [ as (select_statement)] ;

具体的解释以后会补充

创建stu3表,并且把student2表的结构和索引复制过去

复制stu3表中的学号、姓名到新的表stuNameTable

7.4.5 删除表

drop table [if exists] table_name [, table_name].....

删除stu3和stuNameTable表

drop table if exists stu3,stuNameTable;

7.4.6表管理的注意事项

(待补充)

7.5 字段的约束

1.主键(primary)约束

设置主键的时候可以在创建表的时候指定,也可以对表已有的主键进行修改或者增加新的主键。

设置主键通常有两种方式:表级完整性和列级完整性约束。

表级完整性约束

primary key(字段名)

在创建表的时候设置

列级完整性约束,直接在该字段的数据类型或者其他约束条件后加上“primary key” 即可

字段名  数据类型 [其他约束条件] primary key

2)复合主键

在定义完所有字段完后

primary key(字段名1,字段名2)

修改主键

alter table sc1 drop primary key, add primary key(sno,cno);

2.外键约束

表A的外键字段的取值要么为null或者来自于表B主键字段的取值(表A称子表,表B称为表A的父表)

(1)如果子表的某一天记录参照了父表的某条记录,那么父表这一条记录的删除或修改可能以失败告终。

(2)如果试图直接插入或者修改子表的“外键值”,子表中的外键值必须为父表的“主键值(取值域)”,要么为null,否则操作失败。

设置表A的外键两种方式,表级完整性,列级完整性下定义外键约束

表级完整性

foreign key(表A的字段名) reference 表B的(字段名列表)

[on delete {cascade restrict | set null | no action} ]

[on update { cascade restrict | set null | no action } ]

级联选项四种取值意义:

1)cascade: 父表记录的删除或者修改操作会自动删除或修改子表中与之对应的记录

2)set null: 父表记录的删除修改操作会将子表中与之对应的记录外键指自动设为null

3)no action: 父表记录的删除修改操作,如果子表存在与之对应的记录,那么操作将失败

4)restrict: 与no action功能相同,且为级联选项的默认值

如果表已创建好,可以使用alter语句

alter table table_name

      add[ constraint 外键名 ] foreign key [id] (index_col_name,.....)

reference table_name(index_col_name,...........)

[on delete { cascade | restrict | set null | no action}]
[on update {cascade | restrict | set null | no action}

]

创建sc1表时指定外键sno

create table sc1(

        sno char(10) not null,

        cno char(10) not null,

        grade int not null default 0,

        primary key (sno,cno),

        foreign key (sno) references student(sno)

)engine = InnoDB default charset=utf8  collate = utf8_bin;

 列级完整性约束,直接再列的后面添加references命令

create table sc1(

        sno char(10) not null references student(sno),

        cno char(10) not null,

        grade int not null default 0,

        primary key (sno,cno)

)engine = InnoDB default charset=utf8  collate = utf8_bin;

完整性约束名字句式

constraint <完整性约束条件名> [primary key  短语 | foreign key 短语 | check 短语]

例:创建sc3表,将sno字段设置为外键

create table sc(

           sno char(10) not null,

           cno char(10) not null,

           grade int not null default 0,

           constraint sc_student_fk foreign key(sno) references student2(sno)

)engine = MyISAM default charset=utf8 collate=utf8_bin;

注InnoDB支持外键约束,MyISAM暂时不支持外键约束

3.非空约束 not null

4唯一性约束 unique

create table classes(

        class_name char(20) not null unique

)engine=MyISAM default charset=utf8 collate=utf8_bin;

对已存在的表的操作

alter table classes modify class_name char(20) not null unique

5.默认约束

up_limit 约束上限

create tabe course(

   up_limit int default 60

)engine=MyISAM default =utf8 collate=utf8_bin;

 6.自增约束

create table table_name(

属性名 数据类型 auto_increment,

.....

);

7.删除约束

alter table sc drop foreign key sc_student_fk;

猜你喜欢

转载自blog.csdn.net/qq_42954464/article/details/86652223