数据库的基本利用(创建表、查看表、修改表、删除表)
一、创建表1、创建表的格式
create table 表名(
字段名1 数据类型 约束条件,
字段名2 数据类型 约束条件,
字段名3 数据类型 约束条件,
......
约束条件
);
2、束缚条件
(1) 设置主键束缚
主键又叫主码,用于唯一标识记载的字段。
[*]一张表只能有一个主键,并且主键不为空
[*]关键字:primary key
格式一:
字段名 数据类型 primary key
格式二:
primary key(字段名)
(2)设置自增束缚
如果用户渴望某个字段能够按照次序主动天生编号,可以为该字段设置自增束缚。
[*]一张表只能有一个字段为自增束缚,并且该字段只能是主键。
[*]默认初始值是1,每增长一条记载,字段值主动增1。
[*]字段数据类型必须是整数类型
[*]关键字:auto_increment
格式:
字段名 数据类型 auto_increment
(3)设置非空束缚
[*]用户在向数据表中插入数据时,如果设置非空束缚的字段没有指定值,系统就会报错。
[*]作用是规定字段的值不能为空
[*]关键字:not null
格式:
字段名 数据类型 not null
(4)设置唯一性束缚
当数据表中某个字段的值不允许重复时,可以使用唯一性束缚。
[*]关键字:unique
格式一:
字段名 数据类型 unique
格式二:
unique key(字段名)
(5)设置无符号束缚
[*]作用:规定该字段所存储的数据不为负数
[*]关键字:unsigned
格式:
字段名 数据类型 unsigned
(6)设置默认束缚
[*]没有设置默认束缚的字段,系统会自读设置默认值为null
[*]关键字:default
格式:
字段名 数据类型 default 值
(7)设置外键束缚
[*]关键字:constraint foreign key references
格式:
constraint 约束名 foreign key(字段名) references 主表名(主表中的字段名)
(8)设置表的存储引擎
格式:
engine=存储引擎名
3、实训案例
(1)创建goods表
字段数据类型束缚条件idint(11)主键、自增typevarchar(30)非空namevaechar(30)唯一pricedecimal(7,2)无符号numint(11)默认值为0add_timedatetime SQL语句:
格式一:
create table goods(
id int(11) primary key auto_increment,
type varchar(30) not null,
name varchar(30) unique,
price decimal(7,2) unsigned,
num int(11) default 0,
add_time datetime
);
格式二:
create table goods(
id int(11) auto_increment,
type varchar(30) not null,
name varchar(30),
price decimal(7,2) unsigned,
num int(11) default 0,
add_time datetime
primary key(id),
unique key(name)
);
(2)创建orders表
字段数据类型束缚o_idint(11)主键add_timedatetimegoods_idint(11)外键 SQL语句:
create table orders(
o_id int(11) primary key,
add_time datetime,
goods_id int(11),
constraint goo_ord foreign key(goods_id) references goods(id)
);
(3)创建category表
字段数据类型束缚idint(11)主键namevarchar(30)p_idint(11) SQL语句:
create table category(
id int(11) primary key,
name varchar(30),
p_id int(11)
);
(4)创建comment表
字段数据类型束缚idint(11)主键goods_idint(11)非空、无符号user_idint(11)非空、无符号contenttext默认add_timedatetime create table comment(
id int(11) primary key,
goods_id int(11) unsigned not null,
user_id int(11) unsigned not null,
content text,
add_time datetime
);
(5)创建reply表
字段数据类型束缚idint(11)主键、自增commment_idint(11)非空、无符号user_idint(11)非空、无符号r_contenttextadd_timedatetime SQL语句:
create table reply(
id int(11) primary key auto_increment,
comment_id int(11) unsigned not null,
user_id int(11) unsigned not null,
r_content text,
add_time datetime
);
二、查看表
1、查看表的结构
describe可简写成desc
格式:
describe 表名;
https://img-blog.csdnimg.cn/direct/a6ccf65eead94a1db04e795d53be26ce.png
2、查看建表语句
格式:
show create table 表名;
https://img-blog.csdnimg.cn/direct/d209a9deca99482ca7ab70f941e1e3d9.png
三、修改表
修改表的结构—alter
1、修改表名
格式:
alter table 旧表名 rename 新表名;
实训案例:修改goods为tb_goods
alter table goods rename tb_goods;
https://img-blog.csdnimg.cn/direct/e9440fc15d8a49d88125fe4929027036.png#pic_center
2、修改字段数据类型
格式:
alter table 表名 modify 字段名 新数据类型;
实训案例:将tb_goods表中的type字段的数据类型修改为char(30)
alter table tb_goods modify type char(30);
https://img-blog.csdnimg.cn/direct/f2f31865a77c4c8f867ca527792ef23f.png#pic_center
3、修改字段名
格式:
alter table 表名 change 旧字段名 新字段名 数据类型;
实训案例:将tb_goods表中的name字段的数据类型修改为g_name
alter table tb_goods change name g_name varchar(30);
https://img-blog.csdnimg.cn/direct/fd899737650b4e00a7efa3ac565c3aa4.png#pic_center
4、添加字段
(1)在表的末了一列添加字段
格式:
alter table 表名 add 字段名 数据类型;
实训案例:在tb_goods表末了一列添加picture字段,数据类型为varchar(255)
alter table tb_goods add picture varchar(255);
https://img-blog.csdnimg.cn/direct/28d77557403f4732bbbf3731e4ff9c33.png#pic_center
(2)在表的第一列添加字段
alter table 表名 add 字段名 数据类型 first;
实训案例:在tb_goods表中第一列添加state字段,数据类型为tinyint(4)
alter table tb_goods add state tinyint(4);
https://img-blog.csdnimg.cn/direct/89821f239d544ebeb60c5a8842c20bd9.png
(3)在表的指定列之后添加字段
alter table 表名 add 字段名 数据类型 after 字段名2;
实训案例:在tb_goods表中num字段之后添加intro字段,数据类型为text
alter table tb_goods add intro text after num;
https://img-blog.csdnimg.cn/direct/b4d9f5d10cf44db3bad337e2220adfc7.png
5、删除字段
格式:
alter table 表名 drop 字段名;
实训案例:删除tb_goods表中的picture字段
alter table tb_goods drop picture;
https://img-blog.csdnimg.cn/direct/b68efc3fc0f34a4299fa9dfb7003a783.png#pic_center
6、修改字段次序
格式:
alter table 表名 modify 字段1名 数据类型 first|after 字段2名;
实训案例:将tb_goods表的state字段位置修改为id字段之后
alter table tb_goods modify state tinyint(4) after id;
https://img-blog.csdnimg.cn/direct/1566ad4845d14fcc8246b39af931130b.png#pic_center
7、修改存储引擎
格式:
alter table 表名 engine=新存储引擎名;
实训案例:修改category表的存储引擎为InnoDB
alter table category engine=InnoDB;
https://img-blog.csdnimg.cn/direct/c235248fd01a48ee9c5928c0997b3d88.png#pic_center
8、修改束缚条件
(1)主键束缚
格式:
添加:alter table 表名 add primary key(字段名)
;删除:alter table 表名 drop primary key; 实训案例:删除tb_goods表的主键束缚,再添加。
https://img-blog.csdnimg.cn/direct/5ccd4ffd78c444739bf0b611ca9b200e.png#pic_center
(2)非空束缚
格式:
添加:alter table 表名 modify 字段名 数据类型 not null
;删除:alter table 表名 modify 字段名 数据类型 null; 实训案例:删除tb_goods表的非空束缚,然后再添加。
https://img-blog.csdnimg.cn/direct/5951754b4e1e45d78c988bf6e30d2ff3.png#pic_center
(3)唯一束缚
格式:
添加:alter table 表名 add unique key(字段名)
;删除:alter table 表名 drop key 字段名; 实训案例:删除tb_goods表的唯一束缚,再添加。
https://img-blog.csdnimg.cn/direct/6aa87614e1a94a1d9e9227028c3e45fe.png#pic_center
https://img-blog.csdnimg.cn/direct/e875e992ca61429a81bbfe7e1c82d5fe.png#pic_center
(4)自增束缚
添加:alter table 表名 modify 字段名 数据类型 auto_increment
;删除:alter table 表名 modify 字段名 数据类型; 实训案例:删除tb_goods表的自增束缚,再添加。
https://img-blog.csdnimg.cn/direct/70dfa22dd7cc41ada3d3b7e19a1befc4.png#pic_centerhttps://img-blog.csdnimg.cn/direct/b5912ed538cc4f36a8def9edbeb8d38e.png#pic_center
(5)默认值束缚
添加:alter table 表名 modfiy 字段名 数据类型 default '值';
删除:alter table 表名 modify 字段名 数据类型 default null;
实训案例:删除tb_goods表的默认值束缚,再添加。
https://img-blog.csdnimg.cn/direct/bee122451db64503ac2438094b4153dd.png#pic_center
(6)无符号束缚
添加:alter table 表名 modfiy 字段名 数据类型 unsigned
;删除:alter table 表名 modify 字段名 数据类型 实训案例:删除tb_goods表的无符号束缚,再添加。
https://img-blog.csdnimg.cn/direct/05bedf4f5c5b4b44918f1f594747dd27.png
(7)外键束缚
添加:alter table 表名 add constaint 约束名 foreign key(外键列)references 主键表(主键列)
删除:
第一步:删除外键
alter table 表名 drop foreign key 约束名
第二步:删除索引
alter table 表名 drop index 索引名
约束名和索引名一样
实训案例:删除tb_goods表的外键束缚,再添加
https://img-blog.csdnimg.cn/direct/537d4cb82ed94a29a863d035195c4c46.png#pic_center
https://img-blog.csdnimg.cn/direct/ea3e2bcd57ba4d9482958ad3b14fa7a8.png#pic_center
四、删除表
1、删除没有被关联的表
drop table 表名;
drop table 表1,表2,......;
https://img-blog.csdnimg.cn/direct/1253ac817e0a4e74b6fd712b57f8bc3a.png#pic_center
2、删除被其他表关联的主表
(1)从表不生存
删除从表—>删除主表
https://img-blog.csdnimg.cn/direct/d74706d9adfa49169ecc892195d4bee6.png#pic_center
(2)从表生存
删除从表外键—>删除主表
alter table 表名 drop foreign key 外键约束名
https://img-blog.csdnimg.cn/direct/8170d6779eac40f09b782b0aff28c895.png#pic_center
https://img-blog.csdnimg.cn/direct/96cf66e31e34492f8bab03e8f360c351.png#pic_center
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]