MySQL的库指令和表指令,列指令

一,库指令

1,创建库 :create database 库名 charset = utf8 ;
 注:不输入charset = utf8 可能无法在库中书写中文
2,查看当前操作系统的所有数据库 : show databases;
3,使用库 : use 库名;
4,查看当前使用的库 :select database();
5,删除库 :drop database 库名;
注:不要删除系统自带的数据库

二,表指令

1,展示当前库中所有的表:show tables; 

2,创建表:crate table 表名(列名 类型 约束,列名 类型 约束 ...);

3,删除表:drop table 表名;

4,查看表内详细结构: desc table 表名;

5,改名:alter table 表名 rename 新表名;

6,在表内添加列:alter table 表名 add 列名 类型 约束

7,在表内删除列: alter table 表名 drop 列名;

8,替换原列: alter table 表名 change 原列名 新列名 类型 约束;

注:新列名可以和原列名一样

三,列指令

查:

1,查看表下方所有列的内容:select  * from 表名;
2,查询部分列: select  列名1,列名2....  from 表名;
3,通过as给与列别名 :select  列名1as 表名1,列名2 as 别名2....   from  表名;
注:只改变输出时的列名 数据库内不改变
4,去重: select distinct 列名 from 表名;
5,升序 : select * from 表名 order by 列名 asc;
6,降序:select from 表名 order by 列名 desc;
7,分组根据列名相同的放在一起:select  * from 表名 group by  列名;
8,条件分组所有满足条件的放一组,其余不管:select id from user group by age having id > 20;
9,查看单前用户: select uaer();
10,查看当前数据库版本:select version();
11,查看当前日期 :select current_date();
12,查看当前时间:select current_ime();
13,查看标准时间:select utc_time;
14,查看当前日期时间:select current_timestamp;

添:

1,添加列全部的内容: insert into 表名 values(id,name ,age....),(id2,name2,age2....)
注:有多少列接要写多少值
2,添加列部分内容: insert into 表名 (id,name) values (id,name),(id2,name2);
注:有默认值可以不写

改:

1,修改指定列的全部值:update 表名 set 列名 = 值
注:可以在后面添加条件语句指定删除部分值

删:

1,删除指定列中全部的值:delete from 表名 列名; 
注:可以在后面添加条件语句指定修改部分值

条件语句

1,查询 ,删除, 修改 都可以用有条件语句 , 如果没有则针对整个表
2,where 哪里语句(相当于if满足执行不满足执行)
3,比较:  where id = 101  where id > 1   where < 10
4 ,并且(同时满足): where id >1 and id < 10
5, 或者(满足一个即可)where id >10 or id < 2
6,范围 从a 到 b 【a ~b】:where between a and b

7, 选项中有就执行操作 :where  int(1,2,3)
      选项中有就不执行操作 not int(1,2,3)
8, lile  模糊查询
        %   匹配多个
                例:select name from user where like '王%'
                查询王开头的名字
        _    匹配一个 
              例:select name from user where  like  '王_'
               王开头两个字的名字
四,类型

1,整数:int

2,字节长:varchar(20)

五,约束
    1,主键约束:primary key(列名)

    注:每个表中必须有主键 主键必须唯一,能够确定唯一一行
    2,非空约束: not null
    3,自增长:auto_increment
    4,唯一约束 不能重复:unique
    5,默认值:default
    6,外键是另外一个表的主键 :foreign key(列名)  references  表名(主键名)
         外键权限 :on update cascade 级联,同步更新  
                            on delete  cascade 级联,同步删除

                            on update restrict 拒绝同步更新  
                            on delete  restrict 拒绝同步删除

      例:create table orders(id int not null auto_increment, uid int not null, gid int not null , num int not null, primary key(id),                   foreign key(uid) references user(id) on delete cascade on update cascade, foreign key (gid) references goods(id) on delete cascade on update cascade

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值