2.MySQL (b)

The operation data table

1. Create a table

Syntax: the CREATE TABLE table_name (column_name column_type); 

Create Table Student ( 
    -> ID the INT the NOT NULL the AUTO_INCREMENT, 
    -> name CHAR (32) the NOT NULL, 
    -> Age the INT the NOT NULL, 
    -> regiiter_date DATE, 
    -> a PRIMARY KEY (ID )          
    ->); 

AUTO_INCREMENT represents: 1 increment. Written content is empty, the default padding down from 1,2,3 ... write table. primary key: will be bound (not repeatable and can not be empty); accelerating find not null: not null 

see Table
Show the Tables; -> to see which table has
desc student; -> View student table information
show create table student; -> View student table information created

2. Delete table

#drop table 表名

drop table student;

3. Modify the table

Add a column: alter table table name add Column Name Type 
delete columns: alter table table name drop column column name 
to modify column: 
        alter table table name modify column listed types;   - type 
        alter table table name change of the original column names new column name type ; - column name, type 
   
add the primary key: 
        ALTER table table add primary key (column names); 
drop primary: 
        ALTER table table name drop primary key; 
        ALTER table table modify column names int, drop primary key; 
   
Add foreign key: alter table add constraint foreign key name (of the form: FK_ from table _ master table) from the table from the table (the foreign key field) references a main table (the primary key field) foreign key; 
remove foreign key: alter table table name drop foreign key outside The key name 
   
modify the default values: the ALTER TABLE testalter_tbl the ALTER i the SET the dEFAULT 1000 ; 
delete the default value: ALTER TABLE testalter_tbl ALTER i DROP dEFAULT ;
method
1. Increase
 the ALTER TABLE Student the ADD Sex CHAR ( 32);     # -> increase a

 
2 . Remove
 the ALTER TABLE Student the DROP Sex;      # -> Delete a

 
3 to modify the table name.
 The ALTER TABLE Students. Student the RENAME the TO;    # -> rename 

4. modify Listing
the ALTER TABLE Students. regisiter_date register_date the cHANGE DATE;

#change field names, types can be changed, only change the type modify

Table of contents Operation

1. Insert Data

语法:
INSERT INTO table_name ( field1, field2,...fieldN )
                       VALUES
                       ( value1, value2,...valueN );

插入数据:

mysql> INSERT INTO student(name,age,regisiter_date)
       -> VALUES('derek',22,'2017-01-01');

 mysql> INSERT INTO student(name,age,regisiter_date)
         -> VALUES('jack',20,'2017-03-03');

INSERT INTO student(name,age,regisiter_date) VALUES('Tom',25,'2017-05-05');
INSERT INTO student(name,age,regisiter_date) VALUES('David',25,'2017-07-07');


SELECT * FROM student; -->看表里面的内容

2. View data

Syntax
    
 . 1 .Select column_name, column_name
 2 .FROM table_name
 . 3 . [The WHERE Clause]
 . 4 . [The OFFSET M] [N the LIMIT] 

    query you may use one or more tables, table using commas (,) is divided, and use the WHERE clause to set the search criteria. 
    SELECT command reads one or a plurality of records. 
    You can use an asterisk ( * ) instead of the other fields, SELECT statement returns all field data tables 
    you can use the WHERE clause to include any conditions. 
    You can specify the data offset the query SELECT statement begins by OFFSET. Offset 0 by default. 
    You can use the LIMIT attribute to set the number of records returned.
grammar
1. the SELECT * Student the FROM 2 the OFFSET the LIMIT 2; 

# limit: check several data 
# offset: start search from several

 
2. the SELECT * WHERE the FROM ID Student>. 1; 

# condition judgment
 
3. the SELECT * WHERE ID Student the FROM> and Age. 1 <22 is; 

# plurality condition
 
4. the SELECT * WHERE name like the FROM Student "De%"; 

# like Fuzzy query

3. Modify

update students set name = "Eric" where id=3;  

