简介:MySql是一种开源的关系型数据库,本文主要为MySQL基础知识的总结,MySQL的基础知识很多,这里介绍的是常用的知识,通过实际操作进行检验。详细内容请参考下文。
一、登陆MySQL数据库
执行指令# mysql –u root -p

二、Mysql数据库的创建和修改
1、创建一个名称为mydb001的数据库
create database mydb001;
show databases;

2、创建一个使用utf-8字符集的mydb002数据库
create database mydb002 character set utf8;
show databases;

3、创建一个使用utf-8字符集,并带校对规则的mydb003数据库
create database mydb003 character set utf8 collate utf8_general_ci;
show databases;

4、查看前面创建的mydb002数据库的定义信息
show create database mydb002;

5、删除前面创建的mydb001数据库
drop database mydb001;

6、查看服务器中的数据库,并把其中某一个库的字符集修改为gb2312
alter database mydb002 character set gb2312;
show create database mydb002;

三、Mysql数据库创建表并对表结构进行修改
1、创建一个员工表
use mydb002;
create table employee
(
id int,
name varchar(40),
sex varchar(4),
birthday date,
entry_date date,
job varchar(40),
salary decimal(8,2),
resume text
);
show tables; 查看库的所有表

show create table employee; 查看表的创建细节
desc employee; 看表结构

2、在上面员工表的基本上增加一个image列
alter table employee add image blob;

3、修改job列,使其长度为60
alter table employee modify job varchar(60);

4、删除sex列
alter table employee drop sex;

5、表名改为user
rename table employee to user;

6、修改表的字符集为utf-8
alter table user character set utf8;

7、列名name修改为username
alter table user change column name username varchar(40);

8、删除表
drop table user;

四、Mysql数据表插入数据
1、使用insert语句向表中插入三个员工的信息。
(插入的字符和日期类型数据应该加上单引号)
insert into employee(id,name,birthday,entry_date,job,salary,resume) values(1,'aaa','2015-09-29','1980-09-09','bbb',90,'aaaaa');
select * from employee;

2、插入数据的细节1(可以不用指明字段,只要插入的值和表的字段完全匹配就行)
insert into employee values(2, 'bbb', null,'1980-09-09','1980-09-09','bbb',90,'bbbbb');

3、插入数据的细节2(可以把每个插入的字段值都加上单引号,mysql拿到数据以后会自动去转换成相应的类型)
insert into employee values('3','ccc', 'null','1990-09-09','1990-09-09','ccc','00','ccccc');

4、插入数据的细节3(插入中文)
要告诉mysql客户端采用gb2312编码
show variables like 'chara%';
set character_set_client=gb2312;
insert into employee(id, name) values('4','李四');

要想查看时不乱码
show variables like 'chara%';
set character_set_results=gb2312;
select * from employee;
五、Mysql数据表更新数据
1、将所有员工薪水修改为5000元。
update employee set salary=5000;

2、将姓名为’bbb’的员工薪水修改为3000元。
update employee set salary=3000 where name='bbb';

3、将姓名为’bbb的员工薪水修改为4000元,job改为engineer。
update employee set salary=4000,job='engineer' where name='bbb';

4、将bbb的薪水在原有基础上增加1000元。
update employee set salary=salary+1000 where name='bbb';

六、Mysql数据表删除表中的记录
1、删除表中名称为’bbb’的记录。
delete from employee where name='bbb';

2、删除表中所有记录。
delete from employee;
3、使用truncate删除表中记录。
truncate table employee;
4、delete 和truncate table的区别
delete是把表中的记录一条一条地删除,truncate是摧毁表结构,再重建表结构。 |
|