[Practical exercise] database basic knowledge and basic principles Series 03- database SQL commands

It has been introduced database theory, mysql5.7 installation, database design / development, and when the installation is complete database, and application development using c, c ++, java, python, etc. become different languages, databases need to use a special SQL language to operate .


Operation of the database, basically add, delete, change, search, categories 5 connecting operation, can be installed prior to the actual operation test mysql5.7 employed.

1, log on and create a database

# Log into the database

mysql -u root -p

# View existing database

show databases;

# Create a database named testdatabase using utf8 encoding

create database testdatabase default character set utf8 collate utf8_bin;

# Use testdatabase database

use testdatbase


2, by:

2.1 Create a database table

create table <tablename> (column_name column_type);

# Example:

create table test (
  test_id int auto_increment,
  test_username varchar(20) not null,
  test_number varchar(10) not null,
  primary key (test_id)
);

2.2 into the database

insert into test (test_id,test_username,test_number)
values
(1,'zhangsan','3');


3, delete

3.1 Delete Row

delete from test where test_id = 4;

3.2 Delete table

3.3 Delete Database


4, check

4.1 an exact match

select test_username,test_number
from test
where test_id = 4;

4.2 Fuzzy Match

select test_username,test_number
from test
where test_username like '%test2';


5, change

5.1 update data

update <table_name> set field1=new_value1,field2=new_value2

# Example:

update test set test_username='test0',test_number='1' where test_id=1;

5.2 Insert Column (new field)

alter table TABLE_NAME add column NEW_COLUMN_NAME varchar(20) not null;


6, connection

Deletions search command to change the above-described entry are good, because of the above commands are basically single-table operation on it. But for the table has any relationship, how to conduct cross-table queries, um, we need to use connection (UNION) a.

We can take on a ([practical exercise] paradigm of basic knowledge and principles of 02- database design and development of a series of database https://blog.51cto.com/14423403/2418782 ) instances to join query try.

Suppose now that I am asking for, we enrolled student number ID number 0001 of course, name, age, performance, instructor name, title, class time, Venue. Well, obviously single-table queries is not enough, because different fields are all scattered in the student table, transcripts, curriculum, teacher tables inside. You need to write SQL statements to connect the four tables to the query to the desired result.

Then you need to think like this:

1) First, the conditions of the final filter, 0001 is the ID number of courses, the curriculum so that the inside cno = '2019030001'; written in SQL is where cno = '2019030001';

2) identify the needs of the field, the field names in each table inside

Student ID: student.sno

Name: student.sname

Age: student.sage

Results: score.cscore

Instructor Name: teacher.tname

Title: teacher.ttitle

Class time: course.ctime

Venue: course.cplace

3) clearly between each table that are connected by the foreign key and primary key written in the form = B A. foreign key to the table is connected to, for example: transcript inside, foreign key references the table school student number sno , cno curriculum.

So that the connection is join student.sno = score.sno, then join score.sno = course.cno, you can watch through the middle, connecting students with the curriculum up table.

4) In addition, if you write the full name of each table, very complicated, so you can customize aliases, such as student a, then back student.sno only need to write a.sno ​​it.

Eventually statement:

select a.sno,a.sname,a.sage,b.cscore,c.ctime,c.cplace,d.tname,d.ttitle from student a 
join score b on a.sno = b.sno_id 
join course c on b.cno_id = c.cno 
join teacher d on c.tno_id = d.tno 
where c.cno = '2019030001';

Ultimately can be achieved, the query "enrolled student number ID number 0001 of course, name, age, performance, instructor name, title, class time, Venue" This complex queries across the table.


Guess you like

Origin blog.51cto.com/14423403/2418820