MySQL_6.MySQL常用创建语句

1.数据库创建,查询,删除

(1)创建一个test数据库

复制代码
CREATE DATABASE test ;
CREATE DATABASE IF NOT EXISTS test;

default character set :默认字符集

复制代码
CREATE DATABASE IF NOT EXISTS test default character set UTF8;
# default collate:默认排序规格
# utf8_general_ci:不区分大小写
# utf8_general_cs:区分大小写            
CREATE DATABASE IF NOT EXISTS tkjy default character set UTF8 default collate utf8_general_ci;  

(2)切换数据库

复制代码
use test;

(3)查询数据库

复制代码
show databases; 

如果有很多数据库,模糊查询某个数据库

复制代码
show databases like '%test%';

查询创建数据库的语句

复制代码
show create database test;

更新数据库选项信息(操作需要谨慎)

复制代码
alter database test character set gbk;

(4)删除数据库

复制代码
drop database test;
drop database if exists test;

(5)使用mysqladmin工具创建、删除数据库

复制代码
mysqladmin create test  -uroot -p123456
mysqladmin drop test  -uroot -p123456

2.mysql约束对应的五大关键词

复制代码
NOT NULL:        如果在列上定义了 not null,那么当插入数据时,必须为列提供数据
UNIQUE:          当定义了唯一约束后,该列值是不能重复的,但是可以为null
Primary Key:    用于唯一的标识表行的数据,当定义主键约東后,该列不但不能重复而且不能为NULL。
Foreign Key:    用于定义主表和从表之间的关系,外键约束要定义在从表上,主要则必须具有主键约束或是 uniques约束,当定义外键约束后,要求外键列数据必须在主表的主键列存在或是为NULL
CHECK:          用于强制行数据必须满足的条件

3.创建表,修改表,删除表等操作

(1)在test数据库下面创建一张student学生表

复制代码
use test;
create table if not exists student(
        id int(5) unsigned auto_increment primary key comment '学生表主键',
        name varchar(20) not null comment '学生姓名',
        age tinyint  not null comment '学生年龄',
        admission_time datetime comment '入学时间',
        gender enum('男','女','保密') comment '学生性别',
        student_id int(10) UNIQUE comment '学生编号'
) engine=innodb  default charset=utf8  comment '学生表';

# auto_increment :            主键自增(可选操作)
# engine :                    表使用存储引擎(可选操作)
# comment :                   注释(可选操作)
# default charset :           表的字符集(可选操作)

(2)使用like 关键字通过旧表创建新表 ( 包括旧表的 结构 + 信息 + 索引 )

复制代码
create table <table_name> like <old_table_name>;
create table a like student;

(3)使用as 关键字通过旧表创建新表 ( 包括旧表的 结构 + 信息 )

复制代码
create table <table_name> as select * from <old_table_name>;
create table b as select * from a;

(4)查看表、表结构、表创建语句

复制代码
show tables;
show tables from test; 
show tables like '%stud%';
desc student;
show create table student \G;

(5)删除表

复制代码
drop table student;

(6)重命名表

复制代码
rename table student to new_student;

(7)截断表

复制代码
truncate table new_student;

(8)修改表结构

复制代码
alter table <table_name> add/drop/modify/change

增加列、增加主键

复制代码
alter table new_student add student_from varchar(10) not null;
alter table new_student add (phone int unique not null,email varchar(20));
alter table new_student add primary key (id);

删除列、删除主键约束、删除自增的主键约束

复制代码
alter table new_student drop email;
alter table new_student drop primary key;
alter table new_student change id id int; 
alter table new_student drop primary key;

重命名列

复制代码
alter table new_student change student_from st_from varchar(10);

修改表字段属性

复制代码
alter table new_student modify st_from varchar(15) unique ;

修改字符集,有数据不能改

复制代码
alter table new_student character set gbk;
alter table new_student character set utf8;

(9)在test数据库创建包含外键的员工表(YG)和工资表(gz)

创建工资表

复制代码
create table if not exists test.gz(
    id int(5) primary key comment '工资表主键',
    salary int(7) not null comment '薪水',
    job varchar(10)  not null comment '工作岗位',
    department varchar(5) not null comment '工作部门'
) 
engine=innodb  default charset=utf8  comment '员工表';

工资表插入数据

复制代码
insert into test.gz values (1,10000,'销售','销售部');
insert into test.gz values (2,15000,'Oracle DBA','技术部');
insert into test.gz values (3,20000,'mysql DBA','技术部');
insert into test.gz values (4,18000,'java','研发中心');
insert into test.gz values (5,30000,'C++','研发中心');
insert into test.gz values (6,16000,'python','研发中心');
commit;

查看工资

复制代码
select * from test.gz;

创建员工表

复制代码
create table if not exists test.yg(
    id int(5) unsigned auto_increment primary key comment '员工表主键',
    name varchar(7) not null comment '员工姓名',
    age tinyint  not null comment '员工年龄',
    entry_time year comment '入职时间',
    gender enum('男','女','保密') comment '员工性别',
    gz_id int(5) not null,
    foreign key (gz_id) references tkjy.gz (id)
) 
    engine=innodb  default charset=utf8  comment '员工表';

外键必须是主表的主键或者唯一键,如果是另外一张表主键的话,该表主键不允许带有auto_increment 自增长属性。

主表记录删除时 on delete cascade / 更新时的动作 on update cascade

创建表以后再增加外键也可以

复制代码
 alter table tkjy.yg add foreign key (gz_id) references test.gz (id);

#插入数据测试(在主表间键值内成功)

复制代码
insert into test.yg values (1,'春野樱',18,2015,'女',1);
insert into test.yg values (2,'漩涡鸣人',18,2016,'男',2);
insert into test.yg values (3,'宇智波佐助',18,2017,'男',2);
insert into test.yg values (4,'第一代火影',65,1970,'男',3);
insert into test.yg values (5,'第二代火影',60,1975,'男',4);
insert into test.yg values (6,'第三代火影',38,2000,'男',5);
insert into test.yg values (7,'第四代火影',38,2000,'男',6);
commit;
select * from test.yg;

#插入数据测试(在主表间键值外失败)

复制代码
insert into tkjy.yg values (8,'宇智波斑',28,2020,'男',7);

外键总结:mysql数据库不建议使用外键、会极大影响数据库运行性能(并发访问)。

相关推荐
南棱笑笑生2 分钟前
20250510解决NanoPi NEO core开发板在Ubuntu core22.04.3系统下适配移远的4G模块EC200A-CN的问题
数据库·postgresql
苹果酱056732 分钟前
Mac下Robotframework + Python3环境搭建
java·vue.js·spring boot·mysql·课程设计
小白爱编程HC34 分钟前
用pymysql操作数据库
数据库·python·mysql
追风赶月、1 小时前
【Redis】Redis的主从复制
数据库·redis
冰箱上的笑话1 小时前
MySQL 数据库故障排查指南
数据库·mysql·adb
IvanCodes1 小时前
三、Hive DDL数据库操作
大数据·数据库·hive·hadoop
Chasing__Dreams2 小时前
Mysql--基础知识点--91.2--processlist
数据库·mysql
JhonKI2 小时前
【MySQL】行结构详解:InnoDb支持格式、如何存储、头信息区域、Null列表、变长字段以及与其他格式的对比
android·数据库·mysql
今天阳光明媚吗3 小时前
SQlite数据库
数据库·sqlite
iVictor3 小时前
Java应用出现 Public Key Retrieval is not allowed 报错的常见原因和解决方法
mysql