当前位置:网站首页>MySQL之创建表的基本操作
MySQL之创建表的基本操作
2022-08-02 07:24:00 【embelfe_segge】
MySQL
前面我分享了关于数据库的基本操作,例如创建和删除等,接下来分享如何在一个数据库中创建表
1、创建一个具有id、name、salary字段的表
create table tb_tmp01
(
id INT(11),
name VARCHAR(25),
deptId INT(11),
salary FLOAT
);
2、显示该表
show tables;
3、删除该表
drop table tb_tmp01;
4、创建新表
create table tb_emp02
(
id int(5) null comment’user_id’,
name varchar(10) null comment’user_name’,
depId varchar(10) null comment’user_department’,
salary double(7,1) null comment’user_salary’
);
该表有id、name、depI、salary属性,
其中null表示该字段的值可以为空,
comment是对该字段的描述。
5、默认值default
在(4)中我们有些字段是null,显然不符合我们数据设计的规范,所以我们引入了默认值,当用户没有输入的时候,该记录的这个字段则使用我们默认的字段。
create table tb_emp03
(
id int(5) unique comment’user_id’,
name varchar(10) not null comment’user_name’,
depId varchar(10) default 2 comment’user_department’,
salary double(6,1) not null default 5000.00 comment’user_salary’
);
unique:表示该字段的值是不可重复的
not nul:表示该字段的值不可为空
defailt 2:表示当没有赋值的时候,该字段的值默认为2
not null default 5000.00:表示该字段不仅不能为空,而且初始默认值为5000.00
6、为表单插入数据
insert into tb_emp03(id,name) value (‘001’,‘xiaoming’);
7、查询表单数据
select * from tb_emp03;
- select:查询、选择
- :通配符
- from:从XXXX中选择
- tb_emp03:表名
- ; :“;”只是一个标点符号

不够尽兴,我们再插入一个数据并显示:

8、主键
主键(Primary key): 也称为主码或主关键字,用于惟一地确定一个元组的属性或属性组(复合主码)。
每个关系都有一个并且只有一个主码。
同样的举个例子:
create table tb_emp05 (id int(11) not null unique comment ‘员工编号’,name varchar(50) not null comment ‘员工性名’,deptId int(11) default 2 comment ‘部门编号’,salary double(5,2) comment ‘员工薪资’
);
我们可以看到,字段id修饰为 not null unique,代表着必须唯一而且不能为空,束主键约束 = ``非空约束 + 唯一性约
所以,id为我们该表的主键
插入数据并显示
9、定义主键
主键约束关键字primary key
写法一(不推荐)
create table tb_emp06 (id int(11) primary key comment ‘员工编号’,name varchar(50) not null comment ‘员工性名’,deptId int(11) default 2 comment ‘部门编号’,salary double(5,2) comment ‘员工薪资’
);
写法二(推荐)
create table tb_emp04 (id int(11) comment ‘员工编号’,name varchar(50) not null comment ‘员工性名’,deptId int(11) default 2 comment ‘部门编号’,salary double(5,2) comment ‘员工薪资’,
primary key(id)
);
10、使用自增关键字
auto_increment:自动生成相关字段的信息
PS:从1开始生成
11、外键
外键必须存在主从表的关联字段
应用场景一对多或者多对一的关系
先建主表
create table exm01
(
id int(5) auto_increment comment’user_id’,
name varchar(10) comment’user_name’,
primary key(id)
);
后建从表
create table exm02
(
id int(5) auto_increment comment’user_id’,
name varchar(10) not null comment’dept_name’,
deptId varchar(5) default 2 comment’dept_id’,
primary key(id),
constraint fk_exm02_exm01 foreign key(id) references exm01(id)
);
12、show create table tb_emp04;13、show create database db_tmp01;
能力有限,各位有补充的或者发现有什么问题可以在评论区留言,蟹蟹~~~
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- HCIP 第十三天
- Transimpedance amplifier
- MySQL-数据库事务详解
- Mysql error 2003 solution Can 't connect to Mysql server on' localhost '(10061).
- Appium 滑动问题
- Data reveal that the average cost is as high as $4.35 million in 2022, a record!
- Control 'ContentPlaceHolder1_ddlDepartment' of type 'DropDownList' must be placed inside a form tag with runat=server.
- 替换ptmalloc,使用tcmalloc和jemalloc
- 常用的云安全防护措施盘点
- OC-error prompt
猜你喜欢

HCIP第七天

MySQL-执行流程+缓存+存储引擎

sql创建表格 如图 运行完提示invalid table name 是什么原因

Kind of weird!Access the destination URL, the host can container but not

MGRE综合实验

通过建立新的SaaS业务来推动增长的六种方法

Mysql报错2003 解决办法 Can‘t connect to MySQL server on ‘localhost‘ (10061)

HCIP第二天

pnpm install出现:ERR_PNPM_PEER_DEP_ISSUES Unmet peer dependencies

MySQL-锁机制
随机推荐
有点奇怪!访问目的网址,主机能容器却不行
HCIP 第五天
HCIP第三天
PLSQL Developer安装和配置
From cloud computing to function computing
CollectionUtil:一个函数式风格的集合工具
HCIP 第十一天
常用的云安全防护措施盘点
Mysql报错2003 解决办法 Can‘t connect to MySQL server on ‘localhost‘ (10061)
Postgres horizontal table, automatically create partitions, table by time
Data reveal that the average cost is as high as $4.35 million in 2022, a record!
Metasploit(MSF)基础超级详细版
Agile, DevOps and Embedded Systems Testing
MySQL - based
Probability Theory and Mathematical Statistics
Modify apt-get source to domestic mirror source
MGRE环境下的OSPF
如何保护智能家居不受黑客攻击
MySQL-执行流程+缓存+存储引擎
LeetCode 2312. Sell Wood Blocks