MySQL常用命令

MySQL常用命令

数据库相关SQL

连接 MySQL

    格式: mysql [-h主机地址] -u用户名 -p用户密码;

         mysql -uroot -proot;
         mysql -h110.110.110.110 -uroot -proot;

查看所有数据库

    show databases;

查看单个数据库

    格式: show create database 数据库名称;
         show create database name;

创建数据库

    格式: create database 数据库名称;
         create database name;

创建指定字符集数据库

    格式: create database 数据库名称 character set 格式;
         create database name character set utf8;

删除数据库

    格式: drop database 数据库名称;
         drop database name;

使用数据库_进入数据库

    格式: use 数据库名称;
        use name;

数据表相关SQL

创建表

    格式: create table 表名(字段1 类型,字段2 类型,=) engine=引擎 charset=字符集;
         create table person(name varchar(10),age int)engine=myisam charset=编码;

创建表(主键)

    create table t1(id int primary key,age int);

创建表(主键 自增)

    create table t2(id int primary key auto_increment,age int);
    // 自增约束通常和主键约束同时使用,此时主键可以赋值为null,每张表只能有一个字段为自增字段

创建表(非空 )

    create table t3(id int primary key auto_increment, age int not null);

创建表(注释)

    create table t5(id int comment '用户的id',age int comment '用户的年龄');
    //show create table t5;     

查询数据表(引擎和字符集)

    格式: show create table 表名; //查询单个表
         show create table person;

查看表字段详细(表内容)

    格式: desc 表名;
         desc person;

数据表的引擎

    -innoDB: 默认,支持数据库的高级操作,包括事务 主 外键等
    -myisam: 只具备基本的数据存储功能

修改表(对已经创建好的表进行修改)

修改表名称

    格式: rename table 原名 to 新名;
         rename table name to newName

修改属性(引擎和字符集)

    格式: alter table 表名 engine=引擎 charset=字符集;   // innoDB and myisam
         alter table t_stu engine=innodb charset=utf8;

添加字段

    -在最后添加
        格式:alter table 表名 add 字段 int;

    -在最前添加
        格式:alter table 表名 add 字段 int first;

    -在某个字段后面添加
        格式:alter table 表名 add 字段 int after 字段;

修改字段名 [类型]

    格式: alter table 表名 change 原字段名 新字段名 [新类型];
         alter table person change age money [int];

修改字段类型 [位置]

    格式: alter table 表名 modify 字段名 类型 [位置(first/after 字段)]

         alter table person modify name int first;
         alter table person modify name int after id;

删除字段

    格式: alter table 表名 drop 字段名;
         alter table person drop name;

练习

1. 创建t_hero表引擎为myisam  字段为 id 和 name

2. 修改表名为 hero

3. 修改引擎为innodb

4. 在 id后面添加age字段类型为varchar(10)

5. 修改age字段名称为myAge 类型为int

6. 修改myAge 字段 放在最后面

7. 删除myAge字段

答案

    create table t_hero(id int,name varchar(10)) engine=myisam;
    rename table t_hero to hero;
    alter table hero engine=innodb;
    alter table hero add age varchar(10) after id;
    alter table hero change age myAge int;
    alter table hero modify myAge int after name;
    alter table hero drop myAge;

数据相关SQL

    *注 创建一个数据表,完成以下的测试
        create table hero(id int,name varchar(10),age int);

添加语句(插入语句)

    格式: insert into 表名 values(值1,值2,值3);//全表插入
         insert into hero values(1,'悟空',502);

    格式: insert into 表名 (字段1名,字段2名) values(值1,值2) //指定字段插入
         insert into hero (name) values ('八戒');

批量添加语句(插入语句)

    全表插入: insert into hero values(11,'美国队长',34),(12,'钢铁侠',43),(13,'蜘蛛侠',23),(14,'雷神',55);

    指定字段插入: insert into hero (name) values('张三'),('李四'),('王五');

查询语句

    格式: select * from 表名;   // " * "代表所有字段
         select * from hero;

    格式:select 字段 from 表名;   
         select name from hero;

    格式:select 字段1,字段2from 表;
         select name,age from hero;

修改语句(更新语句)

    格式: update 表名 set 新值 where 条件;  // 不加where条件则对整表数据操作
         update hero set age=25 where id=1;.

删除语句

    格式: delete from 表名 where 条件;    //不带条件 则删除整表数据 留下空表
         delete from hero where name='李四';

truncate(清空表 删除表.)

    truncate table hero;//先删除表再创建一个一样的空表

    delete from hero;//只是删除表里面的数据

    drop table hero;//删除表


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值