最近正在了解mysql的知识。将其记录在博客上,方便以后对其进行查阅和更改。 如果有不合理以及错误之处,欢迎指正。 notice: mysql环境中的命令,都是用分号作为命令的结尾 在使用数据库之前,需要启动sql服务: # service mysqld start Now, Let's begin! 1 启动 1.1 刚开始安装的mysql是没有密码的,在终端输入就可以启动 # mysql 1.2 添加用户和密码 mysql -u 用户名 password 新密码 # mysql -u root password helloworld 1.3 修改密码: # mysqladmin -u 用户名 -p password 新密码 输入旧密码之后就会完成更改 2 显示数据库列表,通常中会有多个database # show databases; mysql> show databases; +--------------------+ | Database | +--------------------+ | information_schema | | mysql | +--------------------+ 3 显示指定库中的数据表 # use mysql; //打开名为mysql的数据库 # show tables; //显示数据库中的表 +---------------------------+ | Tables_in_mysql | +---------------------------+ | columns_priv | | db | | event | | func | | general_log | | help_category | | help_keyword | | help_relation | | help_topic | | host | | ndb_binlog_index | | plugin | | proc | | procs_priv | | servers | | slow_log | | tables_priv | | time_zone | | time_zone_leap_second | | time_zone_name | | time_zone_transition | | time_zone_transition_type | | user | +---------------------------+ 23 rows in set (0.00 sec) 4 显示数据表的结构 describe 表名; # describe user; //显示名为user的表的结构 5 显示表中的记录 # select * from 表名; 6 创建一个数据库 create database 数据库名; # create database school; //创建一个名为school的数据库 # show databases; //查看数据库是否创建 +--------------------+ | Database | +--------------------+ | information_schema | | mysql | | school | | test | +--------------------+ 4 rows in set (0.00 sec) 7 在指定的数据库中创建表 7.1 创建 # use mysql; //打开名为mysql的数据库 create table 表名(字段设定列表) # create table student (id int(3) auto_increment not null primary key, name char(10),sex char(6),age int(3)); 建立表name,表中有id(序号,自动增长),name(姓名),xb(性别),年龄 (出生年月)四个字段 类型后面的数字表示占用的位数,如int(3),这列数字为占用3个字节,24位; char(10),varchar(10)表示占用字符个数为10; 7.2 查看建立表格的结构: describe name; # describe student; +-------+----------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------+----------+------+-----+---------+----------------+ | id | int(3) | NO | PRI | NULL | auto_increment | | name | char(10) | YES | | NULL | | | sex | char(6) | YES | | NULL | | | age | int(3) | YES | | NULL | | +-------+----------+------+-----+---------+----------------+ 4 rows in set (0.00 sec) 7.3 增加几条记录; # insert into student values('','tony','male','23'); # insert into student values('','Jone','male','22'); 7.4 使用select命令验证结果; # select * from student; mysql> select * from student; +----+------+--------+------+ | id | name | sex | age | +----+------+--------+------+ | 1 | tony | male | 23 | | 2 | Jone | female | 22 | +----+------+--------+------+ 2 rows in set (0.00 sec) 7.5 修改记录 where 后天的是条件 # update student set name='Tony' where age=23; 7.6 删除记录 # delete from student where name='Jone'; 8 将表中记录清空 # delete from 表名; 9 删库和删表 # drop database 库名; # drop table 表名; 10 修改数据库结构 10.1 增减字段 alter table 表名 add column <字段名><字段选项> # alter table student add column birth char; //给表student增加一个名为birth的列。 mysql> select * from student; +----+------+------+------+-------+ | id | name | sex | age | birth | +----+------+------+------+-------+ | 3 | Tony | male | 23 | NULL | +----+------+------+------+-------+ 1 row in set (0.00 sec) 10.2 修改字段 alter table 表名 change <旧字段名><新字段名><选项> # alter table school change birth born char; mysql> select * from student; +----+------+------+------+------+ | id | name | sex | age | born | +----+------+------+------+------+ | 3 | Tony | male | 23 | NULL | +----+------+------+------+------+ 1 row in set (0.00 sec) 10.3 删除字段 alter table 表名 drop column <字段名> # alter table student drop column birth; 11 数据库备份与导入 11.1 备份 # mysqldump -u root -p school > hello.sql 11.2 导入 # mysql -u root -p school < hello.sql