创建数据库
creat table test(
#整数通常使用int
test_id int,
#小数通常使用decimal
test_price decimal,
#普通文本通常使用,并使用Default指定默认值
test_name varchar(255) default "Xxx",
#大文本类型使用test
test_desc text,
#图片使用blob
test_img blob,
#日期类型使用DateTime
test_date datetime,
);
-----------------------------------------------------------------
mysql 支持的列类型
1.tinyint,smallint,mediumint,int,bigint
2.float,double
3.decimal(dec)
4.date
5.time
6.datetime
7.timestamp
8.year
9.char
10.varchar
11.binary(定长的二进制字符串类型,以二进制形式保存字符串)
12.varbinary
13.tinyblob,blob,mediumblob,longblob
14.tinytext,text,mediumtext,longtext
15.enum('value1','value2'...)//枚举类型(只能是其中之一)
16.set('value1','value2'...)//集合类型(可以是其中几个)
--------------------------------------------------------------------
#创建数据表,该数据表和user_info完全相同,数据也完全相同
create table hehe
as
select * from user_info;
---------------------------------------------------------------------
#修改表的结构的语法
alert table 表名
add(
#可以定义多个列定义
colum_name datatype [default expr],
...
);
---------------------------------------------------------------------
#为hehe数据表增加一个hehe_id字段,该字段类型为int
alter table hehe
add hehe_id int;
#为hehe数据包增加aaa,bbb字段,两个字段的类型都为varchar(25)
alter table hehe
add aaa varchar(25),bbb varchar(25);
----------------------------------------------------------------------
#将hehe表的hehe_id列修改为varchar(255)类型
alter table hehe
modify hehe_id varchar(255);
#将hehe表的bbb列修改为int类型
alter table hehe
modify bbb int;
----------------------------------------------------------------------
#删除指定的列
alter table hehe
drop column_name
#重命名数据表
alter table hehe
rename to wawa;
----------------------------------------------------------------------
#将wawa表的字段bbb字段重命名为ddd
alter table wawa
change bbb ddd int;
#删除表
drop table 表名
----------------------------------------------------------------------
数据库约束
not null
unique
primary key
foreign key
check
#not null约束
create table hehe(
#建立了非空约束,这意味着hehe_id不可以为null
hehe_id int not null,
#mysql为空约束不能指定名字
hehe_name varchar(25) default 'xyz' not null,
#下面列可以为空,默认值就是为null
hehe_gender varchar(28) null
);
---------------------------------------------------------------------
#增加非空约束
alter table hehe
modify hehe_gender varchar(30) not null
#取消非空约束
alter table hehe
modify hehe_name varchar(3) null;
#取消非空约束,并指定默认值
alter table hehe
modify hehe_name varchar(255) default 'abc' null;
-------------------------------------------------------------------
unique约束
#建立表时创建唯一约束,使用列级约束语法建立约束
create table unique_test(
#建立了非空约束,着意味着test_id不可以为null
test_id int not null,
#建立了unique约束
test_name varchar(30) unique
);
#创建表时,使用表级约束语法建立约束
create table unique_test(
test_id int not null,
test_name varchar(30),
test_pass varchar(30),
#使用表级约束创建唯一约束
unique(test_name),
constraint test_uk unique(test_pass)
#constrain test1_uk unique(test_name,test_pass)
);
#修改唯一约束
alter table unique_test
add unique(test_name,test_pass);
#为表增加约束
alter table unique_test
modify test_name varchar(30) unique;
-------------------------------------------------------------------
primary key约束
create table primaryKey_test(
primaryKey_id int primary key,
test_name varchar(255)
);
create table primaryTest(
primary_id int not null,
primary_name varchar(29),
constraint pk_test primary key(primary_id)
);
#删除主键约束
alter table test
drop primary key;
#使用表级语法增加主键约束
alter table test
add primary key(test_id ,test_name);
#使用列级约束语法增加主键约束
alter table test
modify test_name varchar(30) primary key;
------------------------------------------------------------------
#为了保证从表参照的主表存在,通常应该先建立主表
create table teacher_table(
#auto_increment
teacher_id int auto_increment,
teacher_name varchar(255),
primary key(teacher_id)
);
create table student_table(
student_id int auto_increment primary key,
student_name varchar(255),
#指定java_teacher参照到teacher_table的teacher_id的列
java_teacher int references teacher_table(teacher_id)
#java_teacher int
#foreign key(java_teacher) references teacher_table(teacher_id)
#constraint student_teacher_fk foreign key(java_teacher) references
teacher_table(teacher_id)
);
---------------------------------------------------------------------------------------------
#check约束
create table test(
test_id int auto_increment primary key,
test_age int not null,
check(test_age>0 and test_age
)
0
0
相关文章
什么是mysql索引_mysql索引基础原理解析
mysql如何完成基础数据校验_mysql项目安全实践
mysql常用操作有哪些_mysql基础命令总结
如何查询表中数据_mysql select基础操作
如何在mysql中更新数据_mysql update语句基础讲解
本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门AI工具
相关专题
本专题系统整理pixiv网页版官网入口及登录访问方式,涵盖官网登录页面直达路径、在线阅读入口及快速进入方法说明,帮助用户高效找到pixiv官方网站,实现便捷、安全的网页端浏览与账号登录体验。
1044
2026.02.13
本专题系统整理微博网页版官方入口及网页端登录方式,涵盖首页直达地址、账号登录流程与常见访问问题说明,帮助用户快速找到微博官网主页,实现便捷、安全的网页端登录与内容浏览体验。
334
2026.02.13
本专题围绕Flutter框架展开,系统讲解跨平台UI构建原理与状态管理方案。内容涵盖Widget生命周期、路由管理、Provider与Bloc状态管理模式、网络请求封装及性能优化技巧。通过实战项目演示,帮助开发者构建流畅、可维护的跨平台移动应用。
213
2026.02.13
本专题面向前端开发者,深入讲解 TypeScript 类型系统与大型项目结构设计方法,并结合 Vite 构建工具优化前端工程化流程。内容包括模块化设计、类型声明管理、代码分割、热更新原理以及构建性能调优。通过完整项目示例,帮助开发者提升代码可维护性与开发效率。
35
2026.02.13
本专题围绕 Redis 在高并发系统中的应用展开,系统讲解主从复制、哨兵机制、Cluster 集群模式及数据分片原理。内容涵盖缓存穿透与雪崩解决方案、分布式锁实现、热点数据优化及持久化策略。通过真实业务场景演示,帮助开发者构建高可用、可扩展的分布式缓存系统。
111
2026.02.13
本专题系统整理雨课堂网页版官方入口及在线登录方式,涵盖账号登录流程、官方直连入口及平台访问方法说明,帮助师生用户快速进入雨课堂在线教学平台,实现便捷、高效的课程学习与教学管理体验。
17
2026.02.12
本专题汇总豆包AI官方网页版入口及在线使用方式,涵盖智能写作工具、图片生成体验入口和官网登录方法,帮助用户快速直达豆包AI平台,高效完成文本创作与AI生图任务,实现便捷智能创作体验。
813
2026.02.12
本专题面向后端开发与数据库工程师,深入讲解 PostgreSQL 查询优化原理与索引机制。内容包括执行计划分析、常见索引类型对比、慢查询优化策略、事务隔离级别以及高并发场景下的性能调优技巧。通过实战案例解析,帮助开发者提升数据库响应速度与系统稳定性。
97
2026.02.12
热门下载
精品课程

