忘记密码
 新成员注册
查看: 28|回复: 3

[SQL] MySQL数据库操作

[复制链接]
我的人缘1
发起活动
0 次
参加活动
0 次
王帝权 发表于 2019-10-16 12:34 | 显示全部楼层 |阅读模式

你还没有登录,登录后可以看到更多精彩内容

您需要 登录 才可以下载或查看,没有账号?新成员注册

×
数据库操作
显示所有的数据库
mysql> show databases;(注意:最后有个 s)

创建数据库
mysql> create database test;

连接数据库
mysql> use test;

查看当前使用的数据库
mysql> select database();

当前数据库包含的表信息
mysql> show tables; (注意:最后有个 s)

删除数据库
mysql> drop database test;

表操作
备注:操作之前使用“use <数据库名>”应连接某个数据库。
建表
命令:create table <表名> (<字段名 1> <类型 1> [,..<字段名 n> <类型 n>]);
例子:
mysql> create table MyClass(
> id int(4) not null primary key auto_increment,
> name char(20) not null,
> sex int(4) not null default '0',
> degree double(16,2));

获取表结构
命令: desc 表名,或者show columns from 表名
例子:
mysql> describe MyClass
mysql> desc MyClass;
mysql> show columns from MyClass;

删除表
命令:drop table <表名>
例如:删除表名为 MyClass 的表
mysql> drop table MyClass;

插入数据
命令:insert into <表名> [( <字段名 1>[,..<字段名 n > ])] values ( 值 1 )[, ( 值 n )]
例子:
mysql> insert into MyClass values(1,'Tom',96.45),(2,'Joan',82.99), (2,'Wang', 96.59);

查询表中的数据
查询所有行
mysql> select * from MyClass;

查询前几行数据
例如:查看表 MyClass 中前 2 行数据
mysql> select * from MyClass order by id limit 0,2;
或者
mysql> select * from MyClass limit 0,2;

删除表中数据
命令:delete from 表名 where 表达式
例如:删除表 MyClass 中编号为 1 的记录
mysql> delete from MyClass where id=1;

修改表中数据
命令:update 表名 set 字段=新值,... where 条件
mysql> update MyClass set name='Mary' where id=1;

在表中增加字段
命令:alter table 表名 add 字段 类型 其他;
例如:在表 MyClass 中添加了一个字段 passtest,类型为 int(4),默认值为 0
mysql> alter table MyClass add passtest int(4) default '0'

更改表名
命令:rename table 原表名 to 新表名;
例如:在表 MyClass 名字更改为 YouClass
mysql> rename table MyClass to YouClass;

更新字段内容
命令:update 表名 set 字段名 = 新内容
update 表名 set 字段名 = replace(字段名, '旧内容', '新内容');
例如:文章前面加入 4 个空格
update article set content=concat('    ', content);
踩过的脚印
充实自我,展现自我!没有完美的个人,只有优秀的团队!计协我的家,建设靠大家!只有真正的融入了,你才会体会快乐哦~
我的人缘2
发起活动
0 次
参加活动
0 次
韦雅惠 发表于 2019-10-26 22:22 | 显示全部楼层
这些代码,太难了。作为一个菜鸟。一个也看不懂
充实自我,展现自我!没有完美的个人,只有优秀的团队!计协我的家,建设靠大家!只有真正的融入了,你才会体会快乐哦~
我的人缘1
发起活动
0 次
参加活动
0 次
蔡显聪 发表于 2019-11-17 12:23 | 显示全部楼层
学到了,好人一生平安
充实自我,展现自我!没有完美的个人,只有优秀的团队!计协我的家,建设靠大家!只有真正的融入了,你才会体会快乐哦~
我的人缘0
发起活动
0 次
参加活动
0 次
宋春雨 发表于 2019-11-17 12:30 | 显示全部楼层
优秀                                          
充实自我,展现自我!没有完美的个人,只有优秀的团队!计协我的家,建设靠大家!只有真正的融入了,你才会体会快乐哦~
您需要登录后才可以回帖 登录 | 新成员注册

本版积分规则

QQ|手机版|CA之家 ( 桂ICP备07006672号-6 )

GMT+8, 2025-9-16 05:54

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表