1.创建数据库:
create database db1;
create database db2 charset=utf8;
craete database db3 charset=gbk;
2.展示所有数据库
show databases;
3.查询数据库信息
-格式:show create database 数据库名;
-举例:
show create database db1;
show create database db2;
show create database db3;
4.删除数据库
-格式:drop database 数据库名;
-举例:
drop database db1;
drop database db2;
5.使用数据库
-在执行表相关和数据库相关的SQL语句时,需要先使用表。
-格式:use 数据库名;
举例:
use db3;
二、表相关
1.执行表相关的SQL之前必须使用了某个数据库
create database newdb1 charset=utf8;
use newdb1;
2.创建表
-格式:create table 名字(字段名1 类型,字段名2 类型)charset=utf8;
-举例:
create table person(name varchar(50),age int)charset=utf8;
create table student(name varchar(50),chinses int,math int,english int);
3.查询所有表
-格式:show databases;
3.查询表信息
-格式:show create table 表名;
举例:
show create table person;
4.查询表字段
-格式:desc 表名;
举例:
desc student;
查看表的字符集:show create table hero;
5.删除表
-格式:drop table 表名;
举例:
drop table student;
6.修改表名
-格式:rename table 原名 to 新名;
举例:
rename table person to per;
1.添加表字段
-create database newdb2;
use newdb2;
create table emp(name varchar(30));
-最后面添加格式:alter table emp add 字段名 类型;
-最前面添加格式:alter table emp add 字段名 类型 after 字段名;
举例:
alter table emp add age int;
alter table emp add id int first;
alter table emp add gender varchar(10) after name;
2.删除表字段
-格式:alter table 表名 drop 字段名;
举例:
alter table emp drop gender;
alter table emp drop id;
3.修改字段
格式:alter table 表名 change 原名 新名 新类型;
举例:
alter table emp change age gender varchar(10);
三、数据相关
create database mydb5 charset=utf8;
use mydb5;
create table person(name varchar(10),age int);
1.插入数据
-全表插入:inert into person 表名 values(值1,值2);
指定字段插入格式:insert into 表名(字段名1,字段名2) values(值1,值2);
举例:
insert into person values("tom",56);
insert into person(name) values("jerry");
-批量插入格式
全表插入格式:insert into 表名 values values(值1,值2),values(值1,值2)values(值1,值2);
指定字段插入格式:insert into 表名(字段名1,字段名2) values(值1,值2),(值1,值2);
举例:
insert into person values("lucikly",24);
insert into person(name) values("hanmeimei"),("lihua");
中文问题:
insert into person values("刘德华",50);
-执行上面SQL语句报错的话,执行以下指令:
set name
2.查询数据
格式:select 字段信息 from 表名 where 条件;
举例:
select name from person;
select name,age from person;
select * from person;
select * from person where age>30;
select age from person where name="lihua";
3.修改数据
-格式:update 表名 set 字段名=值,字段名=值 where 条件;
举例:
update person set name='汤姆' where name="张三";
update person set name='杰瑞',age=14 where name="刘德华";
update person set age=10 where age is null;