mysql operations on libraries, tables, records

mysql statement

mysql database operation

(1) View the database
directly enter the database to view:

mysql> show databases;

Display in line:

mysql> show databases \G

View in the shell:
mysql -e is directly followed by sql statement, this method is generally used in shell scripts

 mysql -e 'show databases' -uroot -p123456

(2) Create database
mysql> create database database name;

mysql> create database if not exists haha;  
##如果不存在此数据库,才创建,可以避免出现错误信息
mysql> create database `ha-ha`;   
##如果有特殊符号需要有``括起来

The database name in the above command must be consistent with the restricted directory name of the operating system.
File and directory names are not allowed to contain,/,:,*,?,",<,>,|These special symbols, these are in the MySQL database name Letters will be automatically deleted
. The name of the database cannot exceed 64 characters. Names containing special characters or names consisting entirely of numbers or reserved words must be enclosed in backquotes. The
database cannot be duplicated.

(3) Select the database to be operated

mysql> use 数据库名;
##后面的操作默认都是在被选择的数据库中进行操作
Database changed

(4) Check your location

mysql> select database();
##看看自己在数据库中的哪里

mysql> select now(),user(),database();
##看看自己在数据库哪里,用户是谁,
现在的日期和时间,使用时间函数,用户函数,数据库函数

(5) Select the default database on the shell command line

[root@haha ~]# mysql -uroot -p123456 haha

(6) Delete the database

mysql> drop database `ha-ha`;
##此命令不安全,数据无法恢复,在工作中
我们可以移动数据库目录到别的地方,防止
删除数据库了想要恢复
[root@haha ~]# mkdir /db_backup
[root@haha ~]# mv  /data/mysql/data/ha-ha \
>/db_backup

mysql> drop database if exists `ha-ha`;  
 ##如果存在此数据库,才进行删除,
 可以防止出现错误信息

mysql table operations

(1) Create table
mysql> create table table name (field name type, field name type, field name type);

mysql> use haha;
mysql> create table xixi(id int(20),name char(40),age int);

mysql> create table haha.hehe(id int(20),name char(40),age int);
##这样不需要进入到数据库里

(2) View the relevant information of the table

mysql> use haha;
Database changed
mysql> show tables;

(3) View the table structure
mysql> desc table name;

mysql> explain database.table name;
mysql> show columns from database.table name;
mysql> show fields from database.table name;
mysql> show columns from database.table name like'%field';

(4) Check which commands were executed to
create the table mysql> show create table table name\G

(5) Specify the default storage engine and character set.
Storage engine: a file system similar to a hard disk

Character set: different character sets, the length of characters is different

Create a new table, specify the default storage engine as InnoDB, and encode as utf8

mysql> create table student2(id int(20),name char(40),age int) ENGINE=InnoDB DEFAULT CHARSET=utf8;

(6) Delete table
mysql> drop table table name;

(7) Prohibition of pre-reading table information
There will be a prompt message if the database is converted before prohibition
mysql -uroot -p123456 -A

(8) Modify the table name
alter table table name rename new table name;

mysql> alter table student rename students;

(9) Modify the field type in the
table alter table table name modify the type of field name to be modified;

mysql> desc students;
mysql> alter table students modify id int(10);
##修改字段id 的int20)字段类型为int10

(10) Modify the field type and field name in the
table alter table table name change original field name new field name new field type;

mysql> desc students;
mysql> alter table students change name stname char(20);
##重命名字段name为stname,字段类型

CHANGE To rename the column and change the type of the column, you need to give the old column name, the new column name, and the current type. MODIFY can change the type of the column, no need to rename at this time (no need to give a new column name)

(11) Add a field in the table
alter table table name add field name field type;
enum ##enumeration type, such as gender, can only be selected in male and female, male and non-female, female and non-male

mysql>  alter table students add sex enum('M','W'); 

(12) Add a field at a specified position in the table

在第一列添加一个字段
mysql> alter table students add uid int(10) first;
在age后面添加一个address字段
mysql> alter table students add address char(40) after age;

(13) Delete the field
alter table table name drop field name in the table;

mysql>  alter table students drop address;

Record operation in mysql table

(1) Insert <record> INSERT
INSERT INTO statement is used to insert a new row into the table

insert into table name values ​​(field value 1, field value 2, field value 3);

When inserting a record, the value specified by values ​​must correspond to the number, order and type of the fields in the table one-to-one

mysql> drop tables students;
mysql> create table students(id int(20),name char(40),age int);
mysql> alter table students add sex enum(‘M’,’W’);
mysql> insert into students values(1,'haha',25,'M');

(2) Insert multiple records at the same time

mysql>  insert into students values(2,'lis',24,’M’),(3,'wange',26,’W’);

(3) Separately insert table records
insert into table name (field 1, field 2, ...) VALUES (field value 1, field value 2, ...);

mysql> insert into students (id,name)values(4,'hangl');

(4) The
SELECT statement recorded in the query table is used to read data from the database table

select * from table name;
##* indicates all fields in the table

查询所有记录:
mysql> select * from students;

表中记录多的时候可以使用\G查看;
mysql> select * from student\G

只查询表中某个字段或者某个字段的内容;
mysql> select name from students;
mysql> select id,name from students;

查看别的数据库的表或者不在本数据库上进行查看;
SELECT 字段 FROM 数据库名.表名;
mysql> select * from haha.students;

(5) Delete records in the table
delete from table_name WHERE some_column=some_value; The
WHERE clause specifies which record or records need to be deleted. If the WHERE clause is omitted, all records will be deleted!

删除表中id为3的行
mysql>  delete from students where id=3;

删除age为空的行
mysql> delete from students where age is null;

(6) Update record The
update statement is used to modify the data in the table
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value; The
WHERE clause specifies which record or which records need to be updated. If the WHERE clause is omitted, all records will be updated!

更新表中id为2的记录,更新内容age=25
mysql> update students set age=25 where id=2;

把表中所有id都更新为2
mysql> update students set id=2;

同时更新多个字段的记录(字段值),使用逗号隔开
mysql>  update students set id=1,name='zhangsan' where age=21;

Guess you like

Origin blog.csdn.net/weixin_52441468/article/details/112557312