2, MYSQL basic database commands and constraints

[Note] Note Do not forget to sql statement behind ";", said terminator


1. How do you log database server

        C:\Users\zhg>mysql -uroot -p

        Enter password: ***********

2, how to query the database server for all databases

        mysql> show databases;

3, how to select a database

        use + database name;

4, view the data in the database table selected on the basis of

        show tables;

5, a view of the table based on the above field

        select * from + 表名;

6, out of the database server command

        exit;

7, how to create a database in the database server, create a data table (create test databases here, create a pet data table)

       > Create database test; // create database

       > Show databases; // see if the database was created successfully

       > Use test; // Select the database to be operated

       >CREATE VARCHAR pet(

            name VARCHAR(20),

            ower VARCHAR(20),

            species VARCHAR(20),

            sex CHAR(1),

            birth DATE,

            death DATE); // create a data table

        > Show tables; // see data table is successfully created

        > Describe pet; // Check specific configuration data table

        > Drop table pet; // Delete table

8, recorded in the table was added

        >INSERT INTO pet

           VALUES ( 'kiki', 'zhang', 'hamster', 'f', '2020-01-01', NULL) // create corresponding to the structure of the table is added to the recording value

9, records in the table view

       >select * from pet;

10, mysql common data types

(1) Numerical


 

(2) the Date / Time


 

(3) a string (character) type


 

[Note] The above data type selection:

Date / Time: mainly to see the format

Numeric types, string types: range mainly depends on the size of

11, delete data (name deleted record here Want)

    >delete from pet where name='旺旺'

12, modify the data (the owner's name is recorded to zhang Want)

    >update pet set name='旺旺' where owner='zhang';

【to sum up】

By: insert

删: delete

改:update

Charles: select

13, mysql to build the table constraint

(1) primary key constraint

[Function] it can uniquely identify a record in the table, that is, we passed a field to add a constraint, you can make the field not repeated nor empty.

[Sentence 1] to create a new table called user, id to add a primary key constraint to achieve the following:

    >    create table user(

            id int primary key,

            name varchar (20));

Add Record:

    > Insert into user values ​​(1, 'John Doe');

    >insert into user values (2,'李四');

[Note] where id primary key, it can only add unique id data (if simultaneously added the above 1,2 to 1, then the error will)

 

Primary key, create user2, id and name to add a primary key statement at the same time [2], as long as the two are different, even if there is a unique

       >  create table user2(

            id int,

            name varchar(20),

            primary key(id,name));

添加记录:

    >insert into user2 value (1,'张三');

     >insert into user2 value (1,'张三');

【注】这里为联合主键,虽然id一样但是name也叫做唯一

【拓展】在创建表的时候,忘了添加主键约束:

——如何添加 add

【语句】创建表user4,后期再添加主键

    > create table user4(

    -> id int ,

    -> name varchar(20));

//添加主键

    >alter table user4 add primary key(id);

——如何删除 drop

【语句】

>alter table user4 drop primary key;

——使用modify修改字段,添加约束

【语句】

>alter table user4 modify id int primary key;

(2)自增约束 auto_increment

【语句】创建表user3,同时给id添加主键约束与自增约束,在添加记录时,只需要添加name记录,id就会实现自动生成并随着name 的添加而实现自增

    >create table user3(

     id int primary key auto_increment,

    name varchar(20));

    >insert into user3 (name) values('王五');

    >insert into user3 (name) values('王五');

(3)唯一约束 unique

【语句】创建表user5,同时给name添加唯一约束

     >create table user5(

    -> id int,

    -> name varchar(20));

    >alter table user5 add unique(name);

//第二种方式

    >create table user6(

    -> id int,

    -> name varchar(20),

    -> unique(name));

//第三种方式

    > create table user7(

    -> id int,

    -> name varchar(20) unique);

//联合方式(与联合主键一样,两者只要有一个不同就是唯一)

    >create table user8(

    -> id int,

    -> name varchar(20),

    -> unique(id,name));

——删除user7中的唯一约束

    >alter table user7 drop index name;

—— 修改唯一约束

    > alter table user7 modify name varchar(20) unique;

对唯一性的验证可自行验证哦

(4)非空约束 not null

【语句】创建user9,添加非空约束
     >create table user9(

    -> id int,

    -> name varchar(20) not null);

【验证】

       > insert into user9 (id) value (1);            只添加id的话,会报错,因为name不能非空

       >insert into user9 value (1,'zhang');            成功

——后期添加

    >alter table user10 modify name varchar(20) not null;

——删除

    >alter table user10 modify name varchar(20) null;

(5)默认约束

【语句】创建user10

    > create table user10 (

    -> id int,

    -> name varchar(20),

    -> age int default 10);

或者后期再添加

——删除

    >alter table user10 alter age drop default;

——添加

    >alter table user10 alter age set default 5;

(6)外键约束

【注】涉及两个表之间的关联,主表--副表,父表--子表

【语句】创建两个表classes,students

——classes表

    > create table classes(

    -> id int primary key,

    -> name varchar(20));

——students表

    > create table students(

    -> id int,

    -> name varchar(20),

    -> class_id int,

    -> foreign key(class_id) references classes(id));

添加记录:

——添加班级

    > insert into classes value(1,'一班');

    > insert into classes value(2,'二班');

    > insert into classes value(3,'三班');

——添加学生

    >insert into students value(1,'张三',1);

    >insert into students value(1,'张三',2);

    >insert into students value(1,'张三',3);

所谓外键约束:

1、主表(父表)中没有的数据在副表(子表)中是不可以使用的(这里对于学生的添加时,所选班级会有限制,只能是1,2,3)

2、主表中的记录被副表引用时是不可以被删除的(这里的班级已经都被副表引用,所以不可以删除)

3、作为外键必须是唯一的字段才能做外键,即必须是主键才有资格称为外键;

 

【总结】

添加约束

1、建表的时候就直接添加约束

2、alter .......add.......

3、alter.........modify.....

删除约束

alter ........drop ......

 

【补充】

1、更改字段名(将con改为cno)

>alter table course change con cno varchar(20);

2、删除字段(删除名为cno的字段)

>alter table course drop column cno;

3、增加字段(只能默认在后面添加字段,这里添加的是cno)

>alter table course add cno varchar(20);

4、调整字段的相对位置

(1)将cno放到tno前面

>alter table course modify tno varchar(20) after cno;

(2)将tno放到整个字段的最前面

>alter table course modify tno varchar(20) first;

 

Guess you like

Origin www.cnblogs.com/guo-2020/p/12306728.html