MySql速成

MySql学习

安装与可视化

  1. 在根文件夹下创建my.ini
  2. 数据库的安装自己去csdn找
  3. 可视化工具在U盘,网盘,系统盘,H盘都有
[client]
# 设置mysql客户端默认字符集
default-character-set=utf8
 
[mysqld]
# 设置3306端口
port = 3306
# 设置mysql的安装目录
basedir=C:\\web\\mysql-8.0.11
# 设置 mysql数据库的数据的存放目录,MySQL 8+ 不需要以下配置,系统自己生成即可,否则有可能报错
# datadir=C:\\web\\sqldata
# 允许最大连接数
max_connections=20
# 服务端使用的字符集默认为8比特编码的latin1字符集
character-set-server=utf8
# 创建新表时将使用的默认存储引擎
default-storage-engine=INNODB

正文

  1. 注释: – 或/**/

  2. 所有的命令都用分号结尾

  3. 本笔记中所有命令带中括号就是可选,大括号就是必选, 但是在实际操作中不带括号

  4. 好习惯:名称用``来进行包裹才可以,防止特殊字符引起转义,

    其中`是tab键上方的键

  5. 关键字用``来, 字符串用""

  6. 结构层次: databases-> use->table->字段

  7. 不等于号: != 或 <>

  8. --登录:
    --在cmd窗口打开进入操作 
    mysql -uroot -pS19001883
    show databases --查看所有数据库
    show tabels --查看所有表
    use database_name  --进行切换数据库
    describe table_name --显示数据库的所有信息(不包括内容)
    
    --------------------------------------------------------------
    create database [if not exists]  名称
    --增加数据库
    drop database [if exist] 名称
    --删除数据库
    use `table_name`
    --使用数据库
    show `name`
    --查看数据库
    show table status  --总览数据库状态
    -----------------------------------------------------------
    --常用命令
    desc student  --显示表的结构
    show create database try  --查看定义数据库的语句
    show create table student  --查看定义表的语句
    
    
    
  9. 数据类型

    1. 数字: int float double decimal

    2. 字符串: char 0-255 varchar 0-65535 tinytext 2^8-1 text 2^16-1

    3. 时间日期: datetime YYYY-MM-DD HH: MM : SS (年月日时分秒)

      timestamp 1970.1.1 到现在的毫秒数

    4. nul

  10. 创建表

    --创建表
    create table if not exists `表名`(
        '字段名' 列类型 [属性] [索引] [注释],
        '字段名' 列类型 [属性] [索引] [注释],
        .....
        '字段名' 列类型 [属性] [索引] [注释]
        )[表类型][字符集设置][注释];
    
    
    
    样例:
    create table if not exists `student` (
         `id` int(4) not null auto_increment comment '学号',
         `name` varchar(30) not null default '匿名' comment '姓名',
         `pwd` varchar(20) not null default '123456' comment 'password',
         primary key(`id`)
         )engine=innodb default charset=utf8 auto_increment=100
         ;
    
    ------------------------------------------------------------
        
    --修改表
    alter table teacher rename as teacher2   --重命名
    alter table teacher add age int(11)      --添加列
    alter table teacher modify age varchar(11)  --修改约束
    alter table teacher change age age1 int(1)  --修改名称和约束
    alter table teacher drop ages  --删除列
    alter table teacher alter name set default "匿名"    --设置默认字段
    alter table teacher alter name drop default    --删除默认值
    alter table tableName drop foreign key keyName;   --删除外键
    alter table teacher auto_increment=100
    --删除表
    drop table if exists teacher
    
    1. 增删改查操作 删除和修改一定要记得写条件!!!

      --在多列插入多个值
      insert into 表名([字段1,字段2,字段3]) values('值1','值2','值3'),('值1','值2','值3')  
      例如:
      insert into `grade`(`gradename`,`sex`) values('张三','男'),('李四','女');
      
      --------------------------------------------------
      
      --修改多个值
      update 表名 set 原来值=现在的值,原来值=现在的值,原来值=现在的值 条件;
      
      -------------------------------------------------
      #the use of select
      SELECT column_name,column_name
      FROM table_name
      [WHERE Clause]
      [LIMIT N][ OFFSET M]
      查询语句中你可以使用一个或者多个表,表之间使用逗号(,)分割,并使用WHERE语句来设定查询条件。
      SELECT 命令可以读取一条或者多条记录。
      你可以使用星号(*)来代替其他字段,SELECT语句会返回表的所有字段数据
      你可以使用 WHERE 语句来包含任何条件。
      你可以使用 LIMIT 属性来设定返回的记录数。
      你可以通过OFFSET指定SELECT语句开始查询的数据偏移量。默认情况下偏移量为0。
      
      #the use of 'like' in sql 
      #we often use like and % together
      '%a'     //以a结尾的数据
      'a%'     //以a开头的数据
      '%a%'    //含有a的数据
      '_a_'    //三位且中间字母是a的
      '_a'     //两位且结尾字母是a的
      'a_'     //两位且开头字母是a的
      
      #the use of union
      default attribute of union is 'disitnct' that means repeated words will not appear again
      we can use 'union all' to show all of the words
      
      #the use of order by 
      asc is ascending ,desc is descending 
      ORDER BY CONVERT(runoob_title using gbk);  #change the method of coding
      
      # the use of group by
      SELECT column_name, function(column_name)
      FROM table_name
      WHERE condition
      GROUP BY column_name;
      
      #the ues of join 
      SELECT A.x,b.x  FORM
      TABLE_A  A  INNER JOIN TABLE_B B ON A.KEY = B.KEY
      
      #the use of null 
      to decide if a word is null ,we can use 
      'is null' or 'is not null'
      
      # use regular expresion
      the rules in detail is at end of the text
      select name from student where name regexp '^st' or 'ok$';
      
      # the use of index
      
      primary key
      unique key 
      key 
      fulltext
      
      show index from student;
      alter table school.student add fulltext index  `studentName`(`studentName`)   #索引名,列名
      
      #copy a table
      create table targetTable like sourseTable;
      insert into targetTable select *from sourseTable ;
      
      #导出
      select * from teacher into outfile 'C:/desktop/hh.txt'
      #导出
      1.mysql -u用户名    -p密码    <  要导入的数据库数据(runoob.sql)
      mysql -uroot -pS9001883 < z018_rank.sql;
      2. mysql sourse/home/abc/abc.sql;
      3. load data local infile 'rnk.txt' into table mytable
      
      
      #functions in sql 
      char_length("michale")
      format(250500.5634, 2); 
      lower(s)
      rtrim(s)   #去掉尾部的空格
      bin(x)    # 返回x的二进制代码
      
      
  11. 条件

    =
    <> 或 !=
    between ...and ...
    and
    or
    
    

PHP中MySql的应用

在这个笔记中,[]内的参数为可选非必须参数

//connect to databases
mysqli_connect(host,username,password,[dbname,port,socket]);
<?php
$dbhost = 'localhost';  // mysql服务器主机地址
$dbuser = 'root';            // mysql用户名
$dbpass = '123456';          // mysql用户名密码
$conn=mysqli_connection($dbhost,$dbuser,$dbpass)    
?>
   
//close db
   mysqli_close($conn);

// 设置编码,防止中文乱码
mysqli_query($conn , "set names utf8");   
 
//create a new database
mysqli_query(connection,query,[resultmode])
  <
   $sql = 'CREATE DATABASE RUNOOB';
   $retval = mysqli_query($conn,$sql );
   >
       
//select a database you want to use
mysqli_select_db(connection,dbname);
<?php
 mysqli_select_db($conn,'2018_rank')  
?>
 
//if you have chosen a database then:
//want to create a table 
$sql = "CREATE TABLE runoob_tbl( ".      //be careful at  end of the line has a '.'
       "runoob_id INT NOT NULL AUTO_INCREMENT, ".
       "runoob_title VARCHAR(100) NOT NULL, ".
       "runoob_author VARCHAR(40) NOT NULL, ".
       "submission_date DATE, ".
       "PRIMARY KEY ( runoob_id ))ENGINE=InnoDB DEFAULT CHARSET=utf8; ";
mysqli_select_db($conn,'2018_rank');
$return=mysqli_query($conn,$sql);

//show what you have found
$retval=mysqli_query($conn,$sql)
echo '<table border="1"><tr><td>教程 ID</td><td>标题</td><td>作者</td><td>提交日期</td></tr>';
while($row = mysqli_fetch_assoc($retval))
{
   echo "<tr><td> {$row['runoob_id']}</td> ".
        "<td>{$row['runoob_title']} </td> ".
        "<td>{$row['runoob_author']} </td> ".
        "<td>{$row['submission_date']} </td> ".
        "</tr>";
}
while($row = mysqli_fetch_array($retval, MYSQLI_NUM))
{
   echo "<tr><td> {$row[0]}</td> ".
        "<td>{$row[1]} </td> ".
        "<td>{$row[2]} </td> ".
        "<td>{$row[3]} </td> ".
        "</tr>";
}
while($row = mysqli_fetch_array($retval, MYSQLI_ASSOC))
{
   echo "<tr><td> {$row['runoob_id']}</td> ".
        "<td>{$row['runoob_title']} </td> ".
        "<td>{$row['runoob_author']} </td> ".
        "<td>{$row['submission_date']} </td> ".
        "</tr>";
}
echo '</table>';
mysql_free_result($retval);//在我们执行完 SELECT 语句后,释放游标内存是一个很好的习惯。

//获取受影响的条数
mysqli_affected_rows(%conn)
   
//show all of the tables 
   $db_list = mysqli_query($conn, 'SHOW DATABASES');
while ($db = mysqli_fetch_object($db_list))
{
 echo $db->Database . "<br />";
}



  

Appendix

  1. regular expression

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wWCCT2Z6-1626169029530)(img/image-20210713134804669.png)]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值