Mysql操作(1)

Mysql基本操作(1)

1、登录
[root@192 ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 1
Server version: 5.6.46 MySQL Community Server (GPL)

Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

2、库级操作
----------------------------------查看库--------------------------------
show databases;
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| homework           |
| mysql              |
| performance_schema |
| test               |
+--------------------+

----------------------------------切换库-------------------
use 库名;
mysql> use homework
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

------------------删库--------------------------------
drop [if exists] database 库名;
------------------创建库--------------------------------
create database [if not exists] 库名;

3、表级操作
------------------创建表--------------------------------
create table if not exists 表名(
	列名 列类型(长度)属性 索引 注释,
	列名 列类型(长度)属性 索引 注释,
	....
	列名 列类型(长度)属性 索引 注释
)engine=引擎名 charset="" 注释;

----------------------------------删表--------------------------------
drop table 表名;
--------------------------查看建表语句--------------------------------
show create table 表名;
----------------------------查看表结构--------------------------------
mysql> show create table tb_class;
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table    | Create Table                                                                                                                                                                               |
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| tb_class | CREATE TABLE `tb_class` (
  `cid` int(4) unsigned NOT NULL AUTO_INCREMENT,
  `cname` varchar(20) DEFAULT NULL,
  PRIMARY KEY (`cid`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 |
+----------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

desc 表名;
mysql> desc tb_class;
+-------+-----------------+------+-----+---------+----------------+
| Field | Type            | Null | Key | Default | Extra          |
+-------+-----------------+------+-----+---------+----------------+
| cid   | int(4) unsigned | NO   | PRI | NULL    | auto_increment |
| cname | varchar(20)     | YES  |     | NULL    |                |
+-------+-----------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)

-------------------------------修改表--------------------------------
alter table 表名 

3.5 外键操作(为了给字段增加约束)
---------------------------创建时增加外键---------------------------
create table 表名(
	字段列表...,
	[costraint 索引名] foreign key(本表的字段) references 外表(外表字段)
);
-----------------------表已存在时的修改外键--------------------------
alter table 表名 add
		[costraint 索引名] foreign key(本表的字段) references 外表(外表字段)
------------------------删除外键----------------------------------
alter table 表名 drop foreign key 索引名;

4、行级操作(数据操作)
--------------------------插入语句---------------------------------
insert into 表名[(字段列表)] values(对应的值),...,(对应的值)
-------------------------修改语句----------------------------------
update 表名 set 字段名=[,...,字段名=] where 条件语句;

猜你喜欢

转载自blog.csdn.net/wl20040124/article/details/114035069