Mysql基础简单指令(打开/创建/修改/删除)

打开SQL

进入终端,复制粘贴下面,回车,输入密码:

cd /usr/local/mysql/bin
./mysql -u root -p
macdeAir:mysql mac$ cd /usr/local/mysql/bin
macdeAir:bin mac$ ./mysql -u root -p
Enter password: 
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 9
Server version: 8.0.19 MySQL Community Server - GPL

Copyright (c) 2000, 2020, 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.

mysql> 

语法规范

  1. 不区分大小写,但建议关键字大写,表名、列名小写
  2. 每条命令最好用分号;结尾
  3. 每条命令根据需要,可以进行锁进或换行
  4. 注释:
    1. 单行#
    2. --
    3. 多行/*----*/

SQL标准命令

与关系数据库交互的标准SQL命令是创建、选择、插入、更新、删除和删除,简单分为以下几组:

DDL(数据定义语言)

数据定义语言用于改变数据库结构,包括创建、更改和删除数据库对象。用于操纵表结构的数据定义语言命令有:
  1. CREATE TABLE-- 创建(在数据库中创建新表、表视图或其他对象)
  2. ALTER TABLE-- 更改(修改现有的数据库对象,如表)
  3. DROP TABLE-- 删除 (删除数据库中的整个表、表或其他对象的视图)

DML(数据操纵语言)

数据操纵语言用于检索、插入和修改数据,数据操纵语言是最常见的SQL命令。
数据操纵语言命令包括:
  1. INSERT-- 插入 (创建记录)
  2. DELETE-- 删除 (删除记录)
  3. UPDATE-- 修改(修改记录)
  4. SELECT-- 检索 (从一个或多个表检索某些记录)

DCL(数据控制语言)

数据控制语言为用户提供权限控制命令。
用于权限控制的命令有:
  1. GRANT-- 授予权限
  2. REVOKE-- 撤销已授予的权限

数据库

这里用创建一个‘AAA’名字的库
创建 create database AAA; (后面跟分号)
展示 show databases; 展示库
删除 drop database paojiaotest; 删除库
选择库 use AAA; (AAA是库的名字)
修改 alter database mydb2 character set utf8; (utf8没有杠)把mydb2的字符集修改为utf8
查看现在使用的库: select database();
打开制定库 use 库名
在这里插入图片描述

常用数据类型: int, double, char固定长度字符串, char(10),varchar可变长度字符串, varchar(10),text, blob, date, time,timestamp,daetime

1)打开数据库: use admin;
2)展示表格字段: show tables

mysql> use AAA
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
mysql> show tables;
+---------------+
| Tables_in_aaa |
+---------------+
| Course        |
| Score         |
| Student       |
| Teacher       |
+---------------+
4 rows in set (0.00 sec)

3)创建表

create table 表名(
			字段1 字段类型,
			字段2 字段类型,
			...
			最后一行字段 字段类型
);
create table emp(
		id int,
		name varchar(100),
		birthday date
		);

4)查看表的字段信息/表结构:DESC employee;

mysql> desc Student;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| Sid   | varchar(6)  | YES  | MUL | NULL    |       |
| Sname | varchar(10) | YES  |     | NULL    |       |
| Sage  | datetime    | YES  |     | NULL    |       |
| Ssex  | varchar(10) | YES  |     | NULL    |       |
+-------+-------------+------+-----+---------+-------+
4 rows in set (0.02 sec)

5)查看表中某列的信息 select * from Student;

mysql> select * from Student;
+------+--------+---------------------+------+
| Sid  | Sname  | Sage                | Ssex |
+------+--------+---------------------+------+
| 01   | 赵雷   | 1990-01-01 00:00:00 ||
| 02   | 钱电   | 1990-12-21 00:00:00 ||
| 03   | 孙风   | 1990-05-20 00:00:00 ||
| 04   | 李云   | 1990-08-06 00:00:00 ||
| 05   | 周梅   | 1991-12-01 00:00:00 ||
| 06   | 吴兰   | 1992-03-01 00:00:00 ||
| 07   | 郑竹   | 1989-07-01 00:00:00 ||
| 08   | 王菊   | 1990-01-20 00:00:00 ||
+------+--------+---------------------+------+
8 rows in set (0.01 sec)

6)其他内容:
Alter table employee Add image blod 在上面employee表上增加一个image列 类型是blog
alter table employee modify name varchar(25) 将name列改为长度25
alter table employee drop name 删掉列
rename employee to emps 修改表格名称
show create table amps 查看表格的创建细节
alter table emps change name to username varchar(25) 更换列名
drop table emps 删除表格

Insert/Update/Delete

插入INSERT

insert into emps(id,name,birthday values(1.'章三',1892-1-1);

select * from emp

修改UPDATE
update emp set salary=6000; 所有工资都是6000
update emp set salary=3000 where ‘name’=‘zhangsan’; 张三的工资是3000
update emp set salary=salary+3000 where ‘gender’=‘male’; 所有男的工资加一千

删除DELETE
delete from emp where name=‘zhongshan’; 删掉张三的一行
Delete from emp;表结构还在,删除后的数据可找回
truncate table emp; 直接把表Drop掉,再创建一个同样的新表,删除的数据不能找回,执行速度比DELETE快。

帮助列表

敲个“?”回车出现下面列表。

List of all MySQL commands:
Note that all text commands must be first on line and end with ‘;’
? (?) Synonym for `help’.
clear (\c) Clear the current input statement.
connect (\r) Reconnect to the server. Optional arguments are db and host.
delimiter (\d) Set statement delimiter.
edit (\e) Edit command with $EDITOR.
ego (\G) Send command to mysql server, display result vertically.
exit (\q) Exit mysql. Same as quit.
go (\g) Send command to mysql server.
help (\h) Display this help.
nopager (\n) Disable pager, print to stdout.
notee (\t) Don’t write into outfile.
pager (\P) Set PAGER [to_pager]. Print the query results via PAGER.
print (\p) Print current command.
prompt (\R) Change your mysql prompt.
quit (\q) Quit mysql.
rehash (#) Rebuild completion hash.
source (.) Execute an SQL script file. Takes a file name as an argument.
status (\s) Get status information from the server.
system (!) Execute a system shell command.
tee (\T) Set outfile [to_outfile]. Append everything into given outfile.
use (\u) Use another database. Takes database name as argument.
charset (\C) Switch to another charset. Might be needed for processing binlog with multi-byte charsets.
warnings (\W) Show warnings after every statement.
nowarning (\w) Don’t show warnings after every statement.
resetconnection(\x) Clean session context.

参考教程

W3Cschool SQL基础教程、有测试题目: 点这里有链接https://www.w3cschool.cn/sql/82rg1ozi.html

廖雪峰SQL教程 (有在线SQL运行显示、可以编辑在线练习,但具体指令不是很全):点这里有链接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值