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

被折叠的 条评论
为什么被折叠?



