MySQL基础

本文详细介绍了MySQL数据库的基本操作,包括创建、展示和使用数据库,定义及修改表结构,以及管理表中的字段。此外,还涵盖了主键、候选键和外键的概念。通过实例展示了如何创建数据库、添加字段、修改表结构以及删除表等常见操作,是学习MySQL数据库管理的实用指南。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MySQL基础
1、创建、展示、添加、修改、删除

创建库
creat database db_name;
creat database/schema
if not exists db_name
default character set = charset_name#指定字符集,如GB2313
default collate = collation_name;#指定字符集校对规则
展示库
show databases;
使用库
use db_name;
修改库
alter database db_name;#修改数据库
default character set = character_name
default collate = collation_name;
删除库
drop database if exists db_name

创造表
creat table tb_name
(
stuNo char(10) not null unique,
stuName varchar(20) not null,
sex char(2),
birthday date,
classNo char(6)
)
engine=InnoDB;#储存引擎
展示引擎
show engines;
展示表
show tables from/in db_name;
展示字段
show columns from tb_name from db_name;#查询表基本结构
desc tb_name#简化写法
show create table tb_name:#查询表详细结构
添加字段
alter table tb_name add column co_name co_type
not null after co_name1 / FIRST;
修改字段
alter table tb_name change column co_before co_new co_type co_other;
alter table tb_name alter column co_name set/drop default;
alter table tb_name modify column co_name co_type co_other after co_name1 / FIRST;
删除字段
alter table tb_name drop column co_name
重命名表
alter table tb_before rename to tb_new;
rename table tb_before to tb_new;
删除表
drop table if exists tb_name;

2、主键、候选键、外键

...
(
stuNo char(10) primary key,
...
)
...;

...
(
...
...
primary key(stuNo)
)
...;

...
(
stuNo char(6) primary key,
stuName char(6) not null unique,
...
constraint uq_stu unique(stuName)
)
...;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值