MySQL----DDL

本文涉及的产品
RDS MySQL Serverless 基础系列,0.5-2RCU 50GB
RDS MySQL Serverless 高可用系列,价值2615元额度,1个月
简介: 数据定义语言

-- 查看用户名和数据库版本

select user(), version ();

-- 查看所有引擎

show engines;

DDL——执行后,不可回滚!

DML——执行后,可回滚!

 

-- 查看数据库名称

show databases;#查看当前运行的所有数据库名称

show databases like 'm%';#查看以m开头的数据库名称

 

-- 切换数据库

use mydatabase;

 

-- 查看数据库中所有的表

show tables;

-- 查看指定数据库中的表

show tables from mydatabase;

 

-- 查看当前所在数据库

select database();

 

-- 创建数据库

create database if not exists mydatabase;

 

-- 删除数据库

drop database if exists my;

 

-- 修改数据库中的字符集

alter database mydatabase character set 'utf8';

-- 查看当前数据库的字符集

show variables like 'character_set_database';

 

 

-- 创建表

create table if not exists teachers(

tid int primary key auto_increment, #设置主键自增长

tname varchar(10) not null unique, #设置非空且唯一

age int,

gender varchar(10),

address varchar(10) default 'China', #设置默认值

tsubject varchar(10)

-- primary key (tid)  #设置主键

);

 

-- 查看指定表的创建语句

show create table students;

 

-- 查看表结构

desc students;

-- 删除表

drop table if exists stu;

 

-- 清空表

delete from stu;#精细化删除数据,而删除部分数据时只能使用delete

truncate table stu;# 粗暴型清空全部数据

 

 

-- 修改表

 

-- 添加列

ALTER TABLE students ADD hobby VARCHAR ( 15 ) after phone;

alter table students add score int after phone ;

 

-- 修改列的数据类型

alter table students modify score double(4,2);

 

-- 修改列名

alter table students change hobby interest varchar(10);

 

-- 删除列

alter table students drop score;

 

-- 修改表名称

alter table students rename to test_students;

 

-- 重命名表

rename table test_students to students;

 

-- 复制表的结构

create table if not exists stu like students;

-- 复制表的结构和数据

create table stu select * from students;

 

 

 

-- 设置主键(主键具有唯一性和非空特性)

alter table students add primary key (sid);

-- 定义多个字段做联合主键

alter table students add primary key (sid,name,age);

-- 删除主键

alter table students drop primary key ;

 

-- 主键自增长

alter table students change sid sid int auto_increment;

-- 删除主键自增长

alter table students change sid sid int ;

 

 

 

 

-- 默认值

alter table students alter column address set default 'China';

alter table students alter column sname set default '***';

-- 取消默认值

alter table students alter column sname set default;

 

-- 非空

alter table students modify sname varchar(10) not null;

alter table students modify gender char(1) not null;

-- 取消非空

alter table students modify gender char(1);

 

-- 唯一

alter table students modify sname varchar(10) unique;

-- 取消唯一

alter table students modify sname varchar(10);

 

 

-- 查看表的约束

show keys from students;

相关实践学习
基于CentOS快速搭建LAMP环境
本教程介绍如何搭建LAMP环境,其中LAMP分别代表Linux、Apache、MySQL和PHP。
全面了解阿里云能为你做什么
阿里云在全球各地部署高效节能的绿色数据中心,利用清洁计算为万物互联的新世界提供源源不断的能源动力,目前开服的区域包括中国(华北、华东、华南、香港)、新加坡、美国(美东、美西)、欧洲、中东、澳大利亚、日本。目前阿里云的产品涵盖弹性计算、数据库、存储与CDN、分析与搜索、云通信、网络、管理与监控、应用服务、互联网中间件、移动服务、视频服务等。通过本课程,来了解阿里云能够为你的业务带来哪些帮助     相关的阿里云产品:云服务器ECS 云服务器 ECS(Elastic Compute Service)是一种弹性可伸缩的计算服务,助您降低 IT 成本,提升运维效率,使您更专注于核心业务创新。产品详情: https://www.aliyun.com/product/ecs
目录
相关文章
|
9月前
|
存储 关系型数据库 MySQL
|
9月前
|
存储 算法 关系型数据库
|
8天前
|
SQL
mysql-DML篇
DML(增删改表中的数据) 1.添加数据 insert into 表名(列名1,列名2)values(数据1,数据2) 2.删除数据 delete from 表名 where 条件 delete from 表名--效率低,有多少条执行多少次 truncate table 表名--先删除表,在创建 3.修改数据 update 表名 set 列名1=值1 条件
18 0
|
9月前
|
存储 SQL 关系型数据库
|
9月前
|
存储 SQL 关系型数据库
MySQL----存储过程(二)
MySQL----存储过程(二)
|
9月前
|
安全 关系型数据库 MySQL
|
9月前
|
SQL 关系型数据库 MySQL
|
9月前
|
SQL 关系型数据库 MySQL
MySQL----SQL优化(下)
MySQL----SQL优化(下)
|
9月前
|
存储 SQL 缓存
MySQL----存储引擎
MySQL----存储引擎
|
10月前
|
SQL 关系型数据库 MySQL
mysql DML
DML(增删改表中的数据) 1.添加数据 insert into 表名(列名1,列名2)values(数据1,数据2) 2.删除数据 delete from 表名 where 条件 delete from 表名--效率低,有多少条执行多少次 truncate table 表名--先删除表,在创建 3.修改数据 update 表名 set 列名1=值1 条件
31 0