zoukankan      html  css  js  c++  java
  • sql语句

    数据库就是数据的仓库,1、将数据保存到文件或内存;2、接收特定的命令然后对文件进行相应的操作。

    对数据库的操作分为1、数据库;2、数据表;3、数据行操作

    1、数据库操作

    显示有哪些数据库:

    SHOW DATABASES;

    创建数据库:

    CREATE DATABASE 数据库名称 DEFAULT CHARSET utf8 COLLATE utf8_general_ci;

    CREATE DATABASE 数据库名称 DEFAULT CHARACTER SET gbk COLLATE gbk_chinese_ci;

    进入具体某一数据库:

    USE 数据库名;

    2、数据表操作(对数据表的操作都带table,只有show tables为复数,因为查看的是所有有的表,所以为复数)1、创建create;2、删除drop;3、查看show;4、修改alter。创建、删除、修改;所有单数table都带上表名。

    创建表:

    create table 表名(
        列名  类型  是否可以为空,
        列名  类型  是否可以为空
    )ENGINE=InnoDB DEFAULT CHARSET=utf8
    复制代码
    CREATE TABLE `tab1` (
      `nid` int(11) NOT NULL auto_increment,                   # not null表示不能为空,auto_increment表示自增
      `name` varchar(255) DEFAULT aa,                 # default 表示默认值
      `email` varchar(255),
      PRIMARY KEY (`nid`)                                      # 把nid列设置成主键
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    复制代码

    查看表:

    show tables;      #查看数据库中有哪些表

    删除表

    drop table 表名

    修改表(对表结构的更改)

    对表中列的操作:添加列,删除列,修改列

    alter table 表名 add 列名 类型                       #添加
    alter table 表名 drop column 列名                                #删除
    alter table 表名 modify column 列名 类型;              #对列类型的修改
    alter table 表名 change 原列名 新列名 类型;    #对列名和类型的修改

    对主键的操作:

    alter table 表名 add primary key(列名);   # 添加主键
    alter table 表名 drop primary key;   #删除主键
    alter table 表名  modify  列名 int, drop primary key;   #删除主键

    对外键的操作:

    alter table 从表 add constraint 外键名称(形如:FK_从表_主表) foreign key 从表(外键字段) references 主表(主键字段);     # 添加外键
    alter table 表名 drop foreign key 外键名称   #删除外键

    对默认值的操作:修改默认值,删除默认值

    ALTER TABLE 表名 ALTER 列名 SET DEFAULT 10;     #修改默认值
    ALTER TABLE 表名 ALTER 列名 DROP DEFAULT;         #删除默认值

    3、数据行(数据表内容的操作)

    表内容的增删改查

    (1)增

    增加表内容的三种形式

    insert into 表 (列名,列名...) values (值,值,...)
    insert into 表 (列名,列名...) values (值,值,...),(值,值,值...)
    insert into 表 (列名,列名...) select (列名,列名...) from 表

    (2)删除表内容

    delete from 表                                      # 删除表里全部数据
    delete from 表 where nid=1  # 删除nid =1 那一行数据

    (3)更改表内容

    update 表 set name = 'zjs' where id=1

    (4)查询表内容

    一般查询

    select * from 表
    select * from 表 where nid > 10
    select nid,name,age as num from 表 where id > 10

    条件查询

        select * from 表 where nid > 10 and name != 'haha';
        select * from 表 where nid between 13 and 18;
        select * from 表 where nid in (10,20,30)
        select * from 表 where id not in (1,2,3)
        select * from 表 where nid in (select nid from 表)

    通配符like

        select * from 表 where name like 'aa%'  # aa开头的所有(多个字符串)
        select * from 表 where name like 'aa_'  # aa开头的所有(一个字符)

    限制limit

        select * from 表 limit 6;            - 前6行
        select * from 表 limit 3,6;          - 从第3行开始的6行
        select * from 表 limit 6 offset 3    - 从第3行开始的6行

    查询内容后的升降序

        select * from 表 order by 列 asc              - 根据 “列” 从小到大排列
        select * from 表 order by 列 desc             - 根据 “列” 从大到小排列
        select * from 表 order by 列1 desc,列2 asc    - 先根据 “列1” 从大到小排列,相同则再按列2从小到大排序

    查询内容后分组

        select num from 表 group by num
        select num,nid from 表 group by num,nid
        select num,nid from 表  where nid > 10 group by num,nid order by nid desc
        select num,nid,count(*),sum(score),max(score),min(score) from 表 group by num,nid
        select num from 表 group by num having max(id) > 10

    group by 必须在where之后,order by之前

    其他操作:

    查询数据库支持的存储引擎:

    show engines;

    查看表使用的引擎和其他信息:

    show table status from db_name where name='table_name';
    show create table table_name;

    查看建表语句:

    show create table table_name;

    修改表的引擎:(上述内容中的修改表差不多,都是修改嘛!!)

    alter table table_name engine=innodb;

    关闭mysql服务:

    net stop mysql

    开启mysql服务

    net start mysql

    mysql的初始化文件my.ini

    找到default-storage-engine=INNODB 改为default-storage-engine=MYISAM能够修改默认引擎。

  • 相关阅读:
    1144 The Missing Number (20分)
    1145 Hashing
    1146 Topological Order (25分)
    1147 Heaps (30分)
    1148 Werewolf
    1149 Dangerous Goods Packaging (25分)
    TypeReference
    Supervisor安装与配置()二
    谷粒商城ES调用(十九)
    Found interface org.elasticsearch.common.bytes.BytesReference, but class was expected
  • 原文地址:https://www.cnblogs.com/zjsthunder/p/9840241.html
Copyright © 2011-2022 走看看