MariaDB数据表操作实例
1. MariaDB 数据库操作实例
MariaDB>create database class; //创建class数据库
MariaDB>use class;
MariaDB>create table class.student( //创建student表
id int(4) primary key,
name varchar(4) not null,
age int(2) not null,
);
MariaDB>desc student;

MariaDB>alter table student add address varchar(48); //添加address字段
MariaDB>alter table student add gender enum("boy","girl") after age; //在age字段后面添加gender字段
MariaDB>alter table student change gender
sex ("boy","girl") not null; //将gender字段名更改为sex
MariaDB>alert table student drop sex; //删除sex字段

2. MariaDB 索引创建与删除
MariaDB 索引有普通索引、唯一索引、主键索引、自增主键索引.
MariaDB>create database erp; //创建erp数据库
MariaDB>create table erp.product( //创建product表
id int(4) not null,
name varchar(8) not null,
type enum("usefull","bad") not null,
instruction char(8) not null,
index(id),index(name)
);
MariaDB>desc product;
MariaDB>drop index name on product; //删除name字段的索引
MariaDB>create index shuoming on product(instruction); //给指定字段创建索引
MariaDB>show inedx from erp.product\G; //查看表的索引
MariaDB>create table price( //建表时设置唯一索引
id int(4),
name varchar(4) not null,
TTL int(4) not null,
unique(id),unique(name),index(TTL)
);
MariaDB>desc price;
MariaDB>drop index name on price; //删除索引
MariaDB>create unique index name on price(name); //指定字段创建唯一索引
建表时设置主键索引
如果表内没有主键字段, 则新设置的非空唯一索引字段相当于主键索引功能.
每个表的主键字段只能有一个.
MariaDB>create table test1( //建表时指定主键字段
id int(4) primary key,
name varchar(8)
);
MariaDB>create table test1(
id int(4),
name varchar(8),
primary key(id)
);
MariaDB>create table test2( //自增主键
id int(4) auto_incremnet,
name varchar(8) not null,
age int(2) not null,
primary key(id)
); MariaDB>alter table test1 drop primary key; //删除主键 MariaDB>alter table test2 modify id int(4) not null; //test2中id字段有自增属性,必须先去掉自增属性才能删除主键
MariaDB>alter table test2 drop primary key;
MariaDB>alter table test2 add primary key(id); //指定字段添加主键
3. 外键同步更新与删除
MariaDB>create table water( //创建自增主键的表
w_id int(4) auto_increment,
name varchar(8) not null,
primary key(id)
);
MariaDB>create table river( //river表中的r_id作为外键,water表中w_id作为参考键
r_id int(4) not null,
name varchar(8) not null,
position float(7,2) not null default 0,
index(name),
foreign key(r_id) references water(w_id) on update cascade delete cascade
);
MariaDB数据表操作实例的更多相关文章
- MySQL 数据表操作
MySQL 数据表操作 创建MySQL数据表需要以下信息: -表名: -表字段名: -定义每个表字段: 一.创建数据表 1)mysql> create table table_name (c ...
- mysql数据表操作&库操作
首先登陆mysql:mysql -uroot -proot -P3306 -h127.0.0.1 查看所有的库:show databases; 进入一个库:use database; 显示所在的库:s ...
- mysql 数据表操作 目录
mysql 数据表操作 存储引擎介绍 mysql 使用存储引擎 mysql 数据表的增删改查 mysql 数据类型 mysql 约束条件
- Mysql之数据表操作
数据表操作: 查看当前数据库中所有的表: show tables; 查看当前使用数据库: select database(); 使用数据表: use 表名; 创建数据表: create table 数 ...
- 孤荷凌寒自学python第四十九天继续研究跨不同类型数据库的通用数据表操作函数
孤荷凌寒自学python第四十九天继续研究跨不同类型数据库的通用数据表操作函数 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 今天继续建构自感觉用起来顺手些的自定义模块和类的代码. 不同类型 ...
- MySQL5.7(三)数据表操作
概念在数据库中,数据表是数据库中最重要.最基本的操作对象,是数据存储的基本单位.数据表被定义为列的集合,数据在表中是按照行和列的格式来存储的.每一行代表一条唯一的记录,每一列代表记录中的一个域.1.创 ...
- Sql Server系列:数据表操作
表是用来存储数据和操作数据的逻辑结构,用来组织和存储数据,关系数据库中的所有数据都表现为表的形式,数据表由行和列组成.SQL Server中的数据表分为临时表和永久表,临时表存储在tempdb系统数据 ...
- MySQL数据表操作
创建数据表 打开数据库 USE 数据库名称 mysql> USE D1; Database changed 使用 USE D1:表示打开数据库D1,我们可以通过SELECT DATABASE() ...
- MySQL(5):数据表操作
表名的前缀: 两个student表,为了区分相同逻辑表名的不同应用,给逻辑表名,增加前缀 //学生管理 create table info_student ( name varchar(20), st ...
随机推荐
- jmeter接口自动化部署jenkins教程
首先,保证本地安装并部署了jenkins,jmeter,xslproc 我搭建的自动化测试框架是jmeter+jenkins+xslproc ---注意:原理是,jmeter自生成的报告jtl文件,通 ...
- Restful API 设计参考原则
在项目中,需要为后台服务撰写API.刚开始接触的时候,并没有考虑太多,就想提供URL,服务端通过该URL进行查询.创建.更新等操作即可.但再对相关规范进行了解后,才发现,API的设计并没有那么简单,远 ...
- BeanDefinition到Bean
转自:http://songzi0206.iteye.com/blog/1430239 当 BeanDefinition 注册完毕以后, Spring Bean 工厂就可以随时根据需要进行实例化了.对 ...
- Java @Repeatable
查看@PropertySource注解时候,发现了@Repeatable,从来没见过的注解,学习了下: 首先介绍下@Repeatable注解: JDK1.8出现的,作用是解决一个类上不能标注重复的注解 ...
- 并发编程——ConcurrentHashMap#transfer() 扩容逐行分析
前言 ConcurrentHashMap 是并发中的重中之重,也是最常用的数据结果,之前的文章中,我们介绍了 putVal 方法.并发编程之 ConcurrentHashMap(JDK 1.8) pu ...
- [转]论magento1和magento2的速度性能优化问题
本文转自:http://www.360magento.com/blog/magento-speed-up/ magento从2007年发展至今,也经历了十余年的磨练,如今也迎来了magento的换代产 ...
- [日常] Go语言圣经--作用域,基础数据类型,整型
go语言圣经-作用域 1.一个声明语句将程序中的实体和一个名字关联,比如一个函数或一个变量 2.一个变量的生命周期是指程序运行时变量存在的有效时间段;声明语句的作用域对应的是一个源代码的文本区域,它是 ...
- Linux-mkdosfs格式化磁盘命令(15)
名称:mkdosfs 使用:mkdosfs [块设备名称] 说明: 将一个块设备格式化为DOS磁盘类型 例: mkdosfs /dev/memblock //将memblock块设备格式化为dos磁盘 ...
- oracle逐步学习总结之权限和角色(基础六)
原创作品,转自请注明出处:https://www.cnblogs.com/sunshine5683/p/10236129.html 继续上节的索引,这次主要总结oracle数据库的权限问题!(在总结的 ...
- HDU1698(KB7-E 线段树)
Just a Hook Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...