DDL语言

DDL(Data declare Language):数据定义型语言。主要用于数据库和表的操作。创建数据库,修改数据库,删除数据库, 创建表 删除表 修改表结构等相关的操作。这里使用的关键字: create, drop, alter,show.

创建数据库
create database 数据库名; -- 这里分号为英文下的

create database test; -- 创建数据库
删除数据库
drop database 数据库名;

drop database test; -- 删除数据库
切换到指定的数据库
use 数据库名;

use test; -- 切换指定数据库
关于对表的操作
我们的数据实际存储的位置是对应的表中:

如何创建表

create table 表名(
   列名 数据类型, -- 逗号为英文下
   列名 数据类型,
   列名 数据类型
   ......
   列名 数据类型  -- 注意 最后没有逗号
);

数据类型: mysql中常见的数据类型有哪些?

varchar(长度) ---表示可变的字符串65535。长度表示最多可以输入的内容个数

char(长度) ----表示不可变的字符串255。长度固定的。适合固定长度的内容。

text() ---文本字符串--

int----表示整数

double--表示小数。--不要使用这种。因为double存在精度丢失。

decimal--表示小数,设置小数点的位数。decimal(10,2)

date: 表示日期类型 2020-10-11

datetime: 表示日期时间类型。2020-10-11 15:13:16

create table personalInformation( -- 个人信息
    name varchar(10), -- 名字
    sex char(1), -- 性别
    curriculumVitae text, -- 工作简历
    age int, -- 年龄
    height decimal, -- 身高
    dateOfArrival date, -- 出生日期
    timeToFillOutTheForm datetime -- 填写日期
);

修改表名
alter table 老表名 rename to 新表名;

alter table information rename to info; -- 修改表名
删除表
drop table 表名;

drop table personalInformation; -- 删除表
查看表结构
desc 表名;

desc personalInformation; -- 查看表结构
修改表结构

-- 增加一列
alter table 表名 add column 列名 数据类型。

-- 删除某一列
alter table 表名 drop column 列名;

-- 修改列名 
alter table 表名 change column 列名 新列名  数据类型;

-- 修改数据类型
alter table 表名 modify column 列名 新的数据类型
alter table personalInformation add column university varchar(10); -- 添加大学
alter table personalInformation drop column university; -- 删除大学
alter table personalInformation change column university dx varchar(10); -- 改变大学的列名
alter table personalInformation modify column university varchar(20); -- 改变大学的数据类型

关于表中列的约束
约束: 限制表中指定列的值的约束。

何时添加约束:
第一种:在创建表结构时添加约束。

create table student(
    id int primary key , -- 主键约束 该字段的值非空且唯一。只能有一个主键
    name varchar(20) not null, -- 非空约束 该字段的值不能为null
    Phone char(11) unique ,-- 唯一约束,该字段的值不能重复。但是可以为null
    age int check (age>2 and age<30),-- 检查约束,该字段的值必须在2~30之间 mysql 8.0.14后才能使用
    sex char(1) default '男' check (sex='男' or sex='女') -- 默认约束
);

第二种: 表创建好以后添加约束。

-- 添加唯一约束
alter table 表名  add constraint 约束名 unique(列名);
-- 检查约束
alter table 表名  add constraint 约束名 check(条件);
-- 非空约束
alter table 表名 modify 列名 数据类型 not null;
-- 主键约束
alter table 表名 add CONSTRAINT 约束名 primary key(列名)
-- 添加默认约束
ALTER TABLE 表名 MODIFY 列名 列类型 DEFAULT 默认值
create table student(
    id int primary key , -- 主键约束 该字段的值非空且唯一。只能有一个主键
    name varchar(20) not null, -- 非空约束 该字段的值不能为null
    Phone char(11) unique ,-- 唯一约束,该字段的值不能重复。但是可以为null
    age int check (age>2 and age<30),-- 检查约束,该字段的值必须在2~30之间 mysql 8.0.14后才能使用
    sex char(1) default '男' check (sex='男' or sex='女') -- 默认约束
);
-- 主键约束
alter table tbl_student add constraint primary key (id);
-- 设置非空约束
alter table tbl_student modify name varchar(20) not null;
-- 设置检查约束
alter table tbl_student add constraint check(sex='男' or sex='女');
-- 唯一约束
alter table tbl_student add constraint unique(name);
-- 默认约束
alter table tbl_student modify birthday date default '2000-10-11';
-- 主键约束
alter table personalInformation add constraint primary key(id);
-- 设置非空约束
alter table personalInformation modify name varchar(10)not null ;
-- 设置检查约束
alter table personalInformation add constraint check(sex='男' or sex='女');
-- 唯一约束
alter table personalInformation add constraint unique (phone);
-- 默认约束
alter table personalInformation modify timeToFillOutTheForm datetime default now();

外键约束
外键约束用来让两张图的数据之间建立连接,保证数据的一致性和完整性。

表都已经创建完毕再添加外键约束
alter table 表名 add constraint 约束名 foreign key 外键列名 references 主表名(主键列)

alter table student add constraint xh foreign key(id) references personalInformation(id);

posted on   小木不痞  阅读(14)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示