数据库的创建
1.创建数据库
- 创建一个名为hellword的数据库
create database hellword;
- 创建一个使用utf8字符集的 db2 数据库
create database db2 charset=utf;
- 创建一个使用utf字符集,并带校对规则的 db3 数据库
create database db3 charset=utf8 collate utf8_general_ci;
2.使用数据库
- use+库名
eg:use hellword;
3.创建一个名为student的数据库表
create table student(
id int,
name varchar(32),
gender varchar(2)
);
4.向表中插入数据
insert into student (id, name, gender) values (1, '张三', '男');
insert into student (id, name, gender) values (2, '李四', '女');
insert into student (id, name, gender) values (3, '王五', '男');
5.查看相关
查看系统默认字符集以及校验
- show variables like 'character_set_database';
- show variables like 'collation_database';
查看数据库支持的字符集校验规则
- show collation;
查看数据表详细
select * from 表名
- select * from student;
查看数据表结构
describe 表名
- describe students;
查看当前数据库所有的表
- show tables;
查看所有的数据库
- show databases;
数据库的修改
1.修改表中的数据
update 表名 set 列名=新值 where 列名=旧值;
源数据表
修改student表中的一列数据
- update student set id=2;
修改某行某列数据
- update student set id=8 where name='李四';
2.删除数据库
drop database 库名
- drop database hellword
3.删除数据库中的表
drop table 表名
- drop table student
4.修改数据库中的表名
rename tables 旧表名 to 新表名
- rename tables student to students;
5.修改数据表数据
<1>alter table 表名
删除students表
- alter table students;
<2>alter table 表名 drop 列名;
删除student表中的gender列
- alter table students drop gender;
<3>delete from 表名 where 条件(列名=值);
删除student表中id小于8的数据
- delete from students where id<7;
删除表中的数据,可以指定条件,如果不指定条件,则删除该表的所有数据.