MySQL database building, table addition, deletion, modification and query

1.yum installation

      yum -y install mysql mysql-server    centos6
      yum -y install mariadb mariadb-server    centos7
      启动mysql    centos6
      service mysqld start
      chkconfig mysqld on
      
      启动mariadb    centos7
      service mysqld start
      chkconfig mysqld on

2. Log in to MySQL

 mysql -ujack -p123 -h 192.168.189.171 -P 3306
        选项:
            -u   指定登录用户名
            -p   登录的密码
            -h   链接的mysql主机ip地址
            -P   链接数据库的端口
     # 默认用户为root,mysql5.6之前密码为空

3. Create a database and table

3.1 Check which databases are in the database

 show databases;
		+--------------------+
		| Database           |
		+--------------------+
		| information_schema |
		| mysql              |
		| test               |
		+--------------------+
3 rows in set (0.00 sec)

3.2 Create a database

create database db;
Query OK, 1 row affected (0.00 sec)\

1. Enter the database

use mysql

2. View the table

show tables;

3. Create a table

语句: create table 表名(字段1 字段类型 约束条件,字段2 字段类型 约束条件);
create table stu(ID int(5),NAME varchar(10));

4. View the table structure

desc stu;
		+-------+-------------+------+-----+---------+-------+
		| Field | Type        | Null | Key | Default | Extra |
		+-------+-------------+------+-----+---------+-------+
		| ID    | int(5)      | YES  |     | NULL    |       |
		| NAME  | varchar(10) | YES  |     | NULL    |       |
		+-------+-------------+------+-----+---------+-------+

5. Insert a piece of data into the table

insert into stu values(1,"tom");
查询一下
select * from stu;
		+------+------+
		| ID   | NAME |
		+------+------+
		|    1 | tom  |
		+------+------+

4. Modify the default character encoding

修改mysql 5.1版本 默认编码字符为UTF8

修改配置文件
# vim /etc/my.cnf
[mysqld]
character_set_server=utf8
init_connect='SET NAMES utf8'
systemctl restart mariadb

Insert picture description here

MariaDB [(none)]> show variables like '%char%';

Insert picture description here

5. Check

desc view table structure

desc stu;
+--------+-------------+------+-----+---------+----------------+
| Field  | Type        | Null | Key | Default | Extra          |
+--------+-------------+------+-----+---------+----------------+
| 学号   | int(8)      | NO   | PRI | NULL    | auto_increment |
| 姓名   | varchar(10) | NO   |     | NULL    |                |
| 性别   | varchar(5)  | YES  |     | NULL    |                |
+--------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

Query record
select field from table name [where condition];

例子:
查看“学号”为“2”,的学生的“姓名”和“性别”
select 姓名,性别 from stu where 学号='2';
插入三条测试数据
 insert into stu values(3,'王五','女','15');
		insert into stu values(4,'王二','男','25');
		insert into stu values(5,'张六','女','17');
1.查询年龄大于等于18岁的学生信息
select * from stu  where 年龄 >= 18;
2.查询年龄大于18岁的女生的信息
 select * from stu where 性别='女' and 年龄 >= 18;
3.查询年龄在17到20岁之间的信息

select * from stu where age between 17 and 20;
4. Query the information of classmates surnamed Zhang

 select * from stu where 姓名 like '张%';
5.信息按年龄进行降序显示
 select * from stu  order by 年龄 DESC;
6.查询班级男生和女生各多少个
 select 性别,count(学号) from stu group by 性别;
7.查询班级女生有多少个
 select 性别,count(学号) from stu group by 性别 having 性别="女";
8.查询年龄最大的
 select 姓名,max(年龄) from stu;
 年龄最小的
 select 姓名,min(年龄) from stu;
 平均年龄
 select avg(年龄) from stu;
 年龄总和
 select sum(年龄) from stu;
9.查询年龄最小的学生的信息
select * from stu where 年龄 = (select min(年龄) from stu);
10.查询第3行到第5行
select * from stu where 学号 limit 2,3;

6. Change

Add a field:

alter table 表名  add   新字段 字段类型 [after  字段 ];

Add a college field after the student number

   alter table stu add 学院 varchar(10) after 学号;

Delete a field:

alter table 表名 drop  字段名;

Delete the "College" field

 alter table stu drop 学院;

Change the type of field

 alter table 表名 modify  字段名 新的类型 新的约束条件;

Modify the "gender" field type to char(2)

 alter table stu modify 性别 char(2) not null;

Change the field name

alter table stu change 旧字段名  新字段名 字段类型;

Modify the "Name" field to "NAME"

  alter table stu change 姓名  NAME char(10) not null;

Modify the table name:

alter table 表名 rename [to] 新表名;

Modify the table name stu to student

 alter table stu rename student;
  rename table 旧表名 to 新表名;

Modify the table name student to stu

  
    rename table student to stu;

7. Delete

Delete record:
delete all records

  delete from stu;

Delete records according to conditions, delete records that meet the conditions, and do not delete records that do not. Delete
the record named "Wang Wu"

  delete from stu  where  姓名="王五";

Modify the content of a record:

 蒋性别这一列的值全部改为男
 update stu set 性别="男"; 
  蒋李四的"性别"改为"女"
  update stu set 性别="女" where 姓名="李四";

8. Increase

Copy table:
only copy the table structure

  create table  表名  like 旧表名;

Copy table structure and table content

  create table  表名  select * from 旧表名; 

Add a record:
insert all columns:

 insert into 表名  values(1,2,....);

Partial column insertion:


        insert into 表名(字段1,字段2...)  values(1,2,....);

Guess you like

Origin blog.csdn.net/APPLEaaq/article/details/108947532