mysql 常用操作

来源:互联网 发布:斗鱼手机客户端无网络 编辑:程序博客网 时间:2024/06/06 22:31

建表:

names表

create table if not exists names(    id int primary key not null, //id为主键且不为空    name varchar(20),    nick_name varchar(10) not null); 

friendship表

create table if not exists membership(    id int primary key not null auto_increment,    friend varchar(20),    id int,    foreign key (id) references names(id)//将names的主键作为friendship的外键关联); 

修改

添加一行数据

//由于id是auto_increment,可以由系统自动生成//第一个括号是所要修改的列名,第二个括号里是对应列要赋的值insert into names (nick_name, name) values('测试','王钢弹');

添加列

alter table add column new_column varchar(20);

删除一行数据

//where 限定符,指定删除 id等于2 的一行delete from friendship where id=2;

更新一个数据

//将id等于1的那行的friend列改为“小明”update friendship set friend='小明' where id=1;

修改列属性

//将names表的id列改为自动递增,即添加行时,不用指定id值,由系统自动添加,且与前一行有递增关系#alter table names modify id int auto_increment;

删除表

//注意与其他表有约束关系的情况drop table friendship;

删除列

alter table friendship drop column id;

修改表名

alter table friendship rename to  friend;

修改列名

alter table friendship change id new_id int;

更新中…

0 0
原创粉丝点击