mysql基本语句合集--超详细

这篇博客详细介绍了MySQL的各种基本操作,包括创建与删除数据库、表,插入与查询数据,设置主键,使用JOIN、GROUP BY等高级查询,以及权限管理等。
摘要由CSDN通过智能技术生成

1.创建数据库

CREATE DATABASE University;

2.展示数据库

SHOW DATABASES;

3.使用数据库

USE University;

4.创建表

CREATE TABLE student(
Sno CHAR(8),
Sname CHAR(10),
Ssex CHAR(2),
Sage SMALLINT,
Major CHAR(20)
);

create table student(id int,name varchar(20));

5.创建表 并指定存储引擎

create table lyp(id int,name varchar(20)) engine=myisam;

查看存储引擎:

root@liuyq 19:46  mysql>show create table lyp;
+-------+--------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                             |
+-------+--------------------------------------------------------------------------------------------------------------------------+
| lyp   | CREATE TABLE `lyp` (
  `id` int(11) DEFAULT NULL,
  `name` varchar(20) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=utf8 |
+-------+--------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

6.查看表结构 desc

root@sc 19:04  mysql>desc t1;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id    | int(11) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
1 row in set (0.00 sec)

查看表结构

root@ytm 12:55  mysql>show create table city;
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table                                                                                                                                                                                                                                               |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| city  | CREATE TABLE `city` (
  `name` varchar(20) DEFAULT NULL,
  `prov` varchar(20) DEFAULT NULL,
  `peplo` int(11) NOT NULL,
  `food` varchar(20) DEFAULT NULL,
  `happydata` int(6) DEFAULT NULL,
  PRIMARY KEY (`peplo`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
+-------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

7.删表

root@sc 19:07  mysql>drop table t1;

8.删库

root@sc 19:08  mysql>drop database sc;

9.插入

root@ytm 19:14  mysql>insert into student(id,name) values(1,'cali');

10.查询

root@ytm 19:14  mysql>select * from student;
+------+-------+
| id   | name  |
+------+-------+
|    1 | cali  |
|    1 | cali2 |
|    3 | cali2 |
+------+-------+

11.按条件删除

root@ytm 19:15  mysql> delete from student where id=1;
Query OK, 2 rows affected (0.00 sec)

root@ytm 19:37  mysql>select * from student;
+------+-------+
| id   | name  |
+------+-------+
|    3 | cali2 |
+------+-------+

where 指定条件
from 从哪个表

12.设置主键的两种方法
函数法

root@ytm 12:55  mysql>create  table stu2(id int,name varchar(20),gred int,primary key(id));
Query OK, 0 rows affected (0.02 sec)

root@ytm 13:01  mysql>desc stu2;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   | PRI | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
| gred  | int(11)     | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

属性法

root@ytm 13:03  mysql>create  table stu3(id int primary key,name varchar(20),gred int);
Query OK, 0 rows affected (0.01 sec)

root@ytm 13:03  mysql>desc stu2;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id    | int(11)     | NO   | PRI | NULL    |       |
| name  | varchar(20) | YES  |     | NULL    |       |
| gred  | int(11)     | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
3 rows in set (0.00 sec)

13.查看存储引擎

root@ytm 13:05  mysql>show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
| MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
| FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+

14.创建主键和外键

CREATE TABLE dept(
  deptid INTEGER,
  dname VARCHAR(20),
  PRIMARY KEY(deptid) 
); 
employee 员工
department 部门
REFERENCES 参考,引用

CREATE TABLE emp(
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值