SQL basic operation statement

Basic operation of SQL database

mysql -uroot -p            # 在终端打开 mysql 
exit;                    #退出MySQL数据库服务器
create database 数据库名;   # 创建数据库
drop database 数据库名;     # 删除数据库
show databases;            # 查看已经创建的数据库
use 数据库名;               # 使用某个数据库

修改密码语句
 alter user 'root'@'localhost' identified by '123456';

显示所有数据库
show databases;
-- 创建数据库
CREATE DATABASE test;
-- 切换数据库
use test;
-- 显示数据库中的所有表
show tables;
-- 创建数据表
CREATE TABLE pet (
    name VARCHAR(20),
    owner VARCHAR(20),
    species VARCHAR(20),
    sex CHAR(1),
    birth DATE,
    death DATE
);
-- 查看数据表结构
-- describe pet;
desc pet;
-- 查询表
SELECT * from pet;
-- 插入数据
INSERT INTO pet VALUES ('puffball', 'Diane', 'hamster', 'f', '1990-03-30', NULL);
-- 修改数据
UPDATE pet SET name = 'squirrel' where owner = 'Diane';
-- 删除数据
DELETE FROM pet where name = 'squirrel';
-- 删除表
DROP TABLE myorder;

 如何查看数据表的架构?   
describe tableName;
 说明:
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
 Field    :    字段的名称
 Type     :    字段的类型,可以有int    char    varchar    
 Key      :    是否是关键字 如可以定义为:  primary key 或者 unique key   ...
Default: :    若是该字段没有主动设置值的时候,该字段的默认值是什么?

如何插入数据?
 INSERT INTO pet VALUES('kk','cc','dog','1','1998-8-2',null);
           +------+-------+---------+------+------------+-------+
           | name | owner | specise | sex  | brith      | death |
           +------+-------+---------+------+------------+-------+
           | kk   | cc    | dog     |      | 1998-08-02 | NULL  |
           +------+-------+---------+------+------------+-------+
       注意:
           NULL:代表的是空,表示该字段还没有数据.千万不要主动填写'NULL',这代表你的字段有一个值叫做'null'.

其实还有一种写法:
                 
INSERT INTO pet(name,owner) VALUES ('xx','cc');
代表我只在name和owner字段上面插入的一条,其他皆为NULL/默认值的数据

