2020-12-25

数据库mysql笔记 2

1.创建表
create table 表名
( 字段名 数据类型,
字段名 数据类型,
字段名 数据类型)
–创建表student,该表有stuno(学号),stuname(学生姓名),age(年龄)
create table student
(stuno char(13),
stuname varchar(50),
age tinyint(3));

create table 表名
(字段名1 数据类型 primary key auto_increment not null,
字段名2 数据类型 unique,
字段名3 数据类型 unsigned,
字段名4 数据类型 default 默认值,
字段名5 数据类型,
constraint 约束名 foreign key(外键字段名) references 主表(主键字段名))
character set=utf8;
作业:
1、创建表class,该表有字段classno(char(5))有主键,不为空约束;classname字段(varchar(20)),字符集为utf8
use books;
create table class
(classno char(5) primary key not null,
classname varchar(20) )character set=utf8;
2、创建表student,该表有字段id(int(7)),有主键,自增,不为空约束;stuno字段(char(10))有唯一约束,sname字段(varchar(30));age字段(int(3))有无符号约束;spend字段(decimal(6,1))有默认约束,默认值为18000.0;classno字段连接class表;字符集为utf8
create table student
(id int(7) primary key auto_increment not null,
stuno char(10) unique,
sname varchar(30),
age int(3) unsigned,
spend decimal(6,1) default 18000.0,
classno char(5),
constraint fk_classno foreign key(classno) references class(classno))
character set=utf8;

插入数据时,先插入主表数据,再插入从表数据
删除数据时,先删除从表数据,再删除主表数据

2.查看表结构
describe 表名; desc 表名;
描述
–查看student表的结构
describe student;
3.查看建表结构
show create table 表名;
–查看student的建表结构
show create table student;
4.修改表
(1)修改表名
alter table 原表名 rename 新表名;
–修改customer的表名为cus
alter table customer rename cus;
–修改student表名为stu
alter table student rename stu;
(2)修改字段的数据类型
alter table 表名 modify 字段名 新数据类型;
–修改cus表的cusno字段的数据类型为char(6)
alter table cus modify cusno char(6);
–修改student表的sname字段的数据类型为varchar(30)
alter table student modify sname varchar(30);
(3)修改字段名
alter table 表名 change 原字段名 新字段名 数据类型;
–修改stu表的sname字段,把他的名字修改为stuname
alter table stu change sname stuname varchar(30);
5.添加字段
(1)在表的最后添加
alter table 表名 add 字段名 数据类型;
–在stu表添加一个school字段,数据类型为varchar(30)
alter table stu add school varchar(30);
(2)在表的第一列添加
alter table 表名 add 字段名 数据类型 first;
(3)在指定的某个字段之后添加
alter table 表名 add 字段名 数据类型 after 字段名1;
6.删除字段
alter table 表名 drop 字段名;
–把stu表的school字段删除
alter table stu drop school;
7.修改字段的顺序
alter table 表名 modify 字段名 数据类型 first;
alter table 表名 modify 字段名 数据类型 after 字段名1;
–在最后插入sex字段(单选,男,女)
alter table stu add sex enum(‘男’,‘女’)
–修改stu表的sex字段,把它调整到spend后面
alter table stu modify sex enum(‘男’,‘女’) after spend;
8.修改存储引擎InnoDB
alter table 表名 engine=存储引擎名;
9.删除表
drop table [if exists] 表名

主表,从表 用外键连接
删除数据,先删除从表
添加数据,先添加主表

10.删除外键约束
alter table 表名 drop foreign key 约束名;

2.desc 表名;

3.SHOW CREATE TABLE table_name;

4.alter table 表名 change 原字段名 新字段名 数据类型;

5.alter table 表名 add 字段名 数据类型

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
func (c *cAsset) CreatComponent(r *ghttp.Request) { var req *v1.CreateComponentReq if err := r.Parse(&req); err != nil { r.Response.WriteJson(g.Map{ "code": 1, "msg": err.Error(), }) } createRequest := &creativecomponent.CreateRequest{ AdvertiserID: req.AdvertiserID, } res, err := service.Asset().Create(createRequest) if err != nil { r.Response.WriteJson(g.Map{ "code": 2, "msg": err.Error(), }) } r.Response.WriteJson(res) }这段代码中creativecomponent.CreateRequest的过滤条件为type CreateRequest struct { // AdvertiserID 广告主ID AdvertiserID uint64 `json:"advertiser_id,omitempty"` // ComponentInfo 组件信息 ComponentInfo *ComponentInfo `json:"component_info,omitempty"` },其中ComponentInfo为type ComponentInfo struct { // ComponentID 组件ID ComponentID model.Uint64 `json:"component_id,omitempty"` // ComponentType 组件类型 ComponentType enum.ComponentType `json:"component_type,omitempty"` // ComponentName 组件名称。长度小于等于20。一个中文长度为2 ComponentName string `json:"component_name,omitempty"` // ComponentData 组件详细信息。不同的component_type对应的值不同,具体的结构见创建或更新接口定义 ComponentData ComponentData `json:"component_data,omitempty"` // CreateTime 创建时间。格式"2020-12-25 15:12:08" CreateTime string `json:"create_time,omitempty"` // Status 组件审核状态。 Status enum.ComponentStatus `json:"status,omitempty"` }想要把ComponentInfo作为参数放到createRequest中,该怎么做?请详一点
07-20

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值