PS C:\WINDOWS\system32> net stop mysql
MySQL 服务正在停止.
MySQL 服务已成功停止。
PS C:\WINDOWS\system32> net start mysql
MySQL 服务正在启动 .
MySQL 服务已经启动成功。
PS C:\WINDOWS\system32>
C:\Users\lenovo>mysql -h localhost -P 3306 -u root -p
Enter password: ******
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 2
Server version: 5.5.62 MySQL Community Server (GPL)
Copyright (c) 2000, 2018, 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>
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
+--------------------+
4 rows in set (0.00 sec)
mysql>
mysql> use test;
Database changed
mysql>
mysql> show tables;
Empty set (0.00 sec)
mysql> show tables from mysql;
+---------------------------+
| Tables_in_mysql |
+---------------------------+
| columns_priv |
| db |
| event |
| func |
| general_log |
| help_category |
| help_keyword |
| help_relation |
| help_topic |
| host |
| ndb_binlog_index |
| plugin |
| proc |
| procs_priv |
| proxies_priv |
| servers |
| slow_log |
| tables_priv |
| time_zone |
| time_zone_leap_second |
| time_zone_name |
| time_zone_transition |
| time_zone_transition_type |
| user |
+---------------------------+
24 rows in set (0.00 sec)
mysql>
mysql> select database();
+------------+
| database() |
+------------+
| test |
+------------+
1 row in set (0.00 sec)
mysql>
mysql> create table stuinfo(
-> id int,
-> name varchar(20));
Query OK, 0 rows affected (0.02 sec)
mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| stuinfo |
+----------------+
1 row in set (0.00 sec)
mysql> desc stuinfo;
+-------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| name | varchar(20) | YES | | NULL | |
+-------+-------------+------+-----+---------+-------+
2 rows in set (0.01 sec)
mysql>
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.5.62 |
+-----------+
1 row in set (0.00 sec)
mysql> quit
Bye
C:\Users\lenovo>mysql --version
mysql Ver 14.14 Distrib 5.5.62, for Win64 (AMD64)
C:\Users\lenovo>mysql -V
mysql Ver 14.14 Distrib 5.5.62, for Win64 (AMD64)
C:\Users\lenovo>
建表建库的操作
级联删除
alter table tbstudent add constraint fk_stu_major foreign key(majorid) references major(id) on delete cascade;
级联置空
alter table tbstudent add constraint fk_stu_major foreign key (majorid) references major(id) on delete set null;
增删改
数据权限等控制语句
上面主键,外键,唯一都会创建索引,可以使用 show index from 表查看
主键跟唯一键的区别
键 | 唯一性 | 非空性 | 组合性 | 重复性 |
---|---|---|---|---|
主键 | 是 | 是 | 可以组合 | 最多一个主键 |
唯一键 | 是 | 否 | 可以组合 | 可以多个唯一键 |
隐式事务:默认insert,update,delete默认都会开启事务;
显示事务:
步骤一:开启事务
set autocommit=0;
start transaction(可选)
步骤二:sql
select,insert,update,delete;
步骤三:提交事务
commit:提交事务
rollback:回滚事务;
select @@tx_isolation:查看事务隔离级别
set session transaction isolation level read uncommitted:设置隔离级别为read uncommitted
使用delemiter表示设置$为结束符号
delimiter $
create procedure 名称(参数列表)
begin
存储过程体(一组合法的sql语句)
end $
delimiter $
create function 名称 returns 返回类型
begin
函数体
end
iterate 类似于continue
leave类似于break;