4. Delete

delete from students where id>3;

5. Sort

Sorting 
    SELECT * from Table order by column asc - The "column" in ascending order 
    SELECT * from Table order by column desc - The "column" in descending order 
    SELECT * from the table column order by 1 desc, column 2 asc - the "column 1" in descending order, if the same column 2 press from small to large
method
* Students from the Order by the SELECT id desc;    

# ---> id flashback by arrangement

6. Packet

1 . Press the name of the group, and count the number of names appearing 
the SELECT name, COUNT ( *) from Students Group by name;

 2 . Grouped by name, the combined age of 
the SELECT name, SUM (Age) from Students Group by name; 

name the SELECT, sum (age) AS rename_sumage from Students Group by name;
 # plus as, you can customize the sum (age) name

Mysql connection

JOIN divided into the following three types according to the function:

  • INNER JOIN (the connection, or equivalent connection) : Get Records matching fields in the relationship between two tables.
  • LEFT JOIN (left connection): Get the left table all records, even if no record corresponding to the right table matching.
  • RIGHT JOIN (right connecting):  with LEFT JOIN contrast, all records in the table for acquiring the right, the left table even if there is no corresponding matching records.

交集
select * from A inner join B on A.a=B.b;

差集
select * from A left join B on A.a=B.b;
select * from A right join B on A.a=B.b;

并集
select * from A left join B on A.a=B.b union select * from A right join B on A.a=B.b;

事务

MySQL 事务主要用于处理操作量大,复杂度高的数据。

特性: 

  • 1、事务的原子性:一组事务,要么成功;要么撤回。
  • 2、稳定性 : 有非法数据(外键约束之类),事务撤回。
  • 3、隔离性:事务独立运行。一个事务处理后的结果,影响了其他事务,那么其他事务会撤回。事务的100%隔离,需要牺牲速度。
  • 4、可靠性:软、硬件崩溃后,InnoDB数据表驱动会利用日志文件重构修改。可靠性和高速度不可兼得, innodb_flush_log_at_trx_commit选项 决定什么时候吧事务保存到日志里。
begin     -->开启

insert into  ......    -->要写入的内容

rollback;             -->回滚到原来状态

commit;             -->确认提交

外键

MySQL支持外键的存储引擎只有InnoDB , 在创建外键的时候 , 要求父表必须有对应的索引 , 子表在创建外键的时候也会自动创建对应的索引

 

 
 
 

来源地址:https://www.cnblogs.com/derek1184405959/

数据之表操作

1.创建表

语法:CREATE TABLE table_name (column_name column_type);

create table student(
    -> id INT NOT NULL AUTO_INCREMENT,
    -> name CHAR(32) NOT NULL,
    -> age INT NOT NULL,
    -> regiiter_date DATE,
    -> PRIMARY KEY(id)         
    -> );

auto_increment 表示:自增1。写入内容为空时,默认从1,2,3...往下填充写入表格中。primary key:  表示约束(不能重复且不能为空); 加速查找not null: 不为空

查看表
show tables; -->查看有哪些表
desc student; --> 查看student表的信息
show create table student; -->查看表student创建的信息

2.删除表

#drop table 表名

drop table student;

3.修改表

添加列: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 testalter_tbl ALTER i SET DEFAULT 1000;
删除默认值:ALTER TABLE testalter_tbl ALTER i DROP DEFAULT;
方法
1.增加
ALTER TABLE student ADD sex CHAR(32);    #-->增加一列


2.删除
ALTER TABLE student DROP sex;     #-->删除一列


3.修改表名
ALTER TABLE student RENAME TO students;   #-->重命名

4.修改列名
ALTER TABLE students CHANGE regisiter_date register_date DATE;

#change 字段名,类型都可以改,modify只能改类型

表内容操作

1.插入数据

语法:
INSERT INTO table_name ( field1, field2,...fieldN )
                       VALUES
                       ( value1, value2,...valueN );

插入数据:

