mysql数据库、表、索引、触发器

ctrl +c退出sql程序
1、数据库
查看数据库:show databases;
创建数据库:create database tb_name;
删除数据库:drop database db_name;
mysql> drop database affair;
Query OK, 2 rows affected (0.29 sec)
2、表:
查看数据库中所有的表:show tables;
查看表的结构:desc|describe table_name;
创建表:create table table_name(字段 字段类型 完整性约束,...);
完整性约束:非空、唯一约束、自增长、默认值、主键约束、外键约束;
创建表时创建外键:(需要有关联的父表)
create table  mytable(
id int primarykey auto_increment not null,
name varchar(34) not null default 'youname',
constraint  fk(外键别名) foreign key( id) references  mytable0(id));
修改表:
修改表名:alter table table_name rename to new_table_name;;
修改字段:alter table table_name change 字段名 新字段名 新属性 新约束 before|after 某一字段;
增加字段:alter table table_name add 字段名 属性 约束 before|after 某一字段;
字段重新排序:alter table table_name modify 字段x before |after 字段y;

删除表:drop table table_name;
创建索引:
create table index1(
id int,
name varchar(34),
index index_1(id)
);
索引分为:
普通索引:index
唯一索引:unique index
单列索引:index(id)
多列索引:index(id,name)
全文索引:fulltext index(name) (char \varchar\text 类型)
空间索引:spatial index

使用alter 创建索引:alter table mytable add unique index index_2(id);
使用create在已存在的表中创建索引:create index index_3(id) on mytable(id);

触发器:
触发器就是事先编写一个程序,使得对一个表操作的同时也能对另一个表操作;

同一时间对同一事件只能有一个触发器;
创建触发器:
create trigger trigger_name before|after 触发事件(insert |update|delete)
on table_name [for each row ]
begin(多条sql语句时使用begin、end)
sql 语句(对某一表的操作update|delete|insert)
end

查看触发器:
查看触发器名:select trigger_name from information_schema.triggers;
查看触发器的信息:select * from information_schema.triggers where trigger_name=your_trigger_name;
查看所有触发器:show triggers;

删除触发器:drop trigger trigger_name;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值