教程集 www.jiaochengji.com
教程集 >  数据库  >  mysql  >  正文 mysql中alter、update、insert、delete、show语句的例子

mysql中alter、update、insert、delete、show语句的例子

发布时间:2016-03-08   编辑:jiaochengji.com
本文分享下,mysql数据库中的常用语句alter、update、insert、delete、show的例子,供大家学习参考。

本节内容:
alter、update、insert、delete、show语句的用法举例。

一,alter table命令
用来改变数据表的许多设计细节,如添加或删除一些数据列,改变数据列的属性,定义和删除各种索引等。

1,增加数据列
 

复制代码 代码示例:
alter table tblname add newcolname coltype coloptions

2,修改数据列
alter table tblname change oldcolname newcolname coltype coloptions
例如:

复制代码 代码示例:
alter table table1 change id id auto_increment

说明列没有改名,也也可实现改名

3,删除数据列
 

复制代码 代码示例:
alter table tblname drop colname

4,增加索引
 

复制代码 代码示例:
alter table tblname add primary key (indexcols)
alter table tblname add index [indexname] (indexcols)
alter table tblname add unique [indexname] (indexcols)

5,添加外键约束条件
 

复制代码 代码示例:
alter table tblname add foreign key [indexname] (column1) references table2 (column2)

6,删除索引
 

复制代码 代码示例:
alter table tblname drop primary key
alter table tblname drop index indexname
alter table tblname drop foreign key indexname

二,update命令用来修改数据库里现有的数据记录
1,where限定的update语句
 

复制代码 代码示例:
update tablename
set column1=value1,column2=value2
where columnN=value

2,不带where限定的update对整个数据表做修改
 

复制代码 代码示例:
update titles set  year=2005
update titles set price=price*1.05

3,编辑排列清单里的数据记录
 

复制代码 代码示例:
update tablename set mydata=0 order by name limit 10

4,更新关联数据表里的数据记录
 

复制代码 代码示例:
update table1,table2
set table1.columnA = table2.columnB
where table1.table1ID = table2.table1ID

三,insert命令可以向表中插入数据
1,一条命令插入多条数据记录
 

复制代码 代码示例:
insert into table (columnA columnB columnC)
values('a',1,2),('b',12,13),('c',22,33),

四,delete命令用于删除表中记录
delete from titles where titleID=8//因为删除肯定是删除一行记录,所以delete后不需要加*

1,删除关联记录
 

复制代码 代码示例:
delete t1,t2 from t1,t2,t3 where condition1 and condition2

2,输出排序清单里的数据记录
 

复制代码 代码示例:
delete from authors order by ts desc limit 1

五,show命令,查看原数据:
 

复制代码 代码示例:
show databases
show tables from dbname
show [full] columns from tablename //返回全部数据列的详细信息
show index from tablename

您可能感兴趣的文章:
mysql中alter、update、insert、delete、show语句的例子
mysql权限管理教程详解
mysql中添加列、修改列、删除列的方法
mysql insert的操作分享(DELAYED、IGNORE、ON DUPLICATE KEY UPDATE )
Mysql 自增字段设定基值的sql语句
mysql创建用户与授权(实例)
mysql外键约束使用详解
ubuntu下mysql常用命令大全
mysql设置自动清理binlog日志的方法
mysql alter语句的综合示例

[关闭]
~ ~