mysql> INSERT INTO student(name,age,regisiter_date)
       -> VALUES('derek',22,'2017-01-01');

 mysql> INSERT INTO student(name,age,regisiter_date)
         -> VALUES('jack',20,'2017-03-03');

INSERT INTO student(name,age,regisiter_date) VALUES('Tom',25,'2017-05-05');
INSERT INTO student(name,age,regisiter_date) VALUES('David',25,'2017-07-07');


SELECT * FROM student; -->看表里面的内容

2.查看数据

语法
    
1.SELECT column_name,column_name
2.FROM table_name
3.[WHERE Clause]
4.[OFFSET M ][LIMIT N]

    查询语句中你可以使用一个或者多个表,表之间使用逗号(,)分割,并使用WHERE语句来设定查询条件。
    SELECT 命令可以读取一条或者多条记录。
    你可以使用星号(*)来代替其他字段,SELECT语句会返回表的所有字段数据
    你可以使用 WHERE 语句来包含任何条件。
    你可以通过OFFSET指定SELECT语句开始查询的数据偏移量。默认情况下偏移量为0。
    你可以使用 LIMIT 属性来设定返回的记录数。
语法
1.SELECT * FROM student LIMIT 2 OFFSET 2;

#limit: 查几条数据
#offset: 从第几个开始查


2.SELECT * FROM student where id>1;

#条件判断

3.SELECT * FROM student where id>1 and age<22;

#多个条件

4.SELECT * FROM student where name like "De%";

#like模糊查询

3.修改

update students set name = "Eric" where id=3;  

4.删除

delete from students where id>3;

5.排序

排序
    select * from 表 order by 列 asc              - 根据 “列” 从小到大排列
    select * from 表 order by 列 desc             - 根据 “列” 从大到小排列
    select * from 表 order by 列1 desc,列2 asc    - 根据 “列1” 从大到小排列,如果相同则按列2从小到大排
方法
select * from students order by id desc;   

#--->按id倒叙排列

6.分组

1.按名字分组后,并且统计名字出现的次数
select name,count(*) from students group by name;

2.按名字分组后,把年龄加起来
select name,sum(age) from students group by name;

select name,sum(age) as rename_sumage from students group by name;
#加as,可以自定义sum(age)的名字

Mysql 连接

JOIN 按照功能大致分为如下三类:

  • INNER JOIN(内连接,或等值连接):获取两个表中字段匹配关系的记录。
  • LEFT JOIN(左连接):获取左表所有记录,即使右表没有对应匹配的记录。
  • RIGHT JOIN(右连接): 与 LEFT JOIN 相反,用于获取右表所有记录,即使左表没有对应匹配的记录。

交集
select * from A inner join B on A.a=B.b;

差集
select * from A left join B on A.a=B.b;
select * from A right join B on A.a=B.b;

并集
select * from A left join B on A.a=B.b union select * from A right join B on A.a=B.b;

事务

MySQL 事务主要用于处理操作量大,复杂度高的数据。

特性: 

  • 1、事务的原子性:一组事务,要么成功;要么撤回。
  • 2、稳定性 : 有非法数据(外键约束之类),事务撤回。
  • 3、隔离性:事务独立运行。一个事务处理后的结果,影响了其他事务,那么其他事务会撤回。事务的100%隔离,需要牺牲速度。
  • 4、可靠性:软、硬件崩溃后,InnoDB数据表驱动会利用日志文件重构修改。可靠性和高速度不可兼得, innodb_flush_log_at_trx_commit选项 决定什么时候吧事务保存到日志里。
begin     -->开启

insert into  ......    -->要写入的内容

rollback;             -->回滚到原来状态

commit;             -->确认提交

外键

MySQL支持外键的存储引擎只有InnoDB , 在创建外键的时候 , 要求父表必须有对应的索引 , 子表在创建外键的时候也会自动创建对应的索引

 

Guess you like

Origin www.cnblogs.com/gaidy/p/11864459.html