The most commonly used statements in mysql database

Common commands for mysql database

build database:

create database name;

eg:

(I need to build a database about students)

create database Stu_course;

Select the database:

use databasename;

eg:

use Stu_course;

Create a table in the established database:

create table name(column1 datatype,column2 datatype)

eg:

Create table Course(Cno char(9) primary key,
Cname char(40),
Cpno char(4),
Ccredit smallint,
);

Set the foreign code and primary key:

Foreign code:

Foreign key(column1) references tablename(column2)/*column1被设置为外键,参考的是某一个表中的column2*/

Primary key:

 primary key

eg:

(For example, I want to set Cno in the Course table as the primary key and Ccredit as the foreign key)

Create table Course(Cno char(9) primary key,

Cname char(40),

Cpno char(4),

Ccredit smallint,

Foreign key(Cpno) references Course(Cno)

);

Add and delete columns in the table:

Add column:

alter table tablename add column datatype;

eg:

alter table course add ctype char(10);

Delete column:

alter table tablename drop column columnname;

eg:

alter table course drop column ctype;

Table reuse name and delete:

Table reuse name:

rename table tablename to Reuse name;

eg:

rename table course to course1;

Delete table:

set foreign_key_checks = 0;/*这个操作是关闭外码,如果有需关闭否则不能执行操作,切记*/
drop table tablename;

eg:

set foreign_key_checks = 0;
drop table course1;

Add, modify, and delete data to the table:

adding data:

insert into tablename(column1column2,column3,column4)
values(value1,value2,value3,value4),(value5,value6,value7,value8);

eg:

insert into Course(Cno,Cname,Cpno,Ccredit)
values('1','数据库','5',4),
('2','数学',,2),
('3','信息系统','1',4),
('4','操作系统','6',3),
('5','数据结构','7',4),
('6','数据处理',,2),
('7','PASCAL语言','6',4);

The achieved effect is as follows:

BJ0BQK.png

change the data:

update tablename set column1 = value1 where column2 = value2;
/*选中一个表,然后将列为column1,行为column2 = value2的值改为value1,column2的选则最好是主键,因为要确保唯一性*/

eg:

(Change the credits with the course number "2" in the Course table to 4)

update Course set Ccredit =4 where Cno = 2;

The effect is as follows:

BJyngI.png

Delete a row of data:

Delete from tablename where column = value;

eg:

Delete from course where Cno = ‘3’;

(I deleted the information system line with Cno='3')

BJ66w8.png

Guess you like

Origin blog.csdn.net/qq_46354489/article/details/109368001