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
);

最新文章

  1. 【Java每日一题】20161230
  2. iOS开发——高级技术&本地化与国际化详解
  3. CDN技术分享
  4. .NET深入实战系列--EF到底怎么写过滤条件(转)
  5. 8款唯美设计的HTML5/CSS3应用
  6. cocos2d-x 常规库的图文件配置
  7. 教你50招提升ASP.NET性能(五):确保分页是在数据层完成的
  8. bzoj 1069 [SCOI2007]最大土地面积(旋转卡壳)
  9. JS代码获取当前日期时支持IE,不兼容FF和chrome,解决这个问题,我们需要把获取时间的getYear()函数换成getFullYear()
  10. rsync常用参数详解
  11. 自学HTML5第一天(认识HTML5的全局属性)
  12. Linux 安装xtrabackup的依赖问题
  13. android 读写sd卡的权限设置
  14. Json.Net系列教程 4.Linq To JSON
  15. linux动态库编译和使用
  16. 粒子滤波(PF:Particle Filter)
  17. postman Installation has failed: There was an error while installing the application. Check the setup log for more information and contact the author
  18. C#如何生成JSON字符串提交给接口(服务器)
  19. Exp2 后门原理与实践 20164303 景圣
  20. WPF 使用Console.Write打印信息到控制台窗口中

热门文章

  1. PHP之高性能I/O框架:Libevent(二)
  2. SVN安装后,右键不显示SVN菜单项
  3. postgreSql 常用查询总结
  4. python3.X出现关于模块(i18n)的不能使用的解决方法
  5. 七台机器部署Hadoop2.6.5高可用集群
  6. 原本在滴滴只负责批100万元以上开支的Leslie决定,ofo所有30万元以上的开支都要由她亲自过目。那段时间,他“天天晚上12点才下班,眼睛都熬红了”
  7. MVC的部分视图(Partial View)
  8. PHP错误集锦
  9. Vue计算属性的用法
  10. 基于std::mutex std::lock_guard std::condition_variable 和std::async实现的简单同步队列