table operations.

table operations

create table

insert image description here
Example:
insert image description here

[root@VM-8-9-centos db1]# pwd
/var/lib/mysql/db1
[root@VM-8-9-centos db1]# ll
total 20
-rw-r----- 1 mysql mysql   65 May 22 15:55 db.opt
-rw-r----- 1 mysql mysql 8700 May 22 17:05 users.frm
-rw-r----- 1 mysql mysql    0 May 22 17:05 users.MYD
-rw-r----- 1 mysql mysql 1024 May 22 17:05 users.MYI

View database files

[root@VM-8-9-centos mysql]# cat /etc/my.cnf # 查看配置文件中的 datadir
datadir=/var/lib/mysql

[root@VM-8-9-centos mysql]# cd /var/lib/mysql # 进入
[root@VM-8-9-centos mysql]# ls
auto.cnf    client-cert.pem  ib_buffer_pool  ib_logfile1  mysql.sock          private_key.pem  server-key.pem
ca-key.pem  client-key.pem   ibdata1         ibtmp1       mysql.sock.lock     public_key.pem   sys
ca.pem      db1              ib_logfile0     mysql        performance_schema  server-cert.pem

view table

  • View all tables in the current library
show tables;
  • View the structure of the table
desc 表名;

insert image description here

modify table

In the actual development of the project, the structure of a certain table is often modified, such as field name, field size, field type, character set type of the table, storage engine of the table, and so on. We also have requirements, adding fields, removing fields, and so on. Then we need to modify the table.

ALTER TABLE tablename ADD (column datatype [DEFAULT expr][,column datatype]...);  // first - after 
ALTER TABLE tablename MODIfy (column datatype [DEFAULT expr][,column datatype]...);
ALTER TABLE tablename DROP (column);


modify 与 change
1. MODIFY用于修改已有列的数据类型和约束条件,但不修改列的名称
2. CHANGE用于修改表的列名、数据类型和约束条件
因此,如果只需要修改列的数据类型和约束条件,使用MODIFY语句即可;如果需要修改列名、数据类型和约束条件,使用CHANGE语句

  • Add two records in the users table
mysql> insert into users values(1,'a','b','1982-01-04'),(2,'b','c','1984-01-04');
  • Add a field in the users table to save the image path
mysql> alter table users add assets varchar(100) comment '图片路径' after birthday;
  • Modify the name and change its length to 60
mysql> alter table users modify name varchar(60);
  • delete the password column
alter table users drop password; # 注意:删除字段一定要小心,删除字段及其对应的列数据都没了
  • Modify the table name to employee
mysql> alter table users rename to employee; # to:可以省掉
  • Change the name column to xingming
mysql>alter table employee change name xingming varchar(60); --新字段需要完整定义
  • Query the data in the table
mysql> select * from users;

delete table

insert image description here

Guess you like

Origin blog.csdn.net/weixin_54183294/article/details/130810565