mysql的基本操作

时间:2022-09-16 20:51:01

mysql的基本操作

连接数据库

  作为一名程序员首先要有较高的逼格,所以下面的一切操作用cmd来执行

mysql的基本操作

找到mysql.exe的路径,然后链接数据库

D:\wamp\bin\mysql\mysql5.6.17\bin>dir
驱动器 D 中的卷是 软件
卷的序列号是
0004-12AF

D:\wamp\bin\mysql\mysql5.6.17\bin 的目录

2016/04/16 12:51 <DIR> .
2016/04/16 12:51 <DIR> ..
2014/05/01 14:38 134,656 echo.exe
2014/05/01 14:38 4,067,840 innochecksum.exe
2014/05/01 14:38 4,523,008 myisamchk.exe
2014/05/01 14:38 4,306,432 myisamlog.exe
2014/05/01 14:38 4,431,360 myisampack.exe
2014/05/01 14:38 4,398,592 myisam_ftdump.exe
2014/05/01 14:38 4,856,320 mysql.exe
2014/05/01 14:38 4,745,728 mysqladmin.exe
2014/05/01 14:38 4,912,640 mysqlbinlog.exe
2014/05/01 14:38 4,739,072 mysqlcheck.exe
2014/05/01 14:38 32,940,032 mysqld-debug.exe
2014/05/01 14:38 12,942,848 mysqld.exe
2014/05/01 14:38 4,813,312 mysqldump.exe
2014/05/01 14:38 7,635 mysqldumpslow.pl
2014/05/01 14:38 27,732 mysqld_multi.pl
2014/05/01 14:38 36,010 mysqlhotcopy.pl
2014/05/01 14:38 4,732,928 mysqlimport.exe
2014/05/01 14:38 4,732,416 mysqlshow.exe
2014/05/01 14:38 4,759,040 mysqlslap.exe
2014/05/01 14:38 5,038,592 mysqltest.exe
2014/05/01 14:38 13,902,336 mysqltest_embedded.exe
2014/05/01 14:38 5,230,080 mysql_client_test.exe
2014/05/01 14:38 14,152,192 mysql_client_test_embedded.exe
2014/05/01 14:38 9,079 mysql_config.pl
2014/05/01 14:38 4,532,736 mysql_config_editor.exe
2014/05/01 14:38 4,504 mysql_convert_table_format.pl
2014/05/01 14:38 13,757,952 mysql_embedded.exe
2014/05/01 14:38 4,078,080 mysql_plugin.exe
2014/05/01 14:38 10,518 mysql_secure_installation.pl
2014/05/01 14:38 3,955,200 mysql_tzinfo_to_sql.exe
2014/05/01 14:38 4,227,072 mysql_upgrade.exe
2014/05/01 14:38 4,059,136 my_print_defaults.exe
2014/05/01 14:38 4,192,768 perror.exe
2014/05/01 14:38 3,970,560 replace.exe
2014/05/01 14:38 4,057,088 resolveip.exe
35 个文件 191,285,494 字节
2 个目录 39,392,387,072 可用字节

连接数据库,输入密码:

D:\wamp\bin\mysql\mysql5.6.17\bin>mysql -uroot -p
Enter password: ********
Welcome to the MySQL
monitor. Commands end with ; or \g.
Your MySQL connection id is 24
Server
version: 5.6.17 MySQL Community Server (GPL)

Copyright (c)
2000, 2014, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation
and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type
'help;' or '\h' for help. Type '\c' to clear the current input statement.

现在已经进入数据库了,来看一下下面有哪些数据库:show databases;(所有的mysql操作命令后面都要加‘;’)

mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| game |
| mysql |
| performance_schema |
| test |
+--------------------+
5 rows in set (0.00 sec)

一共有五个数据库,5行,下面进行增删改数据库的操作:

创建demo数据库

mysql> create database demo;
Query OK,
1 row affected (0.01 sec)

进入demo数据库:

mysql> use demo;
Database changed

在demo里创建一个user表:

mysql> create table user(id int,name varchar(25),age int(2));
Query OK,
0 rows affected (0.06 sec)

在user表中插入数据:

mysql> insert into user(id,name,age) values(1,'xiaoming',20);
Query OK,
1 row affected (0.09 sec)

查看user表中数据:

mysql> select * from user;
+------+----------+------+
| id | name | age |
+------+----------+------+
| 1 | xiaoming | 20 |
+------+----------+------+
1 row in set (0.00 sec)

删除表:

mysql> drop table user;
Query OK,
0 rows affected (0.03 sec)

删除数据库:

mysql> drop database demo;
Query OK,
0 rows affected (0.01 sec)

创建表的连贯操作:

mysql> create table user(
-> id int(11) unsigned auto_increment primary key,
-> name varchar(30) not null default '',
-> age int(2) not null default '0')engine=innodb DEFAULT CHARSET=utf8;
Query OK,
0 rows affected (0.06 sec)

下面提供一下基本的操作命令供参考:

    查看mysql版本
mysql
> \s
查看数据库
mysql
> show databases;
创建数据库
mysql
> create database mytest;
查看数据库字符编码
mysql
> show create database mytest;
删除数据库
mysql
> drop database mytest;
切换数据库
mysql
> use game;
查看表
mysql
> show tables;
创建一张表
create table 表名(字段1 属性,字段2 属性);
mysql
> create table user(id int,name varchar(25),age int(2));
查看表结构
mysql
> desc user;
查看表数据
mysql
> select * from user;
插入表数据
insert into 表名(字段1,字段2,字段3) values(值1,值2,值3);
mysql
> insert into user(id,name,age) values(1,'xiaoming',18);
修改表数据
update 表名 set 字段1 ='字段1值',字段2='字段2值' where 主键字段=''
mysql
> update user set name='lisi' whert id=1;
删除表数据
delete from 表名称 where (主键) 字段='字段值';
删除表
mysql
> drop table user;
创建表的连贯操作
mysql
> create table user(
-> id int(11) unsigned auto_increment primary key,
-> name varchar(30) not null default '',
-> age int(2) not null default '0')engine=innodb DEFAULT CHARSET=UTF8;
mysql
> show create table user;

表的字段属性:

1.unsigned 无符号,全是整数
2.zerefill0填充,int(5)不够5位补0
3.auto_increment自增长
4.null这一列允许为null
5.not null这一列不允许为空
6.default默认值