继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

MySQL的基本语句1

冷月谷
关注TA
已关注
手记 5
粉丝 1
获赞 21
sql语句不区分大小写,语句以英文分号结尾;(下文中,大写部分为非必写内容)

一、数据库相关
1.查看所有数据库:show databases;
2.创建数据库:
create database 数据库名 CHARACTER SET UTF8/GBK;
如:create database db1 character set gbk;
如:create database db2;

3.查看指定数据库:
show create database 数据库名;
如:show create database db1;

4.删除数据库:
drop database 数据库名;
如:drop database db2;

5.使用指定数据库:(只有使用了数据库,才能建表,不然谁知道你要把表建在哪个数据库)
use 数据库名;
如:use db1;

二、表相关
1.查看所有表:show tables;
2.创建表:
create table 表名(字段1名 字段1类型,字段2名 字段2类型,... ...) ENGINE=INNODB/MYISAM CHARSET=UTF8/GBK;
如:create table student(id int, name varchar(20),age int);
如:create table emp(id int, name varchar(20),age int)engine=innodb charset=gbk;

3.查看指定表:
show create table 表名;
如:show create table student;

4.查询表字段信息:
desc 表名;
如:desc student;

5.修改表名:
rename table 原表名 to 新表名;
如:rename table student to teacher;

6.修改引擎(engine)和字符集
alter table 表名 engine=innodb/myisam charset=utf8/gbk;
如:alter table emp engine=myisam charset=utf8;
(也可只修改一项)
如:alter table emp charset=gbk;

7.删除表:
drop table 表名;
如:drop table emp;

三、字段相关(我觉得字段相关是最难记的)
(字段,就是teacher表中name,id,age这些东东,desc teacher可查看teacher表的字段信息)

1.添加字段:add。
(1)alter table 表名 add 字段名 字段类型;(默认添加到最后面)
如:alter table teacher add gender varchar(10);
(2)alter table 表名 add 字段名 字段类型 first;(添加到最前面)
如:不举例了
(3)alter table 表名 add 字段名 字段类型 after 已有字段名;(添加到已有字段名的后面)
如:alter table teacher add score double after id;(在id后面添加一个score字段)

2.删除字段:drop。
alter table 表名 drop 字段名;
如:alter table teacher drop score;

3.修改字段
(1)修改字段名和类型:change。
alter table 表名 change 原字段名 新字段名 新字段类型;
如:alter table teacher change age height double;
(2)修改字段类型和位置:modify。
alter table 表名 modify 字段名 新类型 first/after 已有字段名;
如:alter table teacher modify height int first;(最前面)
如:alter table teacher modify height int after name;(name后面)
如:alter table teacher modify height int;(最后面)
打开App,阅读手记
2人推荐
发表评论
随时随地看视频慕课网APP

热门评论

哇,写得真好!哇,写得真好!

查看全部评论