- 表格格式:
行称为记录;列称为字段,关键字为字段名
- 表的基本操作
1、创建表(指定字符集)
create table 表名(
字段名 数据类型,
字段名 数据类型,
...
字段名 数据类型
) [character set utf8];
2、查看已有表的字符集
show create table 表名;
show tables; 查看库拥有的表
3、查看表结构
desc 表名; (以类似excel格式)
4、删除表
drop table 表名;
注意:
(1)所有的数据都是以文件的形式存放在数据库目录下
(2)Ubuntu数据目录:/var /lib /mysql /
- 表的记录管理
1 插入(insert)
1、insert into 表名 values(值1),(值2),(值3),(值4),..;
2、指定插入的字段,未插入字段为NULL
insert into 表名(字段1,...) values(值1),...;
2 查询(select)
1、查看满足条件的所有字段
select * from 表名 [where 条件];
2、查看满足条件的指定字段
select 字段名1,字段名2 from 表名 [where 条件]
- 练习:
1、查看所有的库
show databases;
2、创建新库 studb
create database studb;
use studb;
3、在studb中创建表tab1 指定字符集utf8,字段名有id、name、age
create table tab1(id int,
name char(20),
age int) character set utf8;
4、查看tab1的表结构
desc tab1;
5、在tab1中随便插入2条记录
insert into tab1 values(0,"Jordan",31),
(1,"Lebron",19),(2,"Kobe",21);
6、在tab1中的name、age两个字段插入2条记录
insert into tab1(name,age) values("kyli",16),
("Stephen",16);
7、查看tab1中的所有记录
select * from tab1;
8、查看tab1表中所有人的姓名和年龄
select name,age from tab1;
9、查看tab1表中年龄大于20的信息
select * from tab1 where age > 20;
- 如何更改默认字符集为utf8
1、方法(通过更改MySQL配置文件)
2、步骤:
1、获取root权限
su
2、找到配置文件的目录
cd /etc/mysql/mysql.conf.d/
3、备份
cp mysqld.conf mysqld.cnf.bak
4、修改配置
subl mysqld.cnf
character_set_server = utf8
5、重启mysql服务
service mysql restart
或者:/etc/init.q/mysql restart
问题:客户端把数据存储到数据库服务器上的过程
1、连接到数据服务库
mysql -u root -p
2、选择一个库
use 库名
3、创建表/修改表
update 表名....
4、断开与数据库连接
exit;|quit;|\q;
- 表字段的操作
1、语法:alter table 表名 执行动作;
2、添加字段
alter table 表名 add 字段名 数据类型 [first];
alter table 表名 add 字段名 数据类型 [after 字段名];
(默认添加到最后)
3、删除字段drop
alter table 表名 drop 字段名;
4、修改数据类型modify
alter table 表名 modify 字段名 新数据类型;
5、表重命名rename
alter table 表名 rename 新表名;
6、表字段的重命名change
alter table 表名 change 旧字段名 新字段名 数据类型;
- 练习:
create database studb2;
use studb2
create table t1(name char(15),
age tinyint unsigned,
phnumber char(11)
) character set utf8;
desc t1;
alter table t1 add id bigint first;
alter table t1 modify phnumber bigint;
alter table t1 add address char(15);
alter table t1 drop age;
desc t1;
- 表记录管理
1、删除表记录
1、delelte from 表名 where 条件;
eg:delete from hero where name = "魏延";
2、注意
delete语句后如果不加where条件,所有记录全部清空
2、更新表记录
1、update 表名 set 字段1=值1,字段2=值2,...where 条件;
update hero set name='司马懿',id = 8
where name = '曹操';
2、注意
update set语句不加where语句会将字段值全部设置
练习:
select * from hero where country = "蜀国";
select name,sex,country from hero where sex = "女";
update hero set name="典韦",sex="男",country="魏国"
where id = 2;
delete from hero where country = "蜀国";
update hero set country = "魏国" where name ="貂蝉";
delete from hero;
- 表的赋值
1、语法
create table 表名 select ... from 表名 where 条件;
2、示例
1、复制MOSHOU.sanguo表的全部记录和字段,sanguo2
create table sanguo2
select * from MOSHOU.sanguo;
2、复制MOSHOU.sanguo表的前三条记录,sanguo3
create table sanguo3
select * from MOSHOU.sanguo
limit 3;
3、复制MOSHOU.sanguo表的id,name,country,前5条记录sanguo4
create table sanguo3
select id,name,country from MOSHOU.sanguo
limit 5;
3、复制表结构
create table 表名 select * from 表名 where false;
1、示例,MOSHOU.sanguo复制到sanguo5
create table sanguo5
select * from MOSHOU.sanguo where false;
4、注意
复制表的时候不会把原表的键(key)属性复制过来