sql primary

Install mysql and it is a server---such as Taobao's server

You want navicate---such as mobile Taobao app

1. Relational database: When stored, it is stored in the form of rows and columns. And these rows and columns form a table, and there is a table that is combined to become a database

2. Non-relational database: Nosql is not stored in the form of rows and columns

DDL language

        Among them, the statement verbs create, alter and drop. Create a new table or modify in the database, delete a table (create table or drop table), add an index to the table, etc.

        DDL--create database table, delete database, modify table structure, etc.

        1. About the operation of the database

                1. Create a database

                               create database database name

                2. See the data you currently have

                                show databases

                3. Switch to the specified database

                                use database name

                4. Delete the database

                                drop database database name

Note: The database to be deleted must be created by yourself, do not delete the database that comes with the system, and the database is not case-sensitive.

2. About the operation of the table

        1. Create a table

create table Student(
id int primary key auto_increment,--列名:id 数据类型:int(数据类型整形)
--primary key:表示该列为主题列。特点该列的值不能为null不可以重复
--auto_increment:表示该列的值可以递增。 该列的类型必须为int类型
--varchar(长度); 字符串类型
name varchar(20),             
tel char(11),
birthday data,
salary decimal(5,2)--5位 小数2位
);
create table 表名(
  列名 数据类型,
  列名 数据类型
  .....
  --注意:最后一列不在使用逗号
);

        2. Display the existing tables in the current database

                        show tables;

        3. Display the structure of the table

                        desc Student;

        4. Delete the table

                        drop table Student;

        5. Modify the structure of the table

                (1) Add a column

                                Add a column from the student table. The column name is set and the data type is varchar

                                alter table Student add column sex varchar(2);

                        (2) Modify the name of the column

                                Modify a column from the student table and change the sex to aaa, the data type is varchar

                                 alter table Student change column sex aaa varchar(2);

                        (3) Modify the data type of the column

                                Modify the column from the student table and change the data type in aaa to int

                                alter table Student modify column aaa int;

                        (4) delete a column

                                        remove column name is aaa from student table

                                        alter table Student drop column aaa;

The most data types: int tinyint double decimal

If we want to store money and other data in the database, we cannot use float double at this time, because these types will lose precision, use decimal

The difference between char and varchar:

        The length of char(20) is immutable - if the number of characters you store is less than the length you set, the MySQL bottom layer will allocate space according to 20. Generally used for strings with fixed lengths such as: mobile phone number ID card number

        varchar(20) has variable length--if the number of characters you store is less than the length you set, the underlying MySQL installation allocates space for you to store the number of characters

DML language - add data

       DML - operate on records in a table

        1. Add data for all fields

                        Note: Use single quotes for characters, not double quotes

                (1) Add all the data

                                insert into Student (id,name,age,address) value(1,'Zhang San','Shanghai');

                                If you add all the column values ​​then you can omit the column name

                                insert into

                                insert into Student value();

                (2) Because the primary key is an incremental mode to add data, you can not specify a value for the primary key

                                insert into Student value( null,'Li Si','Zhengzhou');

                (3) Add some columns

                                insert into Student(name,age) value('孙琪',18);

                (4) Add multiple records

                                insert into Student values(null,'Zhao San'22,'Henan'),(null,'Zhao Si'24,'Hangzhou')

          2. Modify the record

                        -- update table name set column name = 'value', column name = value where is the condition

                        update Student set name='赵六' ,age=18 where id=5;

        3. Delete records

                (1) Delete according to conditions

                                --delete from table condition id=2

                                delete from Student where id=2;

                (2) Scope deletion

                                --Logical characters link multiple conditions and (and) or (or)

                                delete form Student where age<15 or name='李四'

Guess you like

Origin blog.csdn.net/ne_123456/article/details/124369048