1.查询所有数据库
格式:show databases;
2.创建数据库
格式:create database 数据库名 charset=utf8/gbk;
举例 create database db1;
create database db2 charset=utf8;
create database db3 charset=gbk;
show databases;
3.查询数据库
格式:show create database 数据库名;
举例:show create database db1;
4.删除数据库
格式:drop database 数据库名;
举例:drop database db3;
show databases;
5.使用数据库
使用完数据库之后再执行相关的数据SQL 否则会报错:No database selected
格式:use 数据库名;
举例:use db1;
表相关的SQL执行表相关的SQL必须使用了某个数据库否则会报错
1.创建表
格式:create table 表名(字段1名 类型, 字段2名 类型) charset=utf8/gbk;
举例:create table person(name varchar(50),age int);
create table car(name varchar(50),type varchar(5),price int);
create table student(name varchar(50),age int,chinese int,math int,english int)charset=gbk;
2.查询所有表
格式:show tables;
3.查询表信息
格式:show create table 表名;
举例:show create table person;
show create table student;
4.查询表字段信息
格式:desc 表名;
5.删除表
格式:drop table 表名;
举例:drop table car;
show tables;
6.修改表名
格式:rename table 原名 to 新名;
7.添加表字段
最后面添加格式:alter table 表名 add 字段名 类型;
最前面添加格式:alter table 表明 add 字段名 类型 first;
在xxx字段的后面添加:alter table 表名 add 字段名 类型 after xxx;
举例:creater database db4;
use db4;
creater table teacher(name varchar(20));
alter table teacher add age int;
alter table teacher add id int first;
alter table teacher add salary int after name;
8.删除表字段
格式:alter table 表名 drop 字段名;
alter table teacher change age salary int;
数据相关SQL
执行数据相关的SQL语句必须使用了某个数据库并且已经创建好了保存数据的表
create database db5 charset=utf8;
use db5;
create table person(name varchar(50),age int);
1.插入数据
全表格插入格式:insert into 表名 values(值1,值2);
指定字段插入格式:insert into 表名(字段1名,字段2名) values (值1,值2);
举例:insert into person values("tom",30);
insert into person(name) values("jerry");
批量插入:insert into person values("liubei",20),("guanyu",18),("zhangfei",15);
insert into person(name) values('libai'),('liucangsong');
插入中文:insert into person values("刘德华",17);
如果执行上面的SQL语句报一下错误,执行 set names gbk; 之后再次执行
2.查询数据
格式:select 字段信息 from 表名 where 条件;
举例:select name from person;
select name,age from person;
select * from person;
select * from person where age>20;
select age from person where name='tom';
select name from person where age=15;
3.修改数据
格式:update 表名 set 字段名=值,字段名=值 where 条件;
举例:update person set age=50 where name="刘备";
update person set name=" liubei" where age=20;
4.删除数据
格式:delete from 表名 where 条件;
举例:delete from person where name="刘德华";
delete from person where age<20;
delete from person where age is null;