1、查看所有的数据库
show databases;
2、创建自己的数据库
create databases 数据库名;
#创建atigjjsn数据库,该名称不能与已经存在的数据库重名。
create database atigjjsn;
3、使用自己的数据库
use 数据库名;
#使用atigjjsn数据库
use atigjjsn;
说明:如果没有使用use语句,后面针对数据库的操作也没有加“数据名”的限定,那么会报错no database selected
使用完use语句后,如果接下来的SQL都是针对一个数据库操作的,那就不用重复使用use了,如果要针对另外一个数据库操作,那么重新使用use。
4、查看某个库的所有表格
show tables; #要求前面有use语句
show tables from 数据库名;
5、创建新的表格
create table 表名称(
字段名 数据类型,
字段名 数据类型);
说明:如果是最后一段字段,后面就用加逗号,因为逗号的作用是分割每个字段。
#创建学生表
create table student(
id int,
name varchar(20) #说名字最长不超过20个字符
);
6、查看一个表的数据
select * from 数据库表名称;
#查看学生数据表的数据
select * from student;
7、添加一条记录
insert into 表名称 values(值列表);
#添加两条记录到student表中、
insert into student values(1,'张三');
insert into student values(1,'李四');
8.0不会报错,5.7版本报错,字符集问题。
8、查看表的创建信息
show create table 表名称\G
#查看student表的详细创建信息
show create table student\G
9、查看数据库的创建信息
show create database 数据库名\G
#查看atigjjsn数据库的详细创建信息
show create database atigjjsn\G
10、删除表格
drop table 表名称;
#删除学生表
drop table student;
11、删除数据库
drop database 数据库名称;
#删除atigjjsn数据库
drop database atigjjsn;