mysql table creation constraints

  • Primary key constraint
    It can uniquely determine a record in a table. After adding the primary key constraint, it can make the field not repeated and not empty.

     create table user(
        id int PRIMARY KEY,
        name VARCHAR(20)
     );
     INSERT INTO user VALUES (1,'张三');
     	
     +----+------+
     | id | name |
     +----+------+
     |  1 | 张三 |
     +----+------+
     
     运行DESCRIBE user;
     +-------+-------------+------+-----+---------+-------+
     | Field | Type        | Null | Key | Default | Extra |
     +-------+-------------+------+-----+---------+-------+
     | id    | int(11)     | NO   | PRI | NULL    |       |
     | name  | varchar(20) | YES  |     | NULL    |       |
     +-------+-------------+------+-----+---------+-------+
     发现 id是不可以为null 而且 key的值 也变为:PRI(primary)
    
  • The combined primary key (composite primary key)
    is fine as long as the combined primary key values ​​do not add up to be repeated.

     CREATE TABLE user2(
         id INT,
         name VARCHAR(20),
         password VARCHAR(20),
         PRIMARY key(id,name)
     );
     
     运行DESCRIBE user2;
     +----------+-------------+------+-----+---------+-------+
     | Field    | Type        | Null | Key | Default | Extra |
     +----------+-------------+------+-----+---------+-------+
     | id       | int(11)     | NO   | PRI | NULL    |       |
     | name     | varchar(20) | NO   | PRI | NULL    |       |
     | password | varchar(20) | YES  |     | NULL    |       |
     +----------+-------------+------+-----+---------+-------+
      select * from user2;
     +----+------+----------+
     | id | name | password |
     +----+------+----------+
     |  1 | zcc  | 345      |
     |  2 | zc2  | 345      |
     +----+------+----------+
      select * from user2;
     +----+------+----------+
     | id | name | password |
     +----+------+----------+
     |  1 | zcc  | 345      |
     |  2 | zc2  | 345      |
     |  2 | zcc  | 345      |
     +----+------+----------+
    
  • Incremental constraint

       CREATE TABLE user3(
         id INT PRIMARY KEY AUTO_INCREMENT,
         name VARCHAR(20)
     );
     
     运行DESCRIBE user3;
     +-------+-------------+------+-----+---------+----------------+
     | Field | Type        | Null | Key | Default | Extra          |
     +-------+-------------+------+-----+---------+----------------+
     | id    | int(11)     | NO   | PRI | NULL    | auto_increment |
     | name  | varchar(20) | YES  |     | NULL    |                |
     +-------+-------------+------+-----+---------+----------------+
     
     INSERT INTO user3(name) VALUES('zcc');
     INSERT INTO user3(name) VALUES('zc');
     +----+------+
     | id | name |
     +----+------+
     |  1 | zcc |
     |  2 | zc |
     +----+------+
     没有自定义id值 但是自动生成了id
    
    • What if you forgot to create a primary key constraint when creating a table? What should we do?

         CREATE TABLE user4(
         		    id INT ,
         		    name VARCHAR(20)
         		);
         desc user4;
         +-------+-------------+------+-----+---------+-------+
         | Field | Type        | Null | Key | Default | Extra |
         +-------+-------------+------+-----+---------+-------+
         | id    | int         | YES  |     | NULL    |       |
         | name  | varchar(20) | YES  |     | NULL    |       |
         +-------+-------------+------+-----+---------+-------+
         
         alter table user4 add primary key(id);
         desc user4;
         +-------+-------------+------+-----+---------+-------+
         | Field | Type        | Null | Key | Default | Extra |
         +-------+-------------+------+-----+---------+-------+
         | id    | int         | NO   | PRI | NULL    |       |
         | name  | varchar(20) | YES  |     | NULL    |       |
         +-------+-------------+------+-----+---------+-------+
         如何删除主键约束?
         alter table user4 drop primary key;
         使用modify修改字段添加约束。
         alter table user4 modify id int primary key;
      
  • Unique Constraint
    The value of the constraint modification field cannot be repeated

     CREATE TABLE user5(
         id INT PRIMARY KEY AUTO_INCREMENT,
         name VARCHAR(20)
     );
     运行 DESCRIBE user5;
     +-------+-------------+------+-----+---------+----------------+
     | Field | Type        | Null | Key | Default | Extra          |
     +-------+-------------+------+-----+---------+----------------+
     | id    | int(11)     | NO   | PRI | NULL    | auto_increment |
     | name  | varchar(20) | YES  |     | NULL    |                |
     +-------+-------------+------+-----+---------+----------------+
     
     新增name为唯一约束:
     ALTER TABLE user5 ADD UNIQUE(name);
     运行 DESCRIBE user5;
     +-------+-------------+------+-----+---------+----------------+
     | Field | Type        | Null | Key | Default | Extra          |
     +-------+-------------+------+-----+---------+----------------+
     | id    | int(11)     | NO   | PRI | NULL    | auto_increment |
     | name  | varchar(20) | YES  | UNI | NULL    |                |
     +-------+-------------+------+-----+---------+----------------+
     测试:插入数据
     INSERT INTO user5(name) VALUES ('cc');
     运行 SELECT * FROM user5; 查看结果:
     +----+------+
     | id | name |
     +----+------+
     |  1 | cc   |
     +----+------+
     再次插入INSERT INTO user5(name) VALUES ('cc');
     出现:ERROR 1062 (23000): Duplicate entry 'cc' for key 'name'
     
     换个试试 INSERT INTO user5(name) VALUES ('aa');
     运行 SELECT * FROM user5; 查看结果:
     +----+------+
     | id | name |
     +----+------+
     |  3 | aa   |
     |  1 | cc   |
     +----+------+
     总结一下:
         主键约束(primary key)中包含了唯一约束
     场景:业务需求:设计一张用户注册表,用户姓名必须要用手机号来注册,而且手机号和用户名称都不能为空,那么:
     CREATE TABLE user_test(
         id INT PRIMARY KEY AUTO_INCREMENT COMMENT'主键id',
         name VARCHAR(20) NOT NULL COMMENT'用户姓名,不能为空',
         phone_number VARCHAR(20) UNIQUE NOT NULL COMMENT'用户手机,不能重复且不能为空'
     );
     运行 DESCRIBE user_test;
     +--------------+-------------+------+-----+---------+----------------+
     | Field        | Type        | Null | Key | Default | Extra          |
     +--------------+-------------+------+-----+---------+----------------+
     | id           | int(11)     | NO   | PRI | NULL    | auto_increment |
     | name         | varchar(20) | NO   |     | NULL    |                |
     | phone_number | int(11)     | NO   | UNI | NULL    |                |
     +--------------+-------------+------+-----+---------+----------------+
     这样的话就达到了每一个手机号都只能出现一次,达到了每个手机号只能被注册一次.
     用户姓名可以重复,但是手机号码却不能重复,符合正常的逻辑需求
    
  • Summary of adding constraints:
    1. Add constraints when creating a table;
    2. You can use alter ... add ...
    3. alter ... modify ...
    4. Delete: alter ... drop ...

  • Non-null constraint
    The modified field cannot be empty

      create table user6(id int, name varchar(20) not null);
      desc user6;
     +-------+-------------+------+-----+---------+-------+
     | Field | Type        | Null | Key | Default | Extra |
     +-------+-------------+------+-----+---------+-------+
     | id    | int         | YES  |     | NULL    |       |
     | name  | varchar(20) | NO   |     | NULL    |       |
     +-------+-------------+------+-----+---------+-------+
     
     insert into user6 values(1,'张三');
      insert into user6 (name) values('三');
     
      select * from user6;
     +------+--------+
     | id   | name   |
     +------+--------+
     |    1 | 张三   |
     | NULL | 三     |
     +------+--------+
    
  • Default constraints
    When we insert field values, if no value is passed, the default value will be used;

     create table user7(id int, name varchar(20),age int default 10);
     Query OK, 0 rows affected (0.03 sec)
     
     mysql> desc user7;
     +-------+-------------+------+-----+---------+-------+
     | Field | Type        | Null | Key | Default | Extra |
     +-------+-------------+------+-----+---------+-------+
     | id    | int         | YES  |     | NULL    |       |
     | name  | varchar(20) | YES  |     | NULL    |       |
     | age   | int         | YES  |     | 10      |       |
     +-------+-------------+------+-----+---------+-------+
     
     mysql> insert into user7(id,name) values(1,'dsd');
     Query OK, 1 row affected (0.01 sec)
     
     mysql> insert into user7(id,name,age) values(1,'dsd',20);
     Query OK, 1 row affected (0.01 sec)
     
     mysql> select * from user7;
     +------+------+------+
     | id   | name | age  |
     +------+------+------+
     |    1 | dsd  |   10 |
     |    1 | dsd  |   20 |
     +------+------+------+
     应用场景:
     业务需求:找正常的用户,对这些正常用户进行发放优惠卷或者积分之类的东西,而被禁封的用户我们不让其参加多动.
     我们想要封用户只要将status的值从0改为1就行了,当然我们取用户的时候必须要先判断status是否是0.若是1.说明该用户已经被禁封.
     先封手机号为'1234'的用户:
     UPDATE user6 SET status = 1 WHERE phone_number= '1234';
     SELECT * FROM user6;
     +----+------+--------------+--------+
     | id | name | phone_number | status |
     +----+------+--------------+--------+
     |  1 | aa   | 123          |      0 |
     |  2 | bb   | 1234         |      1 |
     |  3 | cc   | 1263456      |      0 |
     +----+------+--------------+--------+
     status为1,说明用户已经被封,该用户不可以参加活动
     
     我们取用户的时候加上status的判断,如:
     SELECT * FROM user6 WHERE status = 0;
     +----+------+--------------+--------+
     | id | name | phone_number | status |
     +----+------+--------------+--------+
     |  1 | aa   | 123          |      0 |
     |  3 | cc   | 1263456      |      0 |
     +----+------+--------------+--------+
    
  • Foreign key constraints:
    two tables are involved: the main table and the sub table (parent table, child table);
    1. Data values ​​that are not in the classes of the main table (parent table) cannot be used in the sub table (child table) of.
    2. The records in the main table are referenced by the sub-table and cannot be deleted.

     mysql> create table classes(id int primary key,name varchar(20));
     Query OK, 0 rows affected (0.04 sec)
     
     mysql> create table students(id int primary key,name varchar(20),class_id int, foreign key(class_id) references classes(id));
     Query OK, 0 rows affected (0.04 sec)
     
     mysql> desc classes;
     +-------+-------------+------+-----+---------+-------+
     | Field | Type        | Null | Key | Default | Extra |
     +-------+-------------+------+-----+---------+-------+
     | id    | int         | NO   | PRI | NULL    |       |
     | name  | varchar(20) | YES  |     | NULL    |       |
     +-------+-------------+------+-----+---------+-------+
     2 rows in set (0.00 sec)
     
     mysql> desc students;
     +----------+-------------+------+-----+---------+-------+
     | Field    | Type        | Null | Key | Default | Extra |
     +----------+-------------+------+-----+---------+-------+
     | id       | int         | NO   | PRI | NULL    |       |
     | name     | varchar(20) | YES  |     | NULL    |       |
     | class_id | int         | YES  | MUL | NULL    |       |
     +----------+-------------+------+-----+---------+-------+
     3 rows in set (0.00 sec)
     
     mysql> insert into classes values(1,'一班');
     Query OK, 1 row affected (0.01 sec)
     
     mysql> insert into classes values(2,'二班');
     Query OK, 1 row affected (0.01 sec)
     
     mysql> insert into classes values(3,'三班');
     Query OK, 1 row affected (0.01 sec)
     
     mysql> insert into classes values(4,'四班');
     Query OK, 1 row affected (0.01 sec)
     
     mysql> select * from classes;
     +----+--------+
     | id | name   |
     +----+--------+
     |  1 | 一班   |
     |  2 | 二班   |
     |  3 | 三班   |
     |  4 | 四班   |
     +----+--------+
     4 rows in set (0.00 sec)
     
     mysql> insert into students values(1001,'张三',1);
     Query OK, 1 row affected (0.01 sec)
     
     mysql> insert into students values(1002,'张三',2);
     Query OK, 1 row affected (0.01 sec)
     
     mysql> insert into students values(1003,'张三',3);
     Query OK, 1 row affected (0.01 sec)
     
     mysql> insert into students values(1004,'张三',4);
     Query OK, 1 row affected (0.01 sec)
     
     
     mysql> select * from students;
     +------+--------+----------+
     | id   | name   | class_id |
     +------+--------+----------+
     | 1001 | 张三   |        1 |
     | 1002 | 张三   |        2 |
     | 1003 | 张三   |        3 |
     | 1004 | 张三   |        4 |
     +------+--------+----------+
     4 rows in set (0.00 sec)
     
     mysql> insert into students values(1005,'张三',5);
     ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`test`.`students`, CONSTRAINT `students_ibfk_1` FOREIGN KEY (`class_id`) REFERENCES `classes` (`id`))
     mysql> delete from classes where id=4;
     ERROR 1451 (23000): Cannot delete or update a parent row: a foreign key constraint fails (`test`.`students`, CONSTRAINT `students_ibfk_1` FOREIGN KEY (`class_id`) REFERENCES `classes` (`id`))
    

Guess you like

Origin blog.csdn.net/weixin_44847326/article/details/111810758