Non-daily use back-end development Mysql Summary

Five concepts database

  1. The database server
  2. database
  3. data sheet
  4. Data Field
  5. Data line

Below, this is both above the basic concepts of daily operations.

Database engine

Here are just only describes two common engine, InnoDB is from MySQL 5.6. Later InnoDB storage engine is used as the default startup.

(1) InnoDB
        a,支持ACID,简单地说就是支持事务完整性、一致性;
        b,支持行锁,以及类似ORACLE的一致性读,多用户并发;
        c,独有的聚集索引主键设计方式,可大幅提升并发读写性能;
        d,支持外键;
        e,支持崩溃数据自修复;
        InnoDB设计目标是处理大容量数据库系统,它的CPU利用率是其它基于磁盘的关系数据库引擎所不能比的。
         它是一个可靠地事务处理引擎,不支持全文本搜索

(2) MyISAM
        a,不支持 每次查询具有原子性
        b,只支持表所
        c,强调的是性能,其执行数 度比InnoDB类型更快,但是不提供事务支持
        d,如果执行大量的SELECT,MyISAM是更好的选择
        e,缺点:就是不能在表损坏后恢复数据。(是不能主动恢复)

Now that you know the advantages and disadvantages maybe kind of engine, then write about several common API operations.

show engines;   --显示所有可用的引擎

show table status from myDB;   --查看myDB数据库下的所有表使用的引擎

show create table 表名;         --指定查看表名的所有段名以及引擎

create table 表名(id int primary key, name varchar(50)) engine=MyISAM; --建表的时候指定引擎   

alter table 表名 Engine= MyISAM; --建完表后修改引擎为MyISAM

Of course, you can also modify the configuration file my.iniand finally add [mysqld] as the default-storage-engine = InnoDB, restart the service, the default database engine modified to InnoDB.

Database operations

>net start mysql  //启动数据库和停止net stop mysql

>mysql -u root -p   //默认登陆本机(-h是主机地址)

>SELECT USER();   //显示当前用户

>create database 数据库名;    //创建数据库

>SELECT DATABASE();   //显示当前使用数据库

>SHOW DATABASES        //显示所有数据库列表

>USE DATABASE 库名;     //使用该数据库

>DROP DATABASE 库名   //删除数据库

>CMD终端:mysqladmin -u用户名 -p旧密码 password 新密码   //修改密码

>mysql语句:set password for 用户名@localhost = password('新密码'); 

Table Operator

>SHOW TABLES;   //列出库中所有的表

>DESCRIBE table1;  //查看表结构

>show columns from 数据表;   //显示表的所有段名以及类型

>CREATE TABLE 表名 (字段名 VARCHAR(20), 字段名 CHAR(1)); //增加数据表和字段名

>DROP TABLE 表名;  //删除表

>alter table stu rename as students;  //将旧表明stu改为新表明students。

Field Operation

Add field

语法:ALTER TABLE 表名 ADD COLUMN 字段名 字段类型 DEFAULT NULL;

示例:ALTER TABLE dictionary ADD COLUMN calss VARCHAR(10) DEFAULT NULL;
-- dictionary是表名

Modify the field name

语法:ALTER TABLE 表名 CHANGE 旧字段名 新字段名 新字段类型 DEFAULT NULL;

示例:ALTER TABLE dictionary CHANGE calss class VARCHAR(10) DEFAULT NULL;

Delete field

语法:ALTER TABLE 表名 DROP COLUMN 字段名

示例:ALTER TABLE dictionary DROP COLUMN calss;

Add Batch field

bagin;                                           //事务开始
alter table em_day_data add f_day_house7 int(11);
alter table em_day_data add f_day_house8 int(11);
alter table em_day_data add f_day_house9 int(11);
alter table em_day_data add f_day_house10 int(11);
commit;                                             //提交事务,事务结束

Data CRUD

MySQL statement case-insensitive. Data can only be one table for each primary key.

Tip: Since the first field name of the data table (that is, primary key, if it is id) can not be repeated named (recommended id, because each table can only be one primary key), it is recommended that when deleted using (where id = 1 ) keys to locate a better match.

check

var  sql = 'SELECT * FROM websites';   --查询整个websites表所有数据
var  sql = 'SELECT name FROM websites';  --查询整个websites表name字段数据

increase

-- websites表必须存在(Id,name,url,alexa,country)这些字段名
var  addSql = 'INSERT INTO websites(Id,name,url,alexa,country) VALUES(2,"taobao","http://wwww.taobao.com","3","CN")';

insert into table1(id,name,url,country) values(6,"lucas","https://wwww.lucas.com","CN");

change

-- where是定位到上面的增加数据(可以使用任何"字段名=值"来匹配),并更改俩个数据
var modSql = 'UPDATE websites SET name = "facebook",url = "http://www.facebook.com" WHERE Id = 2';

var modSql = 'update websites SET alexa="12" where name="taobao"';  --定位name字段为taobao并更新了alexa字段的值

delete

var delSql = 'DELETE FROM websites where id=2';  -- 删除id=2的数据(当然)

-- 删除name=lucas的数据(建议使用id,因为每张表只能存在一个主键),因为他会将表中所有name=lucas的值全部删除
delete from websites where name="lucas";   

Guess you like

Origin www.cnblogs.com/jing-tian/p/11687588.html