zoukankan      html  css  js  c++  java
  • mysql学习

    最近关注尚学堂的学习视频,关于bbs的一个项目学习,开始重新整理mysql的学习内容和知识点,

    在实际运用中把mysql的灵魂发挥到实质!

    首先看下bbs的数据库创建代码:

    bbs.sql文件

     1     create database bbs;   
     2        
     3     use bbs;   
     4        
     5     create table article    
     6     (   
     7     id int primary key auto_increment,   
     8     pid int,   
     9     rootid int,   
    10     title varchar(255),   
    11     cont text,   
    12     pdate datetime,   
    13     isleaf int #1-not leaf 0-leaf   
    14     );   
    15        
    16     insert into article values (null, 0, 1, '蚂蚁大战大象', '蚂蚁大战大象', now(), 1);   
    17     insert into article values (null, 1, 1, '大象被打趴下了', '大象被打趴下了',now(), 1);   
    18     insert into article values (null, 2, 1, '蚂蚁也不好过','蚂蚁也不好过', now(), 0);   
    19     insert into article values (null, 2, 1, '瞎说', '瞎说', now(), 1);   
    20     insert into article values (null, 4, 1, '没有瞎说', '没有瞎说', now(), 0);   
    21     insert into article values (null, 1, 1, '怎么可能', '怎么可能', now(), 1);   
    22     insert into article values (null, 6, 1, '怎么没有可能', '怎么没有可能', now(), 0);   
    23     insert into article values (null, 6, 1, '可能性是很大的', '可能性是很大的', now(), 0);   
    24     insert into article values (null, 2, 1, '大象进医院了', '大象进医院了', now(), 1);   
    25     insert into article values (null, 9, 1, '护士是蚂蚁', '护士是蚂蚁', now(), 0);  

    1.show create table 表名--查看创建表语句

    2.show engines;--查看数据库表引擎

    3.导出mysql中数据库文件:

    (1)cmd 进入一个目录文件夹eg:E:/workspace/sqlfile

    (2)输入:mysqldump -u用户名 -p密码 数据库名>数据库文件名(eg:sql.sql)

    查看数据表结构的三个方法(试过了)

    查看mysql表结构的方法有三种:
    1、desc tablename;
    例如:
    要查看jos_modules表结构的命令:
    desc jos_modules;
    查看结果:
    mysql> desc jos_modules;
    +------------------+---------------------+------+-----+---------------------+----------------+
    | Field | Type | Null | Key | Default | Extra |
    +------------------+---------------------+------+-----+---------------------+----------------+
    | id | int(11) | NO | PRI | NULL | auto_increment |
    | title | text | NO | | NULL | |
    | content | text | NO | | NULL | |
    | ordering | int(11) | NO | | 0 | |
    | position | varchar(50) | YES | | NULL | |
    | checked_out | int(11) unsigned | NO | | 0 | |
    | checked_out_time | datetime | NO | | 0000-00-00 00:00:00 | |
    | published | tinyint(1) | NO | MUL | 0 | |
    | module | varchar(50) | YES | MUL | NULL | |
    | numnews | int(11) | NO | | 0 | |
    | access | tinyint(3) unsigned | NO | | 0 | |
    | showtitle | tinyint(3) unsigned | NO | | 1 | |
    | params | text | NO | | NULL | |
    | iscore | tinyint(4) | NO | | 0 | |
    | client_id | tinyint(4) | NO | | 0 | |
    | control | text | NO | | NULL | |
    +------------------+---------------------+------+-----+---------------------+----------------+
    2、show create table tablename;
    例如:
    要查看jos_modules表结构的命令:
    show create table jos_modules;
    查看结果:
    mysql> show create table jos_modules;
    jos_modules | CREATE TABLE `jos_modules` (
    `id` int(11) NOT NULL AUTO_INCREMENT,
    `title` text NOT NULL,
    `content` text NOT NULL,
    `ordering` int(11) NOT NULL DEFAULT '0',
    `position` varchar(50) DEFAULT NULL,
    `checked_out` int(11) unsigned NOT NULL DEFAULT '0',
    `checked_out_time` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
    `published` tinyint(1) NOT NULL DEFAULT '0',
    `module` varchar(50) DEFAULT NULL,
    `numnews` int(11) NOT NULL DEFAULT '0',
    `access` tinyint(3) unsigned NOT NULL DEFAULT '0',
    `showtitle` tinyint(3) unsigned NOT NULL DEFAULT '1',
    `params` text NOT NULL,
    `iscore` tinyint(4) NOT NULL DEFAULT '0',
    `client_id` tinyint(4) NOT NULL DEFAULT '0',
    `control` text NOT NULL,
    PRIMARY KEY (`id`),
    KEY `published` (`published`,`access`),
    KEY `newsfeeds` (`module`,`published`)
    ) ENGINE=MyISAM AUTO_INCREMENT=145 DEFAULT CHARSET=utf8
    3、use information_schema;select * from columns where table_name='tablename'
    例如:
    要查看jos_modules表结构的命令:
    use information_schema;
    select * from columns where table_name='jos_modules';
    查看结果:
    略。
    如果要查看怎么建立数据表的命令用第二种方法最佳。

    操作表属性:

    SQL ALTER TABLE 语法

    如需在表中添加列,请使用下列语法:

    ALTER TABLE table_name
    ADD column_name datatype
    

    要删除表中的列,请使用下列语法:

    ALTER TABLE table_name 
    DROP COLUMN column_name
    

    注释:某些数据库系统不允许这种在数据库表中删除列的方式 (DROP COLUMN column_name)。

    要改变表中列的数据类型,请使用下列语法:

    ALTER TABLE table_name
    ALTER COLUMN column_name datatype

    删除所有表数据:
    delete from tablename;
    delte from tablename where 1=1;
    删除特定条件下的数据:
    delete from tablename where ...;

    修改表中数据记录的某个属性值:
    update tablename set 属性=XXX where 1=1;





    mysql 如何将一列为非主键属性设置为主键属性?

    ALERT TABLE tab_name ADD PRIMARY KEY (index_col_name,...);



    为属性为自动编号,类型为tinyint添加自增属性

    alter table t2 modify 自动编号 tinyint unsigned auto_increment;

    delete   from   blur_article    where   id   not   in(select   min(id)   from  blur_article    group   by   title)报错
    错误消息:
    #1093 - You can't specify target table 'blur_article' for update in FROM clause 
    mysql中不能这么用。 (等待mysql升级吧)
    错误提示就是说,不能先select出同一表中的某些值,再update这个表(在同一语句中)

    替换方案:
    create table tmp as select min(id) as col1 from blur_article group by title;
    delete from blur_article where id not in (select col1 from tmp);
    drop table tmp;

    一个叫team的表,里面只有一个字段name,一共有4条纪录,分别是a,b,c,d,对应四个球对,现在四个球对进行比赛,用一条sql语句显示所有可能的比赛组合.
    你先按你自己的想法做一下,看结果有我的这个简单吗?

    答:select a.name, b.name 
    from team a, team b 
    where a.name < b.name

  • 相关阅读:
    Java 访问权限字节码
    vscode 写js项目,自动按照eslint保存
    mac 按键符号说明
    下载git单个文件或目录
    生成密码的方式
    git 笔记
    后端数据返回的snake_case格式,但前端的规范为驼峰格式,实现一种snake_case转驼峰的方法
    wireshark 数据协议解析
    MAC App破解之路十一 charles
    生成检查的顺子的表
  • 原文地址:https://www.cnblogs.com/burns/p/3724422.html
Copyright © 2011-2022 走看看