Dos命令操作数据库。
1.查看数据库的指令 show databases; “;”分号是数据库的结束符,没有加分号 即使按回车 也代表这个语句没有结束
如下:mysql> show databases; ? 出现结果: +--------------------+ ? | Database ??????????| ? +--------------------+ ? | information_schema | ? | d1 ????????????????| ? | mysql ?????????????| | performance_schema | | test ??????????????| | y2 ????????????????| +--------------------+ 6 rows in set (0.00 sec) 2.?创建数据库?create database 数据库名; 如下:mysql> create database y1; 出现结果: Query OK,1 row affected (0.00 sec) (进行查看是否出现:) +--------------------+ | Database ??????????| +--------------------+ | information_schema | | mysql ?????????????| | performance_schema | | test ??????????????| | y1?????????????????| +--------------------+ 3.删除数据库的指令 ?drop database 数据库名; 如下:mysql> drop database y2; 出现结果: Query OK,0 rows affected (0.00 sec) (进行查看是结果:) mysql> show databases; +--------------------+ | Database ??????????| +--------------------+ | information_schema | | mysql ?????????????| | performance_schema | | test ??????????????| +--------------------+ 4 rows in set (0.00 sec) 2.?进入某一个数据库 中 use 数据库名; 如下:mysql> use d1; 出现结果: Database changed 5.对数据表增删查改 查看数据表?show tables; 如下:mysql> create table t2(id int,name int); 出现结果:Query OK,0 rows affected (0.46 sec) 4..创建表 Create table 表名(字段1 类型,字段2 类型); 如下:create table t1(id int,0 rows affected (0.46 sec) (进行查看结果:) mysql> show tables; +--------------+ | Tables_in_d1 | +--------------+ | t1 ??????????| +--------------+ 1 row in set (0.00 sec) 5.修改表名 Alter table 表名 rename 新表名; 如下:mysql> alter table t1 rename t3; 出现结果:Query OK,0 rows affected (0.01 sec) (进行查看结果:) mysql> show tables; +--------------+ | Tables_in_d1 | +--------------+ | t3 ??????????| +--------------+ 2 rows in set (0.00 sec) 6.进入表里对字段进行操作 查看表的定义?desc 表名; ? 如下:mysql> desc t2; ? 出现结果: ? +-------+---------+------+-----+---------+-------+ ? | Field | Type ???| Null | Key | Default | Extra | ? +-------+---------+------+-----+---------+-------+ ? | id ???| int(11) | YES ?| ????| NULL ???| ??????| ? | name ?| int(11) | YES ?| ????| NULL ???| ??????| ? | age ??| int(11) | YES ?| ????| NULL ???| ??????| ? +-------+---------+------+-----+---------+-------+ 7.添加字段 字段定义 ?字段名和字段类型 都要写 alter table 表名 add 字段定义; 如下:mysql> alter table t2 add age int; 出现结果: Query OK,0 rows affected (0.17 sec) Records: 0 ?Duplicates: 0 ?Warnings: 0 8.删除字段 Alter table 表名 drop 字段名; 如下:mysql> alter table t2 add age int; 出现结果: Query OK,0 rows affected (0.26 sec) Records: 0 ?Duplicates: 0 ?Warnings: 0 ? (进行查看结果:) mysql> desc t2; +-------+---------+------+-----+---------+-------+ | Field | Type ???| Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | id ???| int(11) | YES ?| ????| NULL ???| ??????| | name ?| int(11) | YES ?| ????| NULL ???| ??????| +-------+---------+------+-----+---------+-------+ 2 rows in set (0.01 sec) 9.修改字段 Alter table 表名 change 旧的字段名 ?字段定义; 如下:mysql> alter table t2 change name age char; 出现结果: Query OK,0 rows affected (0.52 sec) Records: 0 ?Duplicates: 0 ?Warnings: 0 进行查看结果:) mysql> desc t2; +-------+---------+------+-----+---------+-------+ | Field | Type ???| Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | id ???| int(11) | YES ?| ????| NULL ???| ??????| | age ??| char(1) | YES ?| ????| NULL ???| ??????| +-------+---------+------+-----+---------+-------+ 2 rows in set (0.01 sec) 10.修改字段类型 如下:mysql> alter table t2 modify age int; 出现结果: Query OK,0 rows affected (0.42 sec) Records: 0 ?Duplicates: 0 ?Warnings: 0 (进行查看结果:) mysql> desc t2; +-------+---------+------+-----+---------+-------+ | Field | Type ???| Null | Key | Default | Extra | +-------+---------+------+-----+---------+-------+ | id ???| int(11) | YES ?| ????| NULL ???| ??????| | age ??| int(11) | YES ?| ????| NULL ???| ??????| +-------+---------+------+-----+---------+-------+ 2 rows in set (0.01 sec) 11.给字段添加数据(记录) 添加一条记录 Insert into 表名(id,age) value(值1,值2); 如下:mysql> insert into t3(id,name) value(1,111); (mysql> insert into t3(id,name) value(2,222);) (mysql> insert into t3(id,name) value(3,333);) 出现结果:Query OK,1 row affected (0.00 sec) (进行查看结果:) mysql> select * from t3; +------+------+------+ | id ??| name | age ?| +------+------+------+ | ???1 | ?111 | NULL | | ???2 | ?222 | NULL | | ???3 | ?333 | NULL | +------+------+------+ 3 rows in set (0.00 sec) 12.多条记录添加 Insert into 表名 values(值1,值2),(值1,值2),(值1,值2); 如下:mysql>insert into t5 values(1,20),(2,21),(3,26); 出现结果: Query OK,3 rows affected (0.00 sec) Records: 3 ?Duplicates: 0 ?Warnings: 0 查看记录 Select * from 表名; 查看所有的字段记录 Select id from 表名; 查看单个的字段记录 Select id,age from 表名;查看多个字段的记录 mysql> select * from t4; +------+------+ | id ??| age?| +------+------+ | ???1 | ??20 | | ???2 | ??21 | | ???3 | ??26 | +------+------+ 3 rows in set (0.00 sec) 按条件查询 Select * from 表名 where 条件 条件表达式 ?> < >= <= = != ???and且 ?or 或 如下:mysql> select * from t5 where age>30; (mysql> select * from t5 where age>30 and age<50;) ? 出现结果: ? +------+------+ ? | id ??| age ?| ? +------+------+ ? | ???4 | ??56 | ? | ???5 | ??62 | ? | ???8 | ??54 | ? | ???9 | ??34 | ? | ??10 | ??36 | ? +------+------+ ? 5 rows in set (0.00 sec) ? (出现结果: ? +------+------+ ? | id ??| age ?| ? +------+------+ ? | ???9 | ??34 | ? | ??10 | ??36 | ? +------+------+ ? 2 rows in set (0.10 sec) ? ) ? ? ? ? ?
排序查询 Select * from 表名 order by 字段名 [asc/desc] Asc 由低到高 ?desc 由高到底 如下:mysql> select * from t5 order by age?desc; ? ? 出现结果; ? +------+------+ ? | id ??| age ?| ? +------+------+ ? | ???5 | ??62 | ? | ???4 | ??56 | ? | ???8 | ??54 | ? | ??10 | ??36 | ? | ???9 | ??34 | ? | ???3 | ??26 | ? | ???2 | ??21 | ? | ???1 | ??20 | ? +------+------+ ? 8 rows in set (0.10 sec) ?
限制查询 ? Select * from 表名 limit 2,5;从第二个开始向后查询五个 ? 如下:mysql> select * from t5 limit 2,5; 出现结果; +------+------+ | id ??| age ?| +------+------+ | ???3 | ??26 | | ???4 | ??56 | | ???5 | ??62 | | ???8 | ??54 | | ???9 | ??34 | +------+------+ 5 rows in set (0.00 sec) 如下:mysql> select * from t5 limit 5; +------+------+ | id ??| age ?| +------+------+ | ???1 | ??20 | | ???2 | ??21 | | ???3 | ??26 | | ???4 | ??56 | | ???5 | ??62 | +------+------+ 5 rows in set (0.00 sec) 删除记录 删除所有 Delete from 表名 按条件删 ?delete from 表名 where 条件表达式; 如下:mysql> delete from t5 where id=4; 出现结果: Query OK,1 row affected (0.13 sec) (查看输出结果) mysql> select * from t5; +------+------+ | id ??| age ?| +------+------+ | ???1 | ??20 | | ???2 | ??21 | | ???3 | ??26 | | ???5 | ??62 | | ???8 | ??54 | | ???9 | ??34 | | ??10 | ??36 | +------+------+ ? 7 rows in set (0.00 sec) 改数据 Update 表名 set 字段=值; 如果不带条件 会把字段下面的记录全改 如下:mysql> select * from t5; 出现结果: Query OK,7 rows affected (0.10 sec) Rows matched: 7 ?Changed: 7 ?Warnings: 0 (查看输出结果) mysql> select * from t5; +------+------+ | id ??| age ?| +------+------+ | ???1 | ??12 | | ???2 | ??12 | | ???3 | ??12 | | ???5 | ??12 | | ???8 | ??12 | | ???9 | ??12 | | ??10 | ??12 | +------+------+ 7 rows in set (0.00 sec) 按条件更新 ? 如下:mysql> update t5 set age=56 where id=6; ? 出现结果: ? Query OK,0 rows affected (0.00 sec) ? Rows matched: 0 ?Changed: 0 ?Warnings: 0 ? (查看输出结果) ? mysql> select * from t5; ? +------+------+ ? | id ??| age ?| ? +------+------+ ? | ???1 | ??12 | ? | ???2 | ??12 | ? | ???3 | ??12 | ? | ???5 | ??12 | ? | ???8 | ??12 | ? | ???9 | ??12 | ? | ??10 | ??12 | ? +------+------+ ? 7 rows in set (0.00 sec) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- Windows API一日一练 7 ShowWindow函数
- windows-7 – 在Windows 7中是否有完全支持Tortoise SVN(包
- windows-phone-7 – 如何确定Windows Phone 7的方向?
- 我可以在Microsoft Hyper-v虚拟机中运行Android Studio(And
- [windows Tools] windows程序打包工具-Advanced Installer
- 如何在WIndows 7上使用INETMGR连接到远程IIS
- 将cygwin主目录设置为Windows的“用户配置文件”
- UE4链接第三方库(lib和dll)
- windows-7 – 无法从命令行使用telnet获取http响应
- windows-xp – Windows XP中的任务计划程序是否足够可靠,可
- 有没有快速的方法来创建文件的Windows快捷方式,而
- windows-server-2008-r2 – Windows Server文件共
- Windows Perl行编辑器具有可编辑的默认值?
- windows – (UWP)从assets文件夹获取xml文件
- windows-7 – cygwin tmux在Windows 7上 – 为什
- winapi – Windows SDK 7.0的signtool中的signwi
- windows – 如何针对流行的病毒扫描程序测试我的
- .net – 对于由Microsoft创建的类,“1”或“2”后
- windows-8 – 将本地HTML加载到WebView中
- winapi – 检测在Windows 7下连接或删除的